package com.whitelab.colorquant;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;


public class Quant extends ListActivity {

    private static String logtag = "Quant";
    QuantTableHelper QTH = new QuantTableHelper(this);

    public void toExport(){
        Intent intent = new Intent(this, Export.class);
        intent.putExtra("SEND",this.getString(R.string.title_activity_quant));
        startActivity(intent);
    }

    public void toDropTable(){
        Intent intent = new Intent(this, DropTables.class);
        intent.putExtra("SEND",this.getString(R.string.title_activity_quant));
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quant);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        updateList();
    }

    @Override
    protected void onResume(){
        super.onResume();
        updateList();
    }

    protected void updateList(){
        ArrayList<HashMap<String,String>> quantList = QTH.getAllQuants();
        Log.d(logtag,"QuantList " + Arrays.asList(quantList));

        if(quantList.size() != 0) {
            ListView listView = getListView();

            ListAdapter adapter = new SimpleAdapter(
                    Quant.this, quantList, R.layout.quant_entry,
                    new String[]{"_id", "Alias", "ExpGroup",
                            "RGB1", "RGB2", "RGB3",
                            "Lab1", "Lab2", "Lab3",
                            "Concentration","Distance"},
                    new int[]{R.id.quant_ID, R.id.quant_alias, R.id.quant_group,
                            R.id.quant_R, R.id.quant_G, R.id.quant_B,
                            R.id.quant_L, R.id.quant_a, R.id.quant_b,
                            R.id.quant_conc, R.id.quant_dist});

            setListAdapter(adapter);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_quant, menu);
        return true;
    }

    public void toCaptureQuantSave(View view){
        Intent intent = new Intent(this, CaptureQuantSave.class);
        intent.putExtra("TYPE","Samples");
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()){
            case R.id.action_settings:
                return true;
            case R.id.action_export:
                toExport();
                return true;
            case R.id.action_droptable:
                toDropTable();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
