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

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

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

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

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

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

    private CommentItem getFromCursor(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.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 removeCommentByID(String commentID) {
        try {
            db.delete(TABLE_NAME, "commentID = " + "\"" + commentID + "\"", 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;
        }
    }
}