package dp.mendelu.roman.diplomkaapp42;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Detail aktivity pro vybrany zaznam.  Moznost smazani zaznamu.
 */
public class DetailJSONActivity extends Activity {
    private static final String TAG = "DetailJSONActvitiy" ;
    private SQLiteDatabase db=null;
    private Context mContext;
    long id_row = -1; //nesmyslna hodnota

    public long getId_row() {
        return id_row;
    }

    public void setId_row(long id_row) {
        this.id_row = id_row;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_json);
        Intent intent = getIntent();
        final String id = intent.getStringExtra("id");
        String json = intent.getStringExtra("json");
        if (json == null) json = "No JSON";
        setId_row(Long.parseLong(id));

        Log.i("id", getId_row() + "");
        Log.i("id", id);
        Log.i("json", json);

        TextView jsonView = (TextView)findViewById(R.id.jsonView);
        jsonView.setText(json);

        db = (new DatabaseHelper(this).getWritableDatabase());
        mContext = this;
    }

    public void deleteRow(View v){
        deleteRow(getId_row());
    }

    /**
     * Delete row from database
     * @param loc_id_row
     * @return
     */
    public void deleteRow(final long loc_id_row){
        if (loc_id_row>0){
            new AlertDialog.Builder(this)
                    .setTitle(R.string.delete_title)
                    .setPositiveButton(R.string.ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    Cursor mCursor = db.rawQuery("SELECT doc_id, updated_at FROM geometry " +
                                            "WHERE _ID=" + loc_id_row, null);
                                    mCursor.moveToFirst();
                                    String cloud_delete_doc = mCursor.getString(0);
                                    String cloud_updated_at = mCursor.getString(1);
                                    Log.i(TAG, cloud_updated_at);
                                    if(cloud_delete_doc == null) {
                                        cloud_delete_doc = "";
                                    }

                                        Log.i(TAG, cloud_delete_doc);
                                        new AsyncDelRow(mContext, cloud_delete_doc, cloud_updated_at).execute();
                                        //reseno interne v AsyncDelRow, kontrola konfliktu
                                        //db.delete("geometry", "_ID=?", new String[]{String.valueOf(loc_id_row)});
                                        //Log.i(TAG, "Row " + loc_id_row + "delete");
                                        //ReportsActivity.Log(mContext,TAG, "Sync to cloud. Item deleted.");
                                        finish();
                                }
                            })
                    .setNegativeButton(R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    // ignore, just dismiss
                                }
                            })
                    .show();

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
        Log.i("onDestroy", "end");
    }
}
