package kz.arta.synergy.workflow.mobile.android.documents.offline;

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

import kz.arta.synergy.workflow.mobile.android.attachments.models.AllAttachmentsItem;
import kz.arta.synergy.workflow.mobile.android.attachments.models.AttachmentItem;
import kz.arta.synergy.workflow.mobile.android.common.requests.RestClient;

/**
 * Created by neox on 23.08.16.
 * Таблица со списком вложений для документа
 */
public class DocumentAttachmentsTable extends SQLiteOpenHelper {

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "DocumentAttachmentsTable";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "documentID text,"
            + "json text"
            + ");";

    public DocumentAttachmentsTable(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(String uuid) {
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME + " where json LIKE '%" + uuid + "%'", null);
            if (c.moveToFirst()) {
                AllAttachmentsItem allAttachmentsItem = RestClient.getGson().fromJson(c.getString(c.getColumnIndex("json")), AllAttachmentsItem.class);
                Iterator<AttachmentItem> itemIterator = allAttachmentsItem.getAttachments().iterator();
                while (itemIterator.hasNext()) {
                    AttachmentItem item = itemIterator.next();
                    if (item.getUuid().equals(uuid)) {
                        itemIterator.remove();
                    }
                }
                itemIterator = allAttachmentsItem.getWork_files().iterator();
                while (itemIterator.hasNext()) {
                    AttachmentItem item = itemIterator.next();
                    if (item.getUuid().equals(uuid)) {
                        itemIterator.remove();
                    }
                }
                ContentValues cv = new ContentValues();
                cv.put("json", RestClient.getGson().toJson(allAttachmentsItem));
                db.update(TABLE_NAME, cv, "json LIKE '%" + uuid + "%'", null);
            }
            return true;
        } catch (Exception exc) {
            return false;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    public boolean add(String documentID, String json) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("documentID", documentID);
            cv.put("json", json);
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public String get(String documentID) {
        String result = null;
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME + " where documentID = ?", new String[]{documentID});
            if (c.moveToFirst()) {
                result = c.getString(c.getColumnIndex("json"));
            }
            c.close();
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

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

    }

    public boolean removeByDocumentID(String documentID) {
        try {
            db.delete(TABLE_NAME, "documentID = " + "\"" + documentID + "\"", null);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }
}