package kz.arta.synergy.workflow.mobile.android.common.offline.get;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import kz.arta.synergy.workflow.mobile.android.common.requests.RestClient;
import kz.arta.synergy.workflow.mobile.android.works.models.ScreenFinishInfoItem;

/**
 * Created by neox on 04.07.14.
 */
public class FinishProcessTable extends SQLiteOpenHelper {

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "GetFinishProcess";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "need_comment_input text,"
            + "need_enter_positive_comment text,"
            + "need_enter_negative_comment text,"
            + "raw_data text,"
            + "buttons text"
            + ");";

    public FinishProcessTable(Context context) {
        super(context, TABLE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

    }

    public boolean removeAll() {
        try {
            db.delete(TABLE_NAME, null, null);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public boolean add(String jsonString, String actionID) {
        try {
            db.delete(TABLE_NAME, "actionID = ?", new String[]{actionID});
            ContentValues cv = new ContentValues();
            ScreenFinishInfoItem item = RestClient.getGson().fromJson(jsonString, ScreenFinishInfoItem.class);
            cv.put("actionID", actionID);
            cv.put("need_comment_input", item.getNeed_comment_input());
            cv.put("need_enter_positive_comment", item.getNeed_enter_positive_comment());
            cv.put("need_enter_negative_comment", item.getNeed_enter_negative_comment());
            cv.put("raw_data", item.getRaw_data());
            cv.put("buttons", RestClient.getGson().toJson(item.getButtons()));
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public ScreenFinishInfoItem get(String actionID) {
        ScreenFinishInfoItem result = null;
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME + " where actionID = ?", new String[]{actionID});
            if (c.moveToFirst()) {
                do {
                    result = new ScreenFinishInfoItem();
                    result.setActionID(c.getString(c.getColumnIndex("actionID")));
                    result.setNeed_comment_input(c.getString(c.getColumnIndex("need_comment_input")));
                    result.setNeed_enter_positive_comment(c.getString(c.getColumnIndex("need_enter_positive_comment")));
                    result.setNeed_enter_negative_comment(c.getString(c.getColumnIndex("need_enter_negative_comment")));
                    result.setRaw_data(c.getString(c.getColumnIndex("raw_data")));
                    result.setButtonsString(c.getString(c.getColumnIndex("buttons")));
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }
}
