package kz.arta.synergy.workflow.mobile.android.common.offline.get;

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.comments.models.CommentItem;
import kz.arta.synergy.workflow.mobile.android.common.models.User;
import kz.arta.synergy.workflow.mobile.android.common.requests.RestClient;
import kz.arta.synergy.workflow.mobile.android.works.models.WorkItem;

/**
 * Created by neox on 04.07.14.
 */
public class WorkCommentsTable extends SQLiteOpenHelper {

    private SQLiteDatabase db = getWritableDatabase();
    public static final String TABLE_NAME = "Comments";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "actionID text,"
            + "comment text,"
            + "userID text,"
            + "name text,"
            + "created text,"
            + "commentID,"
            + "created_label text,"
            + "type text,"
            + "is_editable text,"
            + "is_deletable text,"
            + "comment_icon_type text"
            + ");";

    public WorkCommentsTable(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 insertComment(String actionID, String author, String comment, String time, String icon) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("actionID", actionID);
            cv.put("comment", comment);
            cv.put("userID", "");
            cv.put("name", author);
            cv.put("created", "");
            cv.put("created_label", time);
            cv.put("commentID", "");
            cv.put("type", "WORK");
            cv.put("is_editable", "false");
            cv.put("is_deletable", "false");
            cv.put("comment_icon_type", icon);
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public void add(String commentStr, String type, String actionID) throws Exception {
        ContentValues cv = new ContentValues();
        CommentItem[] allCommentItems = RestClient.getGson().fromJson(commentStr, CommentItem[].class);
        if (allCommentItems != null && allCommentItems.length != 0) {
            for (CommentItem itemAllComment : allCommentItems) {
                cv.put("actionID", actionID);
                cv.put("comment", itemAllComment.getComment());
                cv.put("userID", itemAllComment.getAuthor().getUserID());
                cv.put("name", itemAllComment.getAuthor().getName());
                cv.put("created", itemAllComment.getCreated());
                cv.put("created_label", itemAllComment.getCreated_label());
                cv.put("commentID", itemAllComment.getCommentID());
                cv.put("type", type);
                cv.put("is_editable", itemAllComment.getIs_editable());
                cv.put("is_deletable", itemAllComment.getIs_deletable());
                cv.put("comment_icon_type", itemAllComment.getComment_icon_type());
                db.insert(TABLE_NAME, null, cv);
            }
        }
    }

    public List<CommentItem> getByWorkIDAndType(String workID, String type) {
        List<CommentItem> 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 {
                    result.add(getCommentItem(c));
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    public List<CommentItem> getByWorkID(String workID) {
        List<CommentItem> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME + " where actionID = ?", new String[]{workID});
            if (c.moveToFirst()) {
                do {
                    result.add(getCommentItem(c));
                } while (c.moveToNext());
            }
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return result;
    }

    private CommentItem getCommentItem(Cursor c) {
        User author = new User();
        author.setName(c.getString(c.getColumnIndex("name")));
        author.setUserID(c.getString(c.getColumnIndex("userID")));
        CommentItem item = new CommentItem();
        item.setType(c.getString(c.getColumnIndex("type")));
        item.setActionID(c.getString(c.getColumnIndex("actionID")));
        item.setComment(c.getString(c.getColumnIndex("comment")));
        item.setCreated(c.getString(c.getColumnIndex("created")));
        item.setCommentID(c.getString(c.getColumnIndex("commentID")));
        item.setCreated_label(c.getString(c.getColumnIndex("created_label")));
        item.setIs_editable(c.getString(c.getColumnIndex("is_editable")));
        item.setIs_deletable(c.getString(c.getColumnIndex("is_deletable")));
        item.setComment_icon_type(c.getString(c.getColumnIndex("comment_icon_type")));
        item.setAuthor(author);
        return item;
    }

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

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

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

    public void removeWorksByArray(List<WorkItem> works) {
        try {
            for (WorkItem work : works) {
                db.delete(TABLE_NAME, "actionID = " + "\"" + work.getActionID() + "\"", null);
            }
        } catch (Exception exc) {
            // handling error ...
        }
    }

    public boolean removeCommentByIcon(String actionID) {
        try {
            db.delete(TABLE_NAME, "comment_icon_type = \"comment_icon_result\" AND actionID = " + "\"" + actionID + "\"", null);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }
}
