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 java.util.List;

import kz.arta.synergy.workflow.mobile.android.works.models.CompletionFormItem;
import kz.arta.synergy.workflow.mobile.android.works.models.CompletionResultItem;
import kz.arta.synergy.workflow.mobile.android.works.models.WorkItem;

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

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "FormsCompletion";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "typeWork text,"
            + "resultID text,"
            + "is_file text,"
            + "type text,"
            + "comment text,"
            + "user_name text,"
            + "created text,"
            + "file_name text,"
            + "file_icon text,"
            + "fileUUID text,"
            + "formID text"
            + ");";

    public FormCompletionTable(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 insertFormCompletion(String actionID, String type, String isFile, String comment, String filename, String created, String author) {
        try {
            db.delete(TABLE_NAME, "actionID = ?", new String[]{actionID});
            ContentValues cv = new ContentValues();
            cv.put("actionID", actionID);
            cv.put("typeWork", "COMPLETION");
            cv.put("resultID", "");
            cv.put("is_file", isFile);
            cv.put("type", type);
            cv.put("comment", comment);
            cv.put("user_name", author);
            cv.put("created", created);
            cv.put("file_name", filename);
            cv.put("file_icon", "");
            cv.put("fileUUID", "");
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

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

    public boolean addFormCompletion(CompletionResultItem completionResultItem, String actionID) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("actionID", actionID);
            cv.put("typeWork", "COMPLETION");
            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());
            cv.put("file_icon", completionResultItem.getFile_icon());
            cv.put("fileUUID", completionResultItem.getFileUUID());
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public void addFormType(CompletionFormItem completionFormItem, String actionID) throws Exception {
        ContentValues cv = new ContentValues();
        cv.put("actionID", actionID);
        cv.put("typeWork", "NO_COMPLETION");
        cv.put("type", completionFormItem.getResult().getType());
        if (completionFormItem.getForm_params().length != 0 && completionFormItem.getForm_params()[0].getFormID() != null) {
            cv.put("formID", completionFormItem.getForm_params()[0].getFormID());
        }
        db.insert(TABLE_NAME, null, cv);
    }

    public boolean addFormTypeWithRemove(String actionID) {
        try {
            db.delete(TABLE_NAME, "actionID = ?", new String[]{actionID});
            ContentValues cv = new ContentValues();
            cv.put("actionID", actionID);
            cv.put("typeWork", "NO_COMPLETION");
            cv.put("type", "COMMENT");
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public CompletionResultItem getByWorkId(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 = new CompletionResultItem();
                    result.setActionID(c.getString(c.getColumnIndex("actionID")));
                    result.setTypeWork(c.getString(c.getColumnIndex("typeWork")));
                    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")));
                    result.setFile_icon(c.getString(c.getColumnIndex("file_icon")));
                    result.setFileUUID(c.getString(c.getColumnIndex("fileUUID")));
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    public void removeWorksByArray(List<WorkItem> works) {
        try {
            for (WorkItem work : works) {
                db.delete(TABLE_NAME, "actionID = " + "\"" + work.getActionID() + "\"", null);
            }
        } catch (Exception exc) {
            // handling error ...
        }
    }
}
