package com.unibuy.smartdevice.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.structure.SettingStructure;

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

public class SettingsDao {
    private Logs logs;
    private final SQLiteDatabase database;

    public SettingsDao(Context context) {
        logs = new Logs(getClass());
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    public void insertOne(SettingStructure data) {
        logs.debug("insertOne data:" + data.toString());

        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.settings.SETTING_ID.getName(), data.getSettingId());
        if (data.getDataCount() >= 1 && data.getData(0) != null && !data.getData(0).isEmpty()) values.put(DatabaseHelper.settings.DATA1.getName(), data.getData(0));
        if (data.getDataCount() >= 2 && data.getData(1) != null && !data.getData(1).isEmpty()) values.put(DatabaseHelper.settings.DATA2.getName(), data.getData(1));
        if (data.getDataCount() >= 3 && data.getData(2) != null && !data.getData(2).isEmpty()) values.put(DatabaseHelper.settings.DATA3.getName(), data.getData(2));
        if (data.getDataCount() >= 4 && data.getData(3) != null && !data.getData(3).isEmpty()) values.put(DatabaseHelper.settings.DATA4.getName(), data.getData(3));
        if (data.getDataCount() >= 5 && data.getData(4) != null && !data.getData(4).isEmpty()) values.put(DatabaseHelper.settings.DATA5.getName(), data.getData(4));
        if (data.getDataCount() >= 6 && data.getData(5) != null && !data.getData(5).isEmpty()) values.put(DatabaseHelper.settings.DATA6.getName(), data.getData(5));
        if (data.getDataCount() >= 7 && data.getData(6) != null && !data.getData(6).isEmpty()) values.put(DatabaseHelper.settings.DATA7.getName(), data.getData(6));
        if (data.getDataCount() >= 8 && data.getData(7) != null && !data.getData(7).isEmpty()) values.put(DatabaseHelper.settings.DATA8.getName(), data.getData(7));
        if (data.getDataCount() >= 9 && data.getData(8) != null && !data.getData(8).isEmpty()) values.put(DatabaseHelper.settings.DATA9.getName(), data.getData(8));
        if (data.getDataCount() >= 10 && data.getData(9) != null && !data.getData(9).isEmpty()) values.put(DatabaseHelper.settings.DATA10.getName(), data.getData(9));
        long index = database.insert(DatabaseHelper.settings.TABLE.getName(), null, values);
        Log.i("database insert", "插入第"+index+"資料:"+values.toString());
    }

    // 批量插入 Products
    public void insertAll(List<SettingStructure> list) {
        database.beginTransaction();
        try {
            for (SettingStructure data : list) {
                insertOne(data);
            }
            database.setTransactionSuccessful();
        } finally {
            database.endTransaction();
        }
    }

    // 獲取所有 Products
    public List<SettingStructure> getAll() {
        List<SettingStructure> list = new ArrayList<>();
        Cursor cursor = database.query(
                DatabaseHelper.settings.TABLE.getName(),
                null, // 全部欄位
                null, // 無條件
                null,
                null,
                null,
                null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.column.COLUMN_ID.getName()));
                String settingId = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.SETTING_ID.getName()));
                String data1 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA1.getName()));
                String data2 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA2.getName()));
                String data3 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA3.getName()));
                String data4 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA4.getName()));
                String data5 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA5.getName()));
                String data6 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA6.getName()));
                String data7 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA7.getName()));
                String data8 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA8.getName()));
                String data9 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA9.getName()));
                String data10 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA10.getName()));
                list.add(new SettingStructure(settingId, new String[]{data1, data2, data3, data4, data5, data6, data7, data8, data9, data10}));
            }
            cursor.close();
        }
        Log.i("SettingsDaoGetAll", "count:"+list.size());
        return list;
    }

    public List<SettingStructure> getWhere(String whereSettingId) {
        List<SettingStructure> list = new ArrayList<>();
        Cursor cursor = database.query(
                DatabaseHelper.settings.TABLE.getName(),
                null, // 全部欄位
                DatabaseHelper.settings.SETTING_ID.getName()+"=?", // 無條件
                new String[] {whereSettingId},
                null,
                null,
                null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.column.COLUMN_ID.getName()));
                String settingId = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.SETTING_ID.getName()));
                String data1 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA1.getName()));
                String data2 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA2.getName()));
                String data3 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA3.getName()));
                String data4 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA4.getName()));
                String data5 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA5.getName()));
                String data6 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA6.getName()));
                String data7 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA7.getName()));
                String data8 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA8.getName()));
                String data9 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA9.getName()));
                String data10 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settings.DATA10.getName()));
                list.add(new SettingStructure(settingId, new String[]{data1, data2, data3, data4, data5, data6, data7, data8, data9, data10}));
            }
            cursor.close();
        }
        Log.i("SettingsDaoGetWhere", "count:"+list.size());
        return list;
    }

    public int getCount(String whereSettingId) {
        List<SettingStructure> list = getWhere(whereSettingId);
        if (list.isEmpty()) {
            return 0;
        }
        return list.size();
    }

    public SettingStructure getOne(String whereSettingId) {
        List<SettingStructure> list = getWhere(whereSettingId);
        if (list.isEmpty()) {
            return new SettingStructure(whereSettingId, new String[]{});
        }
        return list.get(0);
    }

    public void deleteWhere(String whereSettingId) {
        database.delete(DatabaseHelper.settings.TABLE.getName(), DatabaseHelper.settings.SETTING_ID.getName()+"=?", new String[] {whereSettingId});
    }

    // 刪除所有 Products
    public void deleteAll() {
        database.delete(DatabaseHelper.settings.TABLE.getName(), null, null);
    }

    // 獲取機台鎖定狀態
    public boolean isMachineLocked() {
        SettingStructure setting = getOne("machine_lock");
        if (setting.getDataCount() >= 1) {
            return "true".equals(setting.getData(0));
        }
        return false;
    }

    // 設定機台鎖定狀態
    public void setMachineLocked(boolean locked) {
        deleteWhere("machine_lock");
        insertOne(new SettingStructure("machine_lock", new String[]{String.valueOf(locked)}));
    }

    // 關閉資料庫
    public void close() {
        database.close();
    }
}

