package kz.arta.synergy.workflow.mobile.android.files.offline.actions;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import org.json.JSONArray;
import org.json.JSONObject;

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

import kz.arta.synergy.workflow.mobile.android.files.models.Node;

/**
 * Created by neox on 31.08.16.
 */
public class FilesAddFileTable extends SQLiteOpenHelper {

    private SQLiteDatabase db = getWritableDatabase();
    private static final String TABLE_NAME = "FilesAddFileTable";
    private static final String TABLE = "create table " + TABLE_NAME + " ("
            + "nodeID text,"
            + "fileName text,"
            + "userName text,"
            + "date text,"
            + "filePath text,"
            + "folderName text"
            + ");";

    public FilesAddFileTable(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 nodeID, String fileName, String userName, String filePath, String folderName) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("nodeID", nodeID);
            cv.put("fileName", fileName);
            cv.put("userName", userName);
            cv.put("filePath", filePath);
            cv.put("folderName", folderName);
            cv.put("date", String.valueOf(System.currentTimeMillis()));
            db.insert(TABLE_NAME, null, cv);
            return true;
        } catch (Exception exc) {
            return false;
        }
    }

    public List<Node> get(String key) {
        List<Node> result = new ArrayList<>();
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + TABLE_NAME + " where nodeID = ?", new String[]{key});
            if (c.moveToFirst()) {
                do {
                    Node node = new Node();
                    node.setIs_folder(false);
                    node.setName(c.getString(c.getColumnIndex("fileName")));
                    node.setUserName(c.getString(c.getColumnIndex("userName")));
                    node.setModified(c.getString(c.getColumnIndex("date")));
                    node.setRight(Integer.MAX_VALUE);
                    node.setAi_type("");
                    result.add(node);
                } while (c.moveToNext());
            }
            return result;
        } catch (Exception exc) {
            return result;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    public JSONArray get() {
        JSONArray jsonArray = new JSONArray();
        Cursor c = null;
        try {
            c = db.query(TABLE_NAME, null, null, null, null, null, null);
            if (c.moveToFirst()) {
                do {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("nodeID", c.getString(c.getColumnIndex("nodeID")));
                    jsonObject.put("fileName", c.getString(c.getColumnIndex("fileName")));
                    jsonObject.put("userName", c.getString(c.getColumnIndex("userName")));
                    jsonObject.put("filePath", c.getString(c.getColumnIndex("filePath")));
                    jsonObject.put("folderName", c.getString(c.getColumnIndex("folderName")));
                    jsonObject.put("date", c.getString(c.getColumnIndex("date")));
                    jsonArray.put(jsonObject);
                } while (c.moveToNext());
            }
            return jsonArray;
        } catch (Exception exc) {
            return jsonArray;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

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

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