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

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

public class ProductsDao {
    private final SQLiteDatabase database;

    public ProductsDao(Context context) {
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    // 插入單一 Product
    public void insertOne(ProductStructure data) {
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.products.PRODUCT_ID.getName(), data.getProductID());
        values.put(DatabaseHelper.products.PRODUCT_IMG.getName(), data.getProductImg());
        values.put(DatabaseHelper.products.PRODUCT_NAME.getName(), data.getProductName());
        values.put(DatabaseHelper.products.MACHINE_PRICE.getName(), data.getMachinePrice());
        values.put(DatabaseHelper.products.SELLING_PRICE.getName(), data.getSellingPrice());
        values.put(DatabaseHelper.products.MEMBER_PRICE.getName(), data.getMemberPrice());
        values.put(DatabaseHelper.products.SLOT_UPPER_LIMIT.getName(), data.getSlotUpperLimit());
        values.put(DatabaseHelper.products.MARKETING_PLAN.getName(), data.getMarketingPlan());
        values.put(DatabaseHelper.products.MATERIAL_CODE.getName(), data.getMaterialCode());
        values.put(DatabaseHelper.products.PRODUCT_NAME_EN.getName(), data.getProductNameEn());
        values.put(DatabaseHelper.products.PRODUCT_NAME_JP.getName(), data.getProductNameJp());
        long index = database.insert(DatabaseHelper.products.TABLE.getName(), null, values);
        Log.i("database insert", "插入第"+index+"資料:"+values.toString());
    }

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

    // 獲取所有 Products
    public List<ProductStructure> getAll() {
        List<ProductStructure> list = new ArrayList<>();
        Cursor cursor = database.query(
                DatabaseHelper.products.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 productId = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.products.PRODUCT_ID.getName()));
                String productImg = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.products.PRODUCT_IMG.getName()));
                String productName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.products.PRODUCT_NAME.getName()));
                int machingPrice = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.products.MACHINE_PRICE.getName()));
                int sellingPrice = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.products.SELLING_PRICE.getName()));
                int memberPrice = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.products.MEMBER_PRICE.getName()));
                int slotUpperLimit = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.products.SLOT_UPPER_LIMIT.getName()));
                String marketingPlan = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.products.MARKETING_PLAN.getName()));;
                String materialCode = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.products.MATERIAL_CODE.getName()));;
                String productNameEn = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.products.PRODUCT_NAME_EN.getName()));
                String productNameJp = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.products.PRODUCT_NAME_JP.getName()));
                list.add(new ProductStructure(productId, productImg, productName, machingPrice,sellingPrice,memberPrice,slotUpperLimit,marketingPlan,materialCode,productNameEn,productNameJp));
            }
            cursor.close();
        }
        return list;
    }

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

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

