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

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

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

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

    // 插入單一 Product
    public void insertOne(String data) {
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.settinglogs.STATUS.getName(), data);
        long index = database.insert(DatabaseHelper.settinglogs.TABLE.getName(), null, values);
        logs.info("插入第"+index+"資料:"+values.toString());
    }

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

    // 獲取所有 Products
    public List<String> getAll() {
        List<String> list = new ArrayList<>();
        Cursor cursor = database.query(
                DatabaseHelper.settinglogs.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 status = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.settinglogs.STATUS.getName()));
                list.add(status);
            }
            cursor.close();
        }
        Log.i("SettinglogsDaoGetAll", "count:"+list.size());
        return list;
    }

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

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

