package com.unibuy.smartdevice.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.unibuy.smartdevice.AppEntry;
import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.devices.SlotField;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.external.mqtt.MqttManager;
import com.unibuy.smartdevice.structure.BuyStructure;
import com.unibuy.smartdevice.structure.ReportFlowInfoStructure;
import com.unibuy.smartdevice.structure.SlotStructure;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.structure.mqtt.TransactionFinalizePayload;
import com.unibuy.smartdevice.tools.TransactionFinalizeBuilder;
import com.unibuy.smartdevice.ui.tools.ImageGlide;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

/**
 * Foreground Service，負責持有 WakeLock 並管理 MQTT 連線。
 * 確保販賣機台在關閉螢幕時也能維持通訊。
 * 斷線偵測統一由 MQTT LWT（遺囑）機制處理，不另設其他偵測邏輯。
 */
public class MqttService extends Service {
    private static final String TAG = "MqttService";
    private static final String CHANNEL_ID = "MqttServiceChannel";
    private PowerManager.WakeLock wakeLock;
    private com.unibuy.smartdevice.exception.Logs logs;

    @Override
    public void onCreate() {
        super.onCreate();
        logs = new com.unibuy.smartdevice.exception.Logs(getClass());
        logs.info("MqttService Created");

        // 1. 建立 Notification Channel
        createNotificationChannel();

        // 2. 啟動為前台服務（確保不被系統殺掉）
        Intent notificationIntent = new Intent(this, AppEntry.initialActivity());
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("StarCloud MQTT Service")
                .setContentText("機台通訊監控中...")
                .setSmallIcon(R.drawable.cpu)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        // 3. 取得 WakeLock（確保關螢幕時 CPU 不休眠，心跳不中斷）
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (powerManager != null) {
            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "StarCloud:MqttWakeLock");
            wakeLock.acquire();
            logs.info("WakeLock acquired");
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String machineId = intent != null ? intent.getStringExtra("machineId") : null;
        String apiToken = intent != null ? intent.getStringExtra("apiToken") : null;

        logs.info("MqttService onStartCommand with machineId: " + machineId);

        if (machineId != null && apiToken != null) {
            MqttManager mqttManager = MqttManager.getInstance();
            mqttManager.setCommandListener(command -> {
                String cmd = command.getCommand();
                String cmdId = command.getCommand_id();
                logs.info("Received MQTT command: " + cmd + " (ID: " + cmdId + ")");

                try {
                    if ("reboot".equals(cmd)) {
                        logs.info("Executing remote reboot command...");

                        long idle = MyApp.getInstance().getIdleSeconds();
                        String busyReason = MyApp.getInstance().getBusyReason();

                        // 🚩 若機台忙碌（對話框開著、購物車有東西、NFC 交易中）則拒絕
                        if (!busyReason.isEmpty()) {
                            logs.info("Remote reboot rejected: " + busyReason);
                            mqttManager.publishCommandAck(cmdId, "failed", "Machine busy: " + busyReason);
                            return;
                        }

                        // 🚩 若不忙碌但閒置時間不足，僅記錄警告但允許重啟（遠端控制優先）
                        if (idle < 60) {
                            logs.info("Remote reboot accepted despite low idle (" + idle + "s), as machine is not busy.");
                        }

                        mqttManager.publishCommandAck(cmdId, "success", "Rebooting soon...");
                        // 🚩 優雅重啟：ACK 發出後先 gracefulDisconnect（不觸發 LWT），再 killProcess
                        new android.os.Handler(android.os.Looper.getMainLooper())
                                .postDelayed(() -> mqttManager.gracefulDisconnect(() -> {
                                    new android.os.Handler(android.os.Looper.getMainLooper()).post(() -> {
                                        Intent restartIntent = new Intent(getApplicationContext(),
                                                AppEntry.initialActivity());
                                        restartIntent.addFlags(
                                                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        startActivity(restartIntent);
                                        android.os.Process.killProcess(android.os.Process.myPid());
                                        System.exit(0);
                                    });
                                }), 500); // 0.5 秒確保 ACK 先發出
                    } else if ("lock".equals(cmd)) {
                        logs.info("Executing remote lock command...");
                        com.unibuy.smartdevice.database.SettingsDao settingsDao = new com.unibuy.smartdevice.database.SettingsDao(
                                getApplicationContext());
                        settingsDao.setMachineLocked(true);
                        settingsDao.close();

                        mqttManager.publishCommandAck(cmdId, "success", "Machine locked");

                        new android.os.Handler(android.os.Looper.getMainLooper()).post(() -> {
                            Intent lockIntent = new Intent(getApplicationContext(),
                                    com.unibuy.smartdevice.ui.MaintenanceActivity.class);
                            lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(lockIntent);
                        });
                    } else if ("unlock".equals(cmd)) {
                        logs.info("Executing remote unlock command...");

                        String busyReason = MyApp.getInstance().getBusyReason();

                        if (!busyReason.isEmpty()) {
                            logs.info("Remote unlock rejected: " + busyReason);
                            mqttManager.publishCommandAck(cmdId, "failed", "Machine busy: " + busyReason);
                            return;
                        }

                        com.unibuy.smartdevice.database.SettingsDao settingsDao = new com.unibuy.smartdevice.database.SettingsDao(
                                getApplicationContext());
                        settingsDao.setMachineLocked(false);
                        settingsDao.close();

                        mqttManager.publishCommandAck(cmdId, "success", "Machine unlocked, restarting...");

                        // 🚩 優雅重啟：先 gracefulDisconnect（不觸發 LWT），再 killProcess
                        // 解鎖後重啟到目前版本的初始畫面
                        new android.os.Handler(android.os.Looper.getMainLooper())
                                .postDelayed(() -> mqttManager.gracefulDisconnect(() -> {
                                    new android.os.Handler(android.os.Looper.getMainLooper()).post(() -> {
                                        Intent restartIntent = new Intent(getApplicationContext(),
                                                AppEntry.initialActivity());
                                        restartIntent.addFlags(
                                                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        startActivity(restartIntent);
                                        android.os.Process.killProcess(android.os.Process.myPid());
                                        System.exit(0);
                                    });
                                }), 500);
                    } else if ("nfc_reset".equals(cmd)) {
                        logs.info("Executing remote NFC reset command...");
                        try {
                            com.unibuy.smartdevice.devices.Rs232Dev comPort2 = new com.unibuy.smartdevice.devices.Rs232Dev(
                                    com.unibuy.smartdevice.MyApp.getInstance().getComPort2ByNFCPay());
                            com.unibuy.smartdevice.devices.DevNFCPay devNFCPay = new com.unibuy.smartdevice.devices.DevNFCPay(
                                    getApplicationContext());
                            comPort2.send(devNFCPay.reset());
                            mqttManager.publishCommandAck(cmdId, "success", "NFC reset command sent");
                        } catch (Exception e) {
                            logs.info("NFC reset failed: " + e.getMessage());
                            mqttManager.publishCommandAck(cmdId, "failed", "NFC reset failed: " + e.getMessage());
                        }
                    } else if ("nfc_checkout".equals(cmd)) {
                        logs.info("Executing remote NFC checkout command...");
                        try {
                            com.unibuy.smartdevice.devices.Rs232Dev comPort2 = new com.unibuy.smartdevice.devices.Rs232Dev(
                                    com.unibuy.smartdevice.MyApp.getInstance().getComPort2ByNFCPay());
                            com.unibuy.smartdevice.devices.DevNFCPay devNFCPay = new com.unibuy.smartdevice.devices.DevNFCPay(
                                    getApplicationContext());
                            comPort2.send(devNFCPay.checkout());
                            mqttManager.publishCommandAck(cmdId, "success", "NFC checkout command sent");
                        } catch (Exception e) {
                            logs.info("NFC checkout failed: " + e.getMessage());
                            mqttManager.publishCommandAck(cmdId, "failed", "NFC checkout failed: " + e.getMessage());
                        }
                    } else if ("change".equals(cmd)) {
                        logs.info("Executing remote change command...");
                        try {
                            int amount = 0;
                            if (command.getPayload() != null && command.getPayload().isJsonObject()) {
                                com.google.gson.JsonObject payloadObj = command.getPayload().getAsJsonObject();
                                if (payloadObj.has("amount")) {
                                    amount = payloadObj.get("amount").getAsInt();
                                }
                            }

                            if (amount <= 0) {
                                throw new Exception("Invalid or missing amount: " + amount);
                            }

                            int finalAmount = amount;
                            com.unibuy.smartdevice.tools.CashPayoutManager payoutManager = new com.unibuy.smartdevice.tools.CashPayoutManager(
                                    getApplicationContext(),
                                    new com.unibuy.smartdevice.tools.CashPayoutManager.PayoutListener() {
                                        @Override
                                        public void onProgress(String message) {
                                            logs.debug("Payout Progress: " + message);
                                        }

                                        @Override
                                        public void onSuccess() {
                                            logs.info("Remote payout success for amount: " + finalAmount);
                                            mqttManager.publishCommandAck(cmdId, "success",
                                                    "Payout completed: " + finalAmount);
                                        }

                                        @Override
                                        public void onFailure(String error) {
                                            logs.info("Remote payout failed: " + error);
                                            mqttManager.publishCommandAck(cmdId, "failed", "Payout failed: " + error);
                                        }
                                    });

                            payoutManager.startPayout(amount);
                            // 注意：ACK 會在 PayoutListener 的回調中發送，不在此處發送。
                        } catch (Exception e) {
                            logs.info("Failed to parse or start change command: " + e.getMessage());
                            mqttManager.publishCommandAck(cmdId, "failed", "Error: " + e.getMessage());
                        }
                    } else if ("update_ads".equals(cmd)) {
                        logs.info("Executing remote advertisement sync command...");

                        // 📢 在主執行緒顯示 Toast 提示使用者
                        new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
                                android.widget.Toast.makeText(getApplicationContext(),
                                        "📢 廣告更新中...", android.widget.Toast.LENGTH_SHORT).show());

                        com.unibuy.smartdevice.tools.HandlerMain adHandler = new com.unibuy.smartdevice.tools.HandlerMain(
                                getApplicationContext(), logs) {
                            @Override
                            protected void execute(Context context, int code, String message) {
                                logs.info("Ad sync event: [" + code + "] " + message);
                                if (code == 200 && "廣告同步完成".equals(message)) {
                                    mqttManager.publishCommandAck(cmdId, "success",
                                            "Advertisement sync completed successfully");

                                    // ✅ Toast 提示完成
                                    new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
                                            android.widget.Toast.makeText(context,
                                                    "✅ 廣告更新完成", android.widget.Toast.LENGTH_SHORT).show());
                                    // 注意：廣播 ACTION_AD_UPDATED 已在 StarCloudAPI.saveAds() 中統一發送
                                } else if (code >= 400) {
                                    mqttManager.publishCommandAck(cmdId, "failed",
                                            "Advertisement sync failed: " + message);

                                    new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
                                            android.widget.Toast.makeText(context,
                                                    "❌ 廣告更新失敗: " + message, android.widget.Toast.LENGTH_LONG).show());
                                }
                            }
                        };
                        // 使用新架構 StarCloudAPI 執行同步
                        new com.unibuy.smartdevice.external.StarCloudAPI(adHandler, adHandler).downloadAdvertisement();
                    } else if ("update_products".equals(cmd)) {
                        logs.info("Executing remote product sync command...");

                        // 📢 在主執行緒顯示 Toast 提示使用者
                        new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
                                android.widget.Toast.makeText(getApplicationContext(),
                                        "📢 商品更新中...", android.widget.Toast.LENGTH_SHORT).show());

                        com.unibuy.smartdevice.tools.HandlerMain productHandler = new com.unibuy.smartdevice.tools.HandlerMain(
                                getApplicationContext(), logs) {
                            @Override
                            protected void execute(Context context, int code, String message) {
                                logs.info("Product sync event: [" + code + "] " + message);
                                if (code == 200 && "商品下載成功".equals(message)) {
                                    // 背景預載圖片
                                    new Thread(() -> {
                                        try {
                                            List<ProductStructure> products = MyApp.getInstance().getProductList();
                                            int total = products.size();
                                            logs.info("Starting image preloading for " + total + " products...");

                                            for (int i = 0; i < total; i++) {
                                                ProductStructure p = products.get(i);
                                                if (p.getProductImg() != null && !p.getProductImg().isEmpty()) {
                                                    try {
                                                        new ImageGlide(getApplicationContext()).preload(p.getProductImg());
                                                    } catch (Exception e) {
                                                        logs.info("Failed to preload image for product " + p.getProductID() + ": " + e.getMessage());
                                                    }
                                                }
                                            }

                                            logs.info("Product sync and image preloading completed.");
                                            mqttManager.publishCommandAck(cmdId, "success", "Product sync completed successfully");

                                            new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
                                                    android.widget.Toast.makeText(context,
                                                            "✅ 商品更新完成", android.widget.Toast.LENGTH_SHORT).show());
                                        } catch (Exception e) {
                                            logs.error(com.unibuy.smartdevice.exception.ErrorCode.UNKNOWN_ERROR, "Error during image preloading: " + e.getMessage());
                                            mqttManager.publishCommandAck(cmdId, "failed", "Image preloading error: " + e.getMessage());
                                        }
                                    }).start();
                                } else if (code >= 400) {
                                    mqttManager.publishCommandAck(cmdId, "failed", "Product sync failed: " + message);
                                    new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
                                            android.widget.Toast.makeText(context,
                                                    "❌ 商品更新失敗: " + message, android.widget.Toast.LENGTH_LONG).show());
                                }
                            }
                        };

                        // 1. 清除舊圖片快取
                        ImageGlide.clearProductCache(getApplicationContext());
                        // 2. 開始下載資料
                        new com.unibuy.smartdevice.external.StarCloudAPI(productHandler, productHandler).downloadProductInfo();
                    } else if ("update_inventory".equals(cmd)) {
                        logs.info("Executing update_inventory command...");
                        try {
                            int slotNo = -1;
                            int stock = -1;
                            String expiryDate = null;
                            String batchNo = null;

                            if (command.getPayload() != null && command.getPayload().isJsonObject()) {
                                com.google.gson.JsonObject payloadObj = command.getPayload().getAsJsonObject();
                                if (payloadObj.has("slot_no"))
                                    slotNo = payloadObj.get("slot_no").getAsInt();
                                if (payloadObj.has("stock"))
                                    stock = payloadObj.get("stock").getAsInt();
                                if (payloadObj.has("expiry_date") && !payloadObj.get("expiry_date").isJsonNull())
                                    expiryDate = payloadObj.get("expiry_date").getAsString();
                                if (payloadObj.has("batch_no") && !payloadObj.get("batch_no").isJsonNull())
                                    batchNo = payloadObj.get("batch_no").getAsString();
                            }

                            if (slotNo <= 0 || stock < 0) {
                                throw new Exception("Invalid parameters: slot_no=" + slotNo + ", stock=" + stock);
                            }

                            // 更新本地資料庫
                            int field = com.unibuy.smartdevice.devices.SlotField.VMC.getField();
                            com.unibuy.smartdevice.structure.SlotStructure slotData = MyApp.getInstance()
                                    .getSlotData(field, slotNo);
                            if (slotData != null) {
                                slotData.setCount(stock);

                                // 更新選填欄位至 otherData
                                org.json.JSONObject otherData = slotData.getOtherData();
                                if (otherData == null)
                                    otherData = new org.json.JSONObject();
                                try {
                                    if (expiryDate != null)
                                        otherData.put("expiry_date", expiryDate);
                                    if (batchNo != null)
                                        otherData.put("batch_no", batchNo);
                                } catch (org.json.JSONException e) {
                                    logs.info("Failed to update JSON otherData: " + e.getMessage());
                                }
                                slotData.setOtherData(otherData);

                                com.unibuy.smartdevice.database.SlotsDao slotsDao = new com.unibuy.smartdevice.database.SlotsDao(
                                        getApplicationContext());
                                slotsDao.deleteAll();
                                slotsDao.insertAll(MyApp.getInstance().getSlotMap());
                                slotsDao.close();

                                logs.info("Inventory updated: slot=" + slotNo + ", stock=" + stock);
                                mqttManager
                                        .publishCommandAck(new com.unibuy.smartdevice.structure.mqtt.CommandAckPayload(
                                                cmdId, "success", "Inventory updated successfully",
                                                String.valueOf(stock)));

                                // ✅ 通知前台銷售平台刷新商品列表（輕量級，不重建 Fragment）
                                Intent refreshIntent = new Intent("com.unibuy.smartdevice.ACTION_SLOT_UPDATED");
                                refreshIntent.setPackage(getPackageName());
                                sendBroadcast(refreshIntent);
                            } else {
                                throw new Exception("Slot not found: " + slotNo);
                            }
                        } catch (Exception e) {
                            logs.info("Failed to update inventory: " + e.getMessage());
                            mqttManager.publishCommandAck(cmdId, "failed",
                                    "Update inventory failed: " + e.getMessage());
                        }
                    } else if ("dispense".equals(cmd)) {
                        logs.info("Executing remote dispense command...");
                        try {
                            int slot = -1;
                            if (command.getPayload() != null && command.getPayload().isJsonObject()) {
                                com.google.gson.JsonObject payloadObj = command.getPayload().getAsJsonObject();
                                if (payloadObj.has("slot_no")) {
                                    slot = payloadObj.get("slot_no").getAsInt();
                                } else if (payloadObj.has("slot")) {
                                    slot = payloadObj.get("slot").getAsInt();
                                }
                            }

                            if (slot <= 0) {
                                throw new Exception("Invalid or missing slot_no: " + slot);
                            }

                            int finalSlot = slot;
                            com.unibuy.smartdevice.tools.ProductDispenseManager dispenseManager = new com.unibuy.smartdevice.tools.ProductDispenseManager(
                                    getApplicationContext(),
                                    logs,
                                    new com.unibuy.smartdevice.tools.ProductDispenseManager.DispenseListener() {
                                        @Override
                                        public void onProgress(String message) {
                                            logs.debug("Dispense Progress: " + message);
                                        }

                                        @Override
                                        public void onSuccess() {
                                            if (logs != null) logs.info("Remote dispense success for slot: " + finalSlot + " (cmdId: " + cmdId + ")");
                                            // 依據使用者測試結果：回傳簡潔的 ACK (僅含 command_id 與 result) 即可成功更改狀態
                                            mqttManager.publishCommandAck(new com.unibuy.smartdevice.structure.mqtt.CommandAckPayload(cmdId, "success"));
                                        }

                                        @Override
                                        public void onFailure(String error) {
                                            if (logs != null) logs.info("Remote dispense failed: " + error);
                                            mqttManager.publishCommandAck(new com.unibuy.smartdevice.structure.mqtt.CommandAckPayload(cmdId, "failed"));
                                        }
                                    });

                            dispenseManager.startDispense(slot);
                        } catch (Exception e) {
                            logs.info("Failed to parse or start dispense command: " + e.getMessage());
                            mqttManager.publishCommandAck(cmdId, "failed", "Error: " + e.getMessage());
                        }
                    } else if ("update_app".equals(cmd)) {
                        logs.info("Executing remote app OTA update command...");
                        try {
                            String apkUrl = null;
                            if (command.getPayload() != null && command.getPayload().isJsonObject()) {
                                com.google.gson.JsonObject payloadObj = command.getPayload().getAsJsonObject();
                                if (payloadObj.has("url")) {
                                    apkUrl = payloadObj.get("url").getAsString();
                                }
                            }

                            if (apkUrl == null || apkUrl.isEmpty()) {
                                throw new Exception("Missing 'url' parameter in update_app payload.");
                            }

                            // 啟動背景 OTA 更新管理器
                            com.unibuy.smartdevice.tools.OtaManager.startUpdate(getApplicationContext(), apkUrl, cmdId);

                        } catch (Exception e) {
                            logs.info("Failed to start remote app update: " + e.getMessage());
                            mqttManager.publishCommandAck(cmdId, "failed", "Error: " + e.getMessage());
                        }
                    } else {
                        logs.info("Unknown MQTT command: " + cmd);
                        mqttManager.publishCommandAck(cmdId, "failed", "Unknown command");
                    }
                } catch (Exception e) {
                    logs.info("Error executing MQTT command: " + e.getMessage());
                    mqttManager.publishCommandAck(cmdId, "failed", "Error: " + e.getMessage());
                }
            });
            mqttManager.connect(machineId, apiToken);
        }

        // START_STICKY 確保 Service 被系統殺掉後會自動重啟
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        logs.info("MqttService Destroyed");
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
            logs.info("WakeLock released");
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "MQTT Service Channel",
                    NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager = getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(serviceChannel);
            }
        }
    }
}
