package dp.mendelu.roman.diplomkaapp42;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

/**
 * List Aktivita, zobrazuje lokalni databazi.
 */
public class ListActivity extends android.app.ListActivity {
    private final String TAG = "ListActivity";
    private SQLiteDatabase db=null;
    private Cursor mCursor=null;
    private String columns[] = new String[]{"_id", "updated_at", "created_at", "doc_id", "json", "latitude", "longitude"};

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_list); //nesmi byt u ListActivity, pada na null
        
        db = (new DatabaseHelper(this)).getWritableDatabase();
        //Upgrade smaze celou tabulku!!!
        //DatabaseHelper dq = new DatabaseHelper(this);
        //dq.onUpgrade(db,1,2);

        //mCursor = db.rawQuery("SELECT _ID, layer, latitude, longitude, project" +
        //        " FROM geometry ORDER BY _ID", null);

        //mCursor = getContentResolver().query(Uri.parse("content://dp.mendelu.roman.diplomkaapp42/geometry"), );

        mCursor = db.query(Constants.TABLE_NAME, columns, null, null, null, null, "_ID");
        /**
         * Kontrola sql dotazu
         */
        mCursor.moveToFirst();
        while (!mCursor.isAfterLast()){
            String id = mCursor.getString(0);
            String updated_at = mCursor.getString(1);
            String created_at = mCursor.getString(2);
            String doc_id = mCursor.getString(3);
            String json = mCursor.getString(4);
            String latitude = mCursor.getString(5);
            String longitude = mCursor.getString(6);

            Log.i(TAG, id + updated_at + created_at + doc_id + json + latitude + longitude);
            mCursor.moveToNext();

        }

        /*ListAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.row, this.mCursor,
                new String[] {"_id","layer", "project", "latitude", "longitude"},
                new int[] {R.id.idecko, R.id.layer, R.id.project, R.id.latitude, R.id.longitude});

        setListAdapter(adapter);
        registerForContextMenu(getListView());
        */
        CursorAdapter mCursorAdapter = null;
        mCursorAdapter = new SimpleCursorAdapter(
                getApplicationContext(),
                R.layout.row,
                mCursor,
                columns,
                new int[]{R.id.idecko, R.id.updated_at, R.id.created_at, R.id.doc_id, R.id.json,
                R.id.latitude, R.id.longitude},
                0);

        setListAdapter(mCursorAdapter);
        registerForContextMenu(getListView());

        ListView lv;
        lv = getListView();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCursor.moveToPosition(position); //nastavim pozici kliknuteho radku
                Log.i("position", position + "");
                Log.i("id", "" + id);
                Intent intent = new Intent(getApplicationContext(), DetailJSONActivity.class);
                intent.putExtra("id", mCursor.getString(0)); //predavam ID

                intent.putExtra("json", mCursor.getString(4)); // posilam stringovej json, jen pro kontrolu. nema vyznam

                Log.i("id v sql", mCursor.getString(0));
                //Log.i("sql json", mCursor.getString(4)); // pokud null, spadne, osetreno az v detailu

                startActivity(intent);
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        mCursor.close();
        db.close();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

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

    @Override
    protected void onRestart() {
        super.onRestart();
        recreate();
        Toast.makeText(this,"Refreshing list", Toast.LENGTH_SHORT).show();
    }
}

