package net.ddns.mahaljsp.ch13_01_sqlite;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    List<Item> list;
    ItemDAO itemDAO;
    ItemAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        processViews();
        processControllers();
    }
    public void processViews(){
        listView=(ListView)findViewById(R.id.listView);
    }
    public void processControllers(){
        itemDAO=new ItemDAO(this);
        if (itemDAO.getCount()==0)itemDAO.sample();
        list=itemDAO.getAll();
        adapter=new ItemAdapter(this, list);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.moidfy, menu);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode== Activity.RESULT_OK){
            refresh();
        }
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Intent intent=new Intent(this, UpdateActivity.class);
        final long id=((Item)(list.get(info.position))).getId();
        switch(item.getItemId()){
            case R.id.insert:
                startActivityForResult(intent, 100);
                break;
            case R.id.update:
                intent.putExtra("ID", id);
                startActivityForResult(intent, 101);
                break;
            case R.id.delete:
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle("刪除")
                        .setMessage("確定要刪除?");
                dialog.setPositiveButton(getString(android.R.string.ok),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                itemDAO.delete(id);
                                refresh();
                            }
                        });
                dialog.setNegativeButton(getString(android.R.string.cancel), null);
                dialog.show();
                break;
        }
        return super.onContextItemSelected(item);
    }
    public void refresh(){
        list=itemDAO.getAll();
        adapter.setList(list);
        adapter.notifyDataSetChanged();
    }
}