package com.test.smartnotes;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.test.smartnotes.database.DBAdapter;

public class ListNotesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        DBAdapter.init(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_notes);
        Toolbar toolbar = (Toolbar) findViewById(R.id.list_notes_toolbar);
        setSupportActionBar(toolbar);

//        FloatingActionButton createNoteButton = (FloatingActionButton) findViewById(R.id.create_note_button);
//        createNoteButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
////                        .setAction("Action", null).show();
//                startActivity(new Intent(ListNotesActivity.this, CreateNoteActivity.class));
//            }
//        });

        ListView listView = (ListView)findViewById(R.id.notes_listView);
        ListNotesAdapter listNotesAdapter = new ListNotesAdapter(this, DBAdapter.getAllNoteData());
        listView.setAdapter(listNotesAdapter);
        registerForContextMenu(listView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            Log.d("Clicked item id", "itemClick: position = " + position + ", id = " + id);
            Intent intent = new Intent(ListNotesActivity.this, ViewNoteActivity.class);
            intent.putExtra("id", (int) id);
            startActivity(intent);
            }
        });

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.view_note_menu, menu);
    }

    public void onRemoveClick (final View view) {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        DBAdapter.deleteNoteData(String.valueOf(view.getTag()));
                        dialog.dismiss();
                        refreshListView();
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        dialog.dismiss();
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.are_you_sure)
                .setPositiveButton(R.string.option_yes, dialogClickListener)
                .setNegativeButton(R.string.option_no, dialogClickListener).show();
    }

    private void refreshListView () {
        // Most likely not the best way to solve the refresh task but it works
        ListView listView = (ListView)findViewById(R.id.notes_listView);
        ListNotesAdapter listNotesAdapter = new ListNotesAdapter(this, DBAdapter.getAllNoteData());
        listView.setAdapter(listNotesAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.list_notes_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

}

@@ -124,114 +124,8 @@
 le;%0A
-import android.support.design.widget.FloatingActionButton;%0Aimport android.support.design.widget.Snackbar;%0A
 impo
@@ -159,24 +159,24 @@
 lertDialog;%0A
+
 import andro
@@ -372,16 +372,46 @@
 flater;%0A
+import android.view.MenuItem;%0A
 import a
@@ -467,43 +467,8 @@
 ew;%0A
-import android.widget.ImageButton;%0A
 impo
@@ -921,549 +921,8 @@
 );%0A%0A
-//        FloatingActionButton createNoteButton = (FloatingActionButton) findViewById(R.id.create_note_button);%0A//        createNoteButton.setOnClickListener(new View.OnClickListener() %7B%0A//            @Override%0A//            public void onClick(View view) %7B%0A////                Snackbar.make(view, %22Replace with your own action%22, Snackbar.LENGTH_LONG)%0A////                        .setAction(%22Action%22, null).show();%0A//                startActivity(new Intent(ListNotesActivity.this, CreateNoteActivity.class));%0A//            %7D%0A//        %7D);%0A%0A
     
@@ -2024,38 +2024,41 @@
 oid 
-on
 Remove
-Click (final View view
+NoteDialog (final String id
 ) %7B%0A
@@ -2393,37 +2393,10 @@
 ata(
-String.valueOf(view.getTag())
+id
 );%0A 
@@ -3444,24 +3444,24 @@
 enu, menu);%0A
-
         retu
@@ -3503,10 +3503,705 @@
 %0A    %7D%0A%0A
+    @Override%0A    public boolean onContextItemSelected(MenuItem item) %7B%0A        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();%0A        switch (item.getItemId()) %7B%0A            case R.id.action_edit_note:%0A//                Intent intent = new Intent(ListNotesActivity.this, EditNoteActivity.class);%0A//                intent.putExtra(%22id%22, info.id);%0A//                startActivity(intent);%0A                return true;%0A            case R.id.action_remove_note:%0A                RemoveNoteDialog(String.valueOf(info.id));%0A                return true;%0A            default:%0A                return super.onContextItemSelected(item);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add Remove the Note function to context menu