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

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

    private static final String TABLE_NAME = "AddCommentTable";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "author text,"
            + "comment text,"
            + "time text,"
            + "name text,"
            + "type text"
            + ");";

    private SQLiteDatabase db = getWritableDatabase();

    public AddComment(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 editCommentByData(String time, String comment, String name) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("comment", comment);
            cv.put("name", name);
            db.update(TABLE_NAME, cv, "time = ?", new String[]{time});
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public boolean removeCommentByData(String time) {
        try {
            db.delete(TABLE_NAME, "time = ?", new String[]{time});
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public boolean add(String actionID, String author, String time, String comment, String name, String type) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("actionID", actionID);
            cv.put("author", author);
            cv.put("comment", comment);
            cv.put("time", time);
            cv.put("name", name);
            cv.put("type", type);
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public List<OfflineComment> getByWorkIDAndType(String workID, String type) {
        List<OfflineComment> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME + " where actionID = ? and type = ?", new String[]{workID, type});
            if (c.moveToFirst()) {
                do {
                    OfflineComment offlineComment = new OfflineComment();
                    offlineComment.setActionID(c.getString(c.getColumnIndex("actionID")));
                    offlineComment.setAuthor(c.getString(c.getColumnIndex("author")));
                    offlineComment.setComment(c.getString(c.getColumnIndex("comment")));
                    offlineComment.setTime(c.getString(c.getColumnIndex("time")));
                    offlineComment.setName(c.getString(c.getColumnIndex("name")));
                    offlineComment.setType(c.getString(c.getColumnIndex("type")));
                    result.add(offlineComment);
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    public List<OfflineComment> get() {
        List<OfflineComment> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.query(TABLE_NAME, null, null, null, null, null, null);
            if (c.moveToFirst()) {
                do {
                    OfflineComment offlineComment = new OfflineComment();
                    offlineComment.setActionID(c.getString(c.getColumnIndex("actionID")));
                    offlineComment.setAuthor(c.getString(c.getColumnIndex("author")));
                    offlineComment.setComment(c.getString(c.getColumnIndex("comment")));
                    offlineComment.setTime(c.getString(c.getColumnIndex("time")));
                    offlineComment.setName(c.getString(c.getColumnIndex("name")));
                    offlineComment.setType(c.getString(c.getColumnIndex("type")));
                    result.add(offlineComment);
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }
}
