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

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

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "AddFormParams";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "formID text,"
            + "dataUUID text,"
            + "fileIdentifier text,"
            + "data text,"
            + "name text"
            + ");";

    public AddFormParams(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 remove() {
        try {
            db.delete(TABLE_NAME, null, null);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

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

    public List<OfflineFormParams> get() {
        List<OfflineFormParams> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.query(TABLE_NAME, null, null, null, null, null, null);
            if (c.moveToFirst()) {
                do {
                    OfflineFormParams item = new OfflineFormParams();
                    item.setActionID(c.getString(c.getColumnIndex("actionID")));
                    item.setFormID(c.getString(c.getColumnIndex("formID")));
                    item.setFileIdentifier(c.getString(c.getColumnIndex("fileIdentifier")));
                    item.setDataUUID(c.getString(c.getColumnIndex("dataUUID")));
                    item.setData(c.getString(c.getColumnIndex("data")));
                    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();
            }
        }
    }
}
