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

/**
 * Created by neox on 11.08.14.
 */
public class AddCompletionFile extends SQLiteOpenHelper {

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "AddFormCompletionFile";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "file_name text,"
            + "file_identifier text,"
            + "file_path text,"
            + "resultID text,"
            + "name text,"
            + "from_device text"
            + ");";

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

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

    public List<OfflineCompletionWorkFile> get() {
        List<OfflineCompletionWorkFile> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.query(TABLE_NAME, null, null, null, null, null, null);
            if (c.moveToFirst()) {
                do {
                    OfflineCompletionWorkFile item = new OfflineCompletionWorkFile();
                    item.setWorkID(c.getString(c.getColumnIndex("actionID")));
                    item.setFile_name(c.getString(c.getColumnIndex("file_name")));
                    item.setFile_path(c.getString(c.getColumnIndex("file_path")));
                    item.setFile_identifier(c.getString(c.getColumnIndex("file_identifier")));
                    item.setResultID(c.getString(c.getColumnIndex("resultID")));
                    item.setName(c.getString(c.getColumnIndex("name")));
                    item.setFromDevice(c.getString(c.getColumnIndex("from_device")));
                    result.add(item);
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }
}
