mirrored_repositories/note-app-sqflite/lib
import 'package:flutter_todo_list/note_list.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_todo_list/model/note.dart';

class DataBaseHelper{
  static DataBaseHelper _dataBaseHelper;      // Singletone DataBaseHelper
  static Database _database;

  String noteTable = 'note_table';
  String colId = 'id';
  String colTitle = 'title';
  String colDescription = 'description';
  String colPriority = 'priority';
  String colDate = 'date';

  DataBaseHelper.createInstance();

  factory DataBaseHelper(){

    if(_dataBaseHelper == null){
      _dataBaseHelper = DataBaseHelper.createInstance();
    }
    return _dataBaseHelper;
  }

  Future<Database> get database async{

    if(_database == null){
      _database = await initializeDatabase();
    }
    return _database;
  }

  Future<Database> initializeDatabase() async{
    //Get the directory path
    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path + 'notes.db';

    //Open/Create the database at a given path
    var notesDatabase =  await openDatabase(path, version: 1, onCreate: _createDb);
    return notesDatabase;
  }

  void _createDb(Database db, int newVersion) async{
    await db.execute('CREATE TABLE $noteTable($colId INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT,'
        '$colDescription TEXT, $colPriority INTEGER, $colDate TEXT)');
  }

  //Fetch operation : Get all the notes from database
  Future<List<Map<String, dynamic>>> getNoteMapList() async{
    Database db = await this.database;
    // var result = await db.rawQuery('SELECT * FROM $noteTable order by $colPriority ASC');
    var result = await db.query(noteTable, orderBy: '$colPriority ASC');
    return result;
  }

  //Insert Operation
  Future<int> insertNote(Note note) async{
    Database db = await this.database;
    var result = await db.insert(noteTable, note.toMap());
    return result;
  }

  //Update operation
  Future<int> updateNote(Note note) async{
    Database db = await this.database;
    var result = await db.update(noteTable, note.toMap(), where: '$colId = ?', whereArgs: [note.id]);
    return result;
  }

  //Delete operation
  Future<int> deleteNote(int id) async{
    Database db = await this.database;
    // var result = await db.delete(noteTable, where: '$colId = $id');
    var result = await db.rawDelete('DELETE FROM $noteTable WHERE $colId = $id');
    return result;
  }

  //Get the number of objects present in the database
  Future<int> getCount() async{
    Database db = await this.database;
    List<Map<String, dynamic>> x = await db.rawQuery('SELECT COUNT (*) from $noteTable');
    int result = Sqflite.firstIntValue(x);
    return result;
  }

  //Get the 'Map List' [ List<Map> ] and convert it to 'Note List' [ List<Note> ]
  Future<List<Note>> getNoteList() async{
    var noteMapList = await getNoteMapList(); //Get the 'Map List' from database
    int count = noteMapList.length; //Count number of Map entries in database

    // ignore: deprecated_member_use
    List<Note> noteList = List<Note>(); // Create empty list of note

    //For loop to create 'Note List' from 'Map List'
    for (int i = 0; i < count ; i++){
      noteList.add(Note.fromMapObject(noteMapList[i]));
    }
    return noteList;
  }
}