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

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

import java.util.ArrayList;
import java.util.List;

import kz.arta.synergy.workflow.mobile.android.common.models.offline.completion.OfflineCompletionResult;

/**
 * Created by neox on 20.08.14.
 */
public class AddResultWork extends SQLiteOpenHelper {

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "AcceptRejectWork";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "resultID text,"
            + "signal text,"
            + "startDate text,"
            + "finishDate text,"
            + "progress text,"
            + "comment text,"
            + "name text"
            + ");";

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

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

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

    }

    public boolean accept(String actionID, String resultID, String signal, String name) {
        try {
            db.delete(TABLE_NAME, "actionID = ?", new String[]{actionID});
            ContentValues cv = new ContentValues();
            cv.put("actionID", actionID);
            cv.put("resultID", resultID);
            cv.put("signal", signal);
            cv.put("name", name);
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

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

    public boolean reject(OfflineCompletionResult offlineCompletionResult) {
        try {
            db.delete(TABLE_NAME, "actionID = ?", new String[]{offlineCompletionResult.getActionID()});
            ContentValues cv = new ContentValues();
            cv.put("actionID", offlineCompletionResult.getActionID());
            cv.put("resultID", offlineCompletionResult.getResultID());
            cv.put("signal", offlineCompletionResult.getSignal());
            cv.put("startDate", offlineCompletionResult.getStartDate());
            cv.put("finishDate", offlineCompletionResult.getFinishDate());
            cv.put("progress", offlineCompletionResult.getProgress());
            cv.put("comment", offlineCompletionResult.getComment());
            cv.put("name", offlineCompletionResult.getName());
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public List<OfflineCompletionResult> get() {
        List<OfflineCompletionResult> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.query(TABLE_NAME, null, null, null, null, null, null);
            if (c.moveToFirst()) {
                do {
                    OfflineCompletionResult item = new OfflineCompletionResult();
                    item.setActionID(c.getString(c.getColumnIndex("actionID")));
                    item.setResultID(c.getString(c.getColumnIndex("resultID")));
                    item.setSignal(c.getString(c.getColumnIndex("signal")));
                    item.setStartDate(c.getString(c.getColumnIndex("startDate")));
                    item.setFinishDate(c.getString(c.getColumnIndex("finishDate")));
                    item.setProgress(c.getString(c.getColumnIndex("progress")));
                    item.setComment(c.getString(c.getColumnIndex("comment")));
                    item.setName(c.getString(c.getColumnIndex("name")));
                    result.add(item);
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }
}
