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 com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import kz.arta.synergy.workflow.mobile.android.common.requests.RestClient;
import kz.arta.synergy.workflow.mobile.android.documents.common.models.DocumentItem;

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

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "DocumentTable";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "key text,"
            + "value text"
            + ");";

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

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

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

    public List<String> getAll() {
        List<String> docs = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.query(TABLE_NAME, null, null, null, null, null, null);
            if (c.moveToFirst()) {
                do {
                    docs.add(c.getString(c.getColumnIndex("value")));
                } while (c.moveToNext());
            }
            return docs;
        } catch (Exception exc) {
            return docs;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    public String getDocument(String documentID) {
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME, new String[]{});
            if (c.moveToFirst()) {
                do {
                    String json = c.getString(c.getColumnIndex("value"));
                    List<DocumentItem> documents = RestClient.getGson().fromJson(json, new TypeToken<List<DocumentItem>>() {}.getType());
                    for (DocumentItem document : documents) {
                        if (document.getDocumentID().equals(documentID)) {
                            c.close();
                            return RestClient.getGson().toJson(document);
                        }
                    }
                } while (c.moveToNext());
            }
        } catch (Exception exc) {
            return null;
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }

    public boolean update(String key, String value) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("value", value);
            db.update(TABLE_NAME, cv, "key = ?", new String[]{key});
            return true;
        } catch (Exception exc) {
            return false;
        }
    }
}
