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

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

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "AddAttachmentTable";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "filepath text,"
            + "data text,"
            + "name text,"
            + "filename text"
            + ");";

    public AddAttachment(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 removeAttachmentByDate(String date) {
        try {
            db.delete(TABLE_NAME, "data = ?", new String[]{date});
            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 add(String actionID, String filepath, String date, String name, String filename) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("actionID", actionID);
            cv.put("filepath", filepath);
            cv.put("data", date);
            cv.put("name", name);
            cv.put("filename", filename);
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public List<OfflineAttachment> get() {
        List<OfflineAttachment> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.query(TABLE_NAME, null, null, null, null, null, null);
            if (c.moveToFirst()) {
                do {
                    OfflineAttachment itemOfflineAttachment = new OfflineAttachment();
                    itemOfflineAttachment.setActionID(c.getString(c.getColumnIndex("actionID")));
                    itemOfflineAttachment.setFilepath(c.getString(c.getColumnIndex("filepath")));
                    itemOfflineAttachment.setDate(c.getString(c.getColumnIndex("data")));
                    itemOfflineAttachment.setName(c.getString(c.getColumnIndex("name")));
                    itemOfflineAttachment.setFilename(c.getString(c.getColumnIndex("filename")));
                    result.add(itemOfflineAttachment);
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    public List<OfflineAttachment> getByWorkID(String workID) {
        List<OfflineAttachment> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME + " where actionID = ?", new String[]{workID});
            if (c.moveToFirst()) {
                do {
                    OfflineAttachment itemOfflineAttachment = new OfflineAttachment();
                    itemOfflineAttachment.setActionID(c.getString(c.getColumnIndex("actionID")));
                    itemOfflineAttachment.setFilepath(c.getString(c.getColumnIndex("filepath")));
                    itemOfflineAttachment.setDate(c.getString(c.getColumnIndex("data")));
                    itemOfflineAttachment.setName(c.getString(c.getColumnIndex("name")));
                    itemOfflineAttachment.setFilename(c.getString(c.getColumnIndex("filename")));
                    result.add(itemOfflineAttachment);
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }
}
