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.CompletionResultItem;

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

    private SQLiteDatabase db = getWritableDatabase();
    public static final String TABLE_NAME = "GetOfflineResult";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "resultID text,"
            + "is_file text,"
            + "type text,"
            + "comment text,"
            + "user_name text,"
            + "created text,"
            + "file_name text"
            + ");";

    public WorkResultTable(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 insertResult(String jsonString, String actionID) {
        try {
            CompletionResultItem completionResultItem = RestClient.getGson().fromJson(jsonString, CompletionResultItem.class);
            completionResultItem.setActionID(actionID);
            ContentValues cv = new ContentValues();
            db.delete(TABLE_NAME, "actionID = ?", new String[]{completionResultItem.getActionID()});
            cv.put("actionID", completionResultItem.getActionID());
            cv.put("resultID", completionResultItem.getResultID());
            cv.put("is_file", completionResultItem.getIs_file());
            cv.put("type", completionResultItem.getType());
            cv.put("comment", completionResultItem.getComment());
            cv.put("user_name", completionResultItem.getUser_name());
            cv.put("created", completionResultItem.getCreated());
            cv.put("file_name", completionResultItem.getFile_name());
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public CompletionResultItem get(String actionID) {
        CompletionResultItem result = new CompletionResultItem();
        Cursor c = null;
        try {
            c = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE actionID = ?", new String[]{actionID});
            if (c.moveToFirst()) {
                do {
                    result.setActionID(c.getString(c.getColumnIndex("actionID")));
                    result.setResultID(c.getString(c.getColumnIndex("resultID")));
                    result.setIs_file(c.getString(c.getColumnIndex("is_file")));
                    result.setType(c.getString(c.getColumnIndex("type")));
                    result.setComment(c.getString(c.getColumnIndex("comment")));
                    result.setUser_name(c.getString(c.getColumnIndex("user_name")));
                    result.setCreated(c.getString(c.getColumnIndex("created")));
                    result.setFile_name(c.getString(c.getColumnIndex("file_name")));
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

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