package com.andela.notelib.note;


import android.content.Context;

import com.andela.notelib.util.Settings;
import com.andela.notelib.util.SortById;

import java.util.Collections;

public class NoteManager {
    public static final int NOTES = 0;
    public static final int TRASH = 1;

    private String COUNT = "count";
    private int counter;
    private NoteDBWriter<Note> noteDBWriter;
    private NoteList<Note> notes = new NoteList<>();
    private NoteList<Note> trashNotes = new NoteList<>();
    private Context context;

    public NoteManager(Context context, String tableName) throws Exception {
        this.noteDBWriter = new NoteDBWriter<>(tableName);

        this.notes = this.noteDBWriter.selectAllNote(NOTES);
        Collections.sort(this.notes, new SortById());

        this.trashNotes = this.noteDBWriter.selectAllNote(TRASH);
        Collections.sort(this.trashNotes, new SortById());

        this.COUNT += tableName;
        this.context = context;
        counter = getCounter();
    }

    public void saveNote(Note note) {
        note.setId(counter);
        note.setTrash(NOTES);
        this.notes.addOrderedNote(note);
        this.noteDBWriter.insertNote(note);
        counter++;
    }

    public void updateNote(Note note) {
        this.notes.updateNote(note);
        this.noteDBWriter.updateNote(note);
    }

    public boolean containsNote(Note note) {
        return this.notes.containsNote(note);
    }

    public void moveNoteToTrash(Note note) {
        this.notes.removeNote(note);
        note.setTrash(TRASH);
        this.trashNotes.addOrderedNote(note);
        this.noteDBWriter.updateNote(note);
    }

    public void restoreNoteFromTrash(Note note) {
        this.trashNotes.removeNote(note);
        note.setTrash(NOTES);
        this.notes.addOrderedNote(note);
        this.noteDBWriter.updateNote(note);
    }

    public void deleteFromTrash(Note note) {
        this.trashNotes.removeNote(note);
        this.noteDBWriter.deleteNote(note.getId());
    }

    public void deleteNote(Note note) {
        this.trashNotes.removeNote(note);
        this.notes.removeNote(note);
        this.noteDBWriter.deleteNote(note.getId());
    }

    public void deleteAllTrash() {
        this.trashNotes.clear();
        this.noteDBWriter.deleteAlNote(TRASH);
    }

    public void deleteAll() {
        this.notes.clear();
        this.trashNotes.clear();
        this.noteDBWriter.deleteAlNote(NOTES);
        this.noteDBWriter.deleteAlNote(TRASH);
    }

    public void saveChanges(){
        saveCounter(counter);
    }

    public NoteList<Note> getAllNotes() {
        return this.notes;
    }

    public NoteList<Note> getAllTrashNotes() {
        return this.trashNotes;
    }

    public Note getNote(Note note) {
        return this.notes.getNote(note.getId());
    }

    public Note getTrashNote(Note note) {
        return this.trashNotes.getNote(note.getId());
    }

    public NoteList<Note> getFolderNotes(int index) {
        return this.notes.getFolderNotes(index);
    }

    public int getNotesSize() {
        return this.notes.size();
    }

    public int getTrashNotesSize() {
        return this.trashNotes.size();
    }

    public boolean isNotesEmpty() {
        return this.getNotesSize() == 0;
    }

    public boolean isTrashNotesEmpty() {
        return this.getTrashNotesSize() == 0;
    }

    private void saveCounter(int counter) {
        Settings.saveInt(context, COUNT, counter);
    }

    private int getCounter() {
        return Settings.fetchInt(context, COUNT, 0);
    }
}

@@ -1018,28 +1018,28 @@
 %0A    public 
-void
+Note
  saveNote(No
@@ -1161,61 +1161,38 @@
     
-this.noteDBWriter.insertNote(note);%0A        counter++
+counter++;%0A        return note
 ;%0A  
@@ -1277,52 +1277,8 @@
 e);%0A
-        this.noteDBWriter.updateNote(note);%0A
     
@@ -1540,52 +1540,8 @@
 e);%0A
-        this.noteDBWriter.updateNote(note);%0A
     
@@ -1710,52 +1710,8 @@
 e);%0A
-        this.noteDBWriter.updateNote(note);%0A
     
@@ -2343,32 +2343,124 @@
 (TRASH);%0A    %7D%0A%0A
+    public int getNotePosition(Note note) %7B%0A        return this.notes.indexOf(note);%0A    %7D%0A%0A
     public void 
@@ -2472,16 +2472,17 @@
 hanges()
+ 
 %7B%0A      
@@ -2497,32 +2497,525 @@
 unter(counter);%0A
+        makeChangesToDatabase(this.notes);%0A        makeChangesToDatabase(this.trashNotes);%0A    %7D%0A%0A    private void makeChangesToDatabase(NoteList%3CNote%3E noteList) %7B%0A        if (noteList.size() %3E 0) %7B%0A            for (Note note : noteList) %7B%0A                if (this.noteDBWriter.selectById(note.getId()) == null) %7B%0A                    this.noteDBWriter.insertNote(note);%0A                %7D else %7B%0A                    this.noteDBWriter.updateNote(note);%0A                %7D%0A            %7D%0A        %7D%0A
     %7D%0A%0A    publi
@@ -3252,32 +3252,116 @@
 etId());%0A    %7D%0A%0A
+    public Note getNoteById(int id) %7B%0A        return this.notes.getNote(id);%0A    %7D%0A%0A
     public Note 

Add getNotePosition Method and Refactor class