package com.unibuy.smartdevice.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import com.unibuy.smartdevice.DialogAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.databinding.DialogCheckoutTapPayBinding;
import com.unibuy.smartdevice.devices.DeviceType;
import com.unibuy.smartdevice.devices.SlotField;
import com.unibuy.smartdevice.devices.UsbDev;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.exception.LogsIOException;
import com.unibuy.smartdevice.exception.LogsParseException;
import com.unibuy.smartdevice.exception.LogsSettingEmptyException;
import com.unibuy.smartdevice.exception.LogsUnsupportedOperationException;
import com.unibuy.smartdevice.external.HttpAPI;
import com.unibuy.smartdevice.external.HttpTapPay;
import com.unibuy.smartdevice.structure.BuyStructure;
import com.unibuy.smartdevice.structure.InvoiceItemStructure;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.structure.ReportFreeGiftCodeStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainCountdown;
import com.unibuy.smartdevice.tools.HandlerMainScheduler;
import com.unibuy.smartdevice.tools.ToastHandlerMain;
import com.unibuy.smartdevice.tools.Tools;
import com.unibuy.smartdevice.ui.FontendActivity;

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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

public class CheckoutTapPayDialog extends DialogAbstract {
    public enum Option {
        TOAST(0),
        COUNTDOWN_CANCEL(1),
        SET_COUNTDOWN_TEXT(2),
        TRANSACTION_SUCCESSFUL(4),
        TRANSACTION_FAILED(5),
        TRANSACTION_AGAIN(12),
        CHANGE_DISPATCH(6),
        CHANGE_BUY(7),
        REPORT_FLOW(8),
        POST_INVOICE(9),
        REPORT_INVOICE(10),
        SET_STATUS_TEXT(11),
        ;

        private int option;

        public int getOption() {
            return option;
        }

        Option(int option) {
            this.option = option;
        }
    }

    public static final Map<Integer, Option> optionMap = new HashMap<>();
    static {
        for (Option option : Option.values()) {
            optionMap.put(option.getOption(), option);
        }
    }

    private String productNames;
    private int totalPrice;

    @Override
    protected Context setCtx() {
        return getContext();
    }

    @Override
    protected Class<? extends DialogAbstract> setCls() {
        return getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs()) {
            @Override
            protected void execute(Context context, int commandCode, String message) {
                if (MyApp.getInstance().getBuyList().size() <= 0) {

                    dialogCancel();
                } else {
                    Option option = optionMap.get(commandCode);
                    getLogs().info("Option " + option.getOption() + ":" + option.name());
                    switch (option) {
                        case TOAST:
                            super.execute(context, commandCode, message);
                            break;
                        case COUNTDOWN_CANCEL:
                            cancelOnThreadCountdown();
                            break;
                        case SET_COUNTDOWN_TEXT:
                            setButtonCancelText(Integer.valueOf(message));
                            break;
                        case TRANSACTION_SUCCESSFUL:
                            if (isTransaction) {
                                isTransaction = false; // 🚨 馬上關閉，避免送出第二筆
                                MyApp.getInstance().getReportFlowInfoData().setFlowStatusType(1);
                                try {
                                    httpAPI.reportFlowInfo(MyApp.getInstance().getReportFlowInfoData());
                                } catch (LogsSettingEmptyException | LogsParseException e) {
                                    getLogs().warning(e);
                                }
                            }
                            break;
                        case TRANSACTION_FAILED:
                            if (isTransaction) {
                                isTransaction = false; // 🚨 馬上關閉
                                MyApp.getInstance().getReportFlowInfoData().setFlowStatusType(0);
                                try {
                                    httpAPI.reportFlowInfo(MyApp.getInstance().getReportFlowInfoData());
                                } catch (LogsSettingEmptyException | LogsParseException e) {
                                    getLogs().warning(e);
                                }
                            }
                            break;
                        case TRANSACTION_AGAIN:
//                            getRecord();
                            scheduleAutoRetryGetRecord();
                            break;
                        case CHANGE_DISPATCH:
                            changeDispatch();
                            break;
                        case CHANGE_BUY:
                            changeBuy();
                            break;
                        case REPORT_FLOW:
                            getLogs().info(MyApp.getInstance().getReportFlowInfoData().toString());

//                            if (MyApp.getInstance().getReportFlowInfoData().getFlowStatusType() == 0) {
//                                changeBuy();
//                            } else {
                            if (!MyApp.getInstance().getReportFlowInfoData().getInvoiceInfo().isEmpty()) {
                                try {
                                    httpAPI.postInvoiceByGreen(MyApp.getInstance().getPostInvoiceByGreenData());
                                } catch (LogsSettingEmptyException | LogsParseException e) {
                                    getLogs().warning(e);
                                }
                            }

                            changeDispatch();
//                            }
                            break;
                        case POST_INVOICE:
                            try {
                                httpAPI.postInvoiceByGreen(MyApp.getInstance().getPostInvoiceByGreenData());
                            } catch (LogsSettingEmptyException | LogsParseException e) {
                                getLogs().warning(e);
                            }
                            changeDispatch();
                            break;
                        case REPORT_INVOICE:
                            changeDispatch();
                            break;
                        case SET_STATUS_TEXT:
                            binding.textStatus.setText(message);
                            break;
                    }
                }
            }
        };
    }

    private DialogCheckoutTapPayBinding binding;
    private CloseDialogOnThreadHandler closeDialogCountdown;
    private boolean openBarcode;
    private boolean isTransaction;
    private boolean isChangeDispatch;
    private HttpAPI httpAPI;
    private HttpTapPay httpTapPay;
    private boolean isGetRecordScheduled = false;

    public CheckoutTapPayDialog(Context context, HandlerMain srcHandlerMain) {
        super(context, srcHandlerMain);
    }

    @Override
    protected void onCreate(Context context) {
        binding = DialogCheckoutTapPayBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        openBarcode = true;
        isTransaction = true;
        isChangeDispatch = true;

        httpAPI = new HttpAPI(getHandlerMain());
        httpTapPay = new HttpTapPay(getHandlerMain());

        this.binding.buttonChange.setOnClickListener(v -> changeBuy());
        this.binding.buttonCancel.setOnClickListener(v -> dialogCancel());

        closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
        closeDialogCountdown.start(80);

        productNames = "";
        totalPrice = getTotalPrice();

        binding.textPrice.setText(String.valueOf(totalPrice));

        ProductStructure buyProduct = MyApp.getInstance().getBuyList().get(0).getProduct();
        MyApp.getInstance().getReportFlowInfoData().setProductId(buyProduct.getProductID());
        MyApp.getInstance().getReportFlowInfoData().setTotalPrice(getTotalPrice());
        JSONArray jsonArray = new JSONArray();

        try {
            MyApp.getInstance().getInvoiceItems().clear();
            for (BuyStructure buyData : MyApp.getInstance().getBuyList()) {
                JSONObject productData = new JSONObject();
                productData.put("pid", buyData.getProduct().getProductID());
                productData.put("amount", String.valueOf(buyData.getProduct().getRealPrice()));
                productData.put("num", String.valueOf(buyData.getCount()));
                jsonArray.put(productData);

                if (!productNames.isEmpty()) {
                    productNames += ",";
                }

                productNames += buyData.getProduct().getProductName()+"*"+buyData.getCount();

                InvoiceItemStructure invoiceItem = new InvoiceItemStructure("", "", 0, "件", 0, 0);
                invoiceItem.setItemSeq(buyData.getProduct().getProductID());
                invoiceItem.setItemName(buyData.getProduct().getProductName());
                invoiceItem.setItemCount(buyData.getCount());
                invoiceItem.setItemPrice(buyData.getProduct().getRealPrice());
                invoiceItem.setItemAmount(buyData.getFullPrice());
                try {
                    SlotField slotField = SlotField.getSlotField(buyData.getField());
                    switch (slotField) {
                        case VMC:
                            invoiceItem.setItemWord("件");
                            break;
                        case ELECTRIC:
                            invoiceItem.setItemWord("時");
                            break;
                        default:
                            invoiceItem.setItemWord("件");
                            break;
                    }
                } catch (LogsEmptyException e) {
                    invoiceItem.setItemWord("件");
                }
                MyApp.getInstance().getInvoiceItems().add(invoiceItem);
            }
            MyApp.getInstance().getPostInvoiceByGreenData().setInvoiceItem(MyApp.getInstance().getInvoiceItems());
            MyApp.getInstance().getPostInvoiceByGreenData().setInvoiceData(MyApp.getInstance().getInvoiceData());
            MyApp.getInstance().getPostInvoiceByGreenData().setTotalPrice(getTotalPrice());
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        MyApp.getInstance().getReportFlowInfoData().setShoppingCartInfoByJsonArray(jsonArray);

        try {
            showWeb(httpTapPay.getPrimeUrl(productNames, totalPrice));
        } catch (LogsSettingEmptyException e) {
            getLogs().warning(e);
        }

        binding.button30.setOnClickListener(v -> {
            buttonSetCloseColor();

            if (closeDialogCountdown != null && !closeDialogCountdown.isShutdown()) {
                closeDialogCountdown.setCountdown(80);
            } else {
                closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
                closeDialogCountdown.start(80);
            }

            binding.button30.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.black));
            binding.button30.setTextColor(ContextCompat.getColor(getCtx(), R.color.white));

            MyApp.getInstance().getReportFlowInfoData().setFlowType(30);
            try {
                showWeb(httpTapPay.getPrimeUrl(productNames, totalPrice));
            } catch (LogsSettingEmptyException e) {
                getLogs().warning(e);
            }
        });

        binding.button31.setOnClickListener(v -> {
            buttonSetCloseColor();

            if (closeDialogCountdown != null && !closeDialogCountdown.isShutdown()) {
                closeDialogCountdown.setCountdown(80);
            } else {
                closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
                closeDialogCountdown.start(80);
            }

            binding.button31.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.black));
            binding.button31.setTextColor(ContextCompat.getColor(getCtx(), R.color.white));

            MyApp.getInstance().getReportFlowInfoData().setFlowType(31);
            try {
                showWeb(httpTapPay.getPrimeUrl(productNames, totalPrice));
            } catch (LogsSettingEmptyException e) {
                getLogs().warning(e);
            }
        });

        binding.button32.setOnClickListener(v -> {
            buttonSetCloseColor();

            if (closeDialogCountdown != null && !closeDialogCountdown.isShutdown()) {
                closeDialogCountdown.setCountdown(80);
            } else {
                closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
                closeDialogCountdown.start(80);
            }

            binding.button32.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.black));
            binding.button32.setTextColor(ContextCompat.getColor(getCtx(), R.color.white));

            MyApp.getInstance().getReportFlowInfoData().setFlowType(32);
            try {
                showWeb(httpTapPay.getPrimeUrl(productNames, totalPrice));
            } catch (LogsSettingEmptyException e) {
                getLogs().warning(e);
            }
        });

        binding.button33.setOnClickListener(v -> {
            buttonSetCloseColor();

            if (closeDialogCountdown != null && !closeDialogCountdown.isShutdown()) {
                closeDialogCountdown.setCountdown(80);
            } else {
                closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
                closeDialogCountdown.start(80);
            }

            binding.button33.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.black));
            binding.button33.setTextColor(ContextCompat.getColor(getCtx(), R.color.white));

            MyApp.getInstance().getReportFlowInfoData().setFlowType(33);
            try {
                showWeb(httpTapPay.getPrimeUrl(productNames, totalPrice));
            } catch (LogsSettingEmptyException e) {
                getLogs().warning(e);
            }
        });

        binding.button34.setOnClickListener(v -> {
            buttonSetCloseColor();

            if (closeDialogCountdown != null && !closeDialogCountdown.isShutdown()) {
                closeDialogCountdown.setCountdown(80);
            } else {
                closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
                closeDialogCountdown.start(80);
            }

            binding.button34.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.black));
            binding.button34.setTextColor(ContextCompat.getColor(getCtx(), R.color.white));

            MyApp.getInstance().getReportFlowInfoData().setFlowType(34);
            try {
                showWeb(httpTapPay.getPrimeUrl(productNames, totalPrice));
            } catch (LogsSettingEmptyException e) {
                getLogs().warning(e);
            }
        });

        if (MyApp.getInstance().getDevSet().isTapPay30()) {
            binding.button30.setVisibility(View.VISIBLE);
        } else {
            binding.button30.setVisibility(View.GONE);
        }

        if (MyApp.getInstance().getDevSet().isTapPay31()) {
            binding.button31.setVisibility(View.VISIBLE);
        } else {
            binding.button31.setVisibility(View.GONE);
        }

        if (MyApp.getInstance().getDevSet().isTapPay32()) {
            binding.button32.setVisibility(View.VISIBLE);
        } else {
            binding.button32.setVisibility(View.GONE);
        }

        if (MyApp.getInstance().getDevSet().isTapPay33()) {
            binding.button33.setVisibility(View.VISIBLE);
        } else {
            binding.button33.setVisibility(View.GONE);
        }

        if (MyApp.getInstance().getDevSet().isTapPay34()) {
            binding.button34.setVisibility(View.VISIBLE);
        } else {
            binding.button34.setVisibility(View.GONE);
        }
    }

    public void buttonSetCloseColor() {
        binding.button30.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.gray));
        binding.button30.setTextColor(ContextCompat.getColor(getCtx(), R.color.black));
        binding.button31.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.gray));
        binding.button31.setTextColor(ContextCompat.getColor(getCtx(), R.color.black));
        binding.button32.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.gray));
        binding.button32.setTextColor(ContextCompat.getColor(getCtx(), R.color.black));
        binding.button33.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.gray));
        binding.button33.setTextColor(ContextCompat.getColor(getCtx(), R.color.black));
        binding.button34.setBackgroundTintList(ContextCompat.getColorStateList(getCtx(), R.color.gray));
        binding.button34.setTextColor(ContextCompat.getColor(getCtx(), R.color.black));
    }

    public int getTotalPrice() {
        int totalPrice = 0;
        if (MyApp.getInstance().getBuyList().size() == 0) return totalPrice;
        for(BuyStructure buy: MyApp.getInstance().getBuyList()) {
            if (!buy.isFreeGift()) totalPrice += buy.getFullPrice();
        }
        if (MyApp.getInstance().getReportFreeGiftCodeStructure() != null) {
            switch (MyApp.getInstance().getReportFreeGiftCodeStructure().getMode()) {
                case 0:
                    break;
                case 1:
                    totalPrice = totalPrice - MyApp.getInstance().getReportFreeGiftCodeStructure().getDiscountAmount();
                    break;
                case 2:
                    totalPrice = (int) (totalPrice * MyApp.getInstance().getReportFreeGiftCodeStructure().getDiscountPercentage());
                    break;
            }
        }
        if (totalPrice == 0) totalPrice = 1;
        return totalPrice;
    }

    private void showWeb(String strWeb) {
        if (strWeb == null || !strWeb.startsWith("http")) {
            binding.textStatus.setText("支付連結無效，請選擇其他方式");
            return;
        }
        binding.textStatus.setText("QRCode產生中，請等待片刻");
        binding.layoutQRCode.setVisibility(View.VISIBLE);
        WebSettings webViewSettings = binding.webViewQRCode.getSettings();
        webViewSettings.setAllowFileAccessFromFileURLs(true);
        webViewSettings.setAllowUniversalAccessFromFileURLs(true);

        // ✅ 允許開啟新的視窗（有些 JS 會用 window.open）
        webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        // ✅ 啟用 DOM 儲存（某些第三方支付 JS 會用 localStorage）
        webViewSettings.setDomStorageEnabled(true);
        // ✅ 增加相容性：支援 viewport 及畫面縮放
        webViewSettings.setUseWideViewPort(true);
        webViewSettings.setLoadWithOverviewMode(true);
        // ✅ 強制混合內容允許（Tappay 有時 JS 來自 HTTP CDN）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webViewSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        // ✅ 修改 User Agent：模擬正常瀏覽器（防止被 Tappay 認為是機器人或 App 環境）
        String newUA = webViewSettings.getUserAgentString() + " Chrome/110.0 Mobile Safari/537.36";
        webViewSettings.setUserAgentString(newUA);
        // ✅ Cookie 管理（允許跨站第三方 Cookie）
        CookieManager.getInstance().setAcceptCookie(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(binding.webViewQRCode, true);
        }

        binding.webViewQRCode.getSettings().setJavaScriptEnabled(true);
        webViewSettings.setUseWideViewPort(true);
        webViewSettings.setLoadWithOverviewMode(true);
        String timeStamp = new SimpleDateFormat("yyyyMMddhhmmss", Locale.US).format(new Date());
        String callBackStr = MyApp.getInstance().getMachine().getMachineID() + timeStamp;
        binding.webViewQRCode.loadUrl(strWeb + "&order_number=" + callBackStr);
        binding.textStatus.setText("請先開啟APP，掃QRCode支付");

        scheduleAutoRetryGetRecord(); // ✅ 使用帶有防重複的排程方法

//        getRecord();
//        getHandlerMain().getHandler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                binding.textStatus.setText("請先開啟APP，掃QRCode支付");
//                getRecord();
//            }
//        }, 10000);

    }

    private void scheduleAutoRetryGetRecord() {
        if (isGetRecordScheduled || !isTransaction) return; // 🚨 交易已完成，不再重複排程

        isGetRecordScheduled = true;

        getHandlerMain().getHandler().postDelayed(() -> {
            if (!isTransaction) {
                isGetRecordScheduled = false;
                return; // ✅ 已完成交易，停止重送
            }

            try {
                httpTapPay.getRecord();
            } catch (LogsParseException | LogsSettingEmptyException e) {
                getLogs().warning(e);
            }

            if (!isTransactionSuccess()) {
                isGetRecordScheduled = false;
                scheduleAutoRetryGetRecord(); // ⏱️ 繼續每 5 秒排程一次
            } else {
                isTransaction = false; // ✅ 成功後立刻鎖住
                isGetRecordScheduled = false;
            }

        }, 5000);
    }

    private boolean isTransactionSuccess() {
        return MyApp.getInstance().getReportFlowInfoData().getFlowStatusType() == 1;
    }

    public void getRecord() {
        if (!isTransaction) {
            getLogs().info("交易已完成或正在處理，不再送出 getRecord");
            return;
        }
        try {
            httpTapPay.getRecord();
        } catch (LogsParseException | LogsSettingEmptyException e) {
            getLogs().warning(e);
        }
//        getHandlerMain().getHandler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    httpTapPay.getRecord();
//                } catch (LogsParseException e) {
//                    getLogs().warning(e);
//                } catch (LogsSettingEmptyException e) {
//                    getLogs().warning(e);
//                }
//            }
//        }, 5000);
    }

    public void changeDispatch() {
        if (isChangeDispatch) {
            closeDialogCountdown.shutdown();
            openBarcode = false;
            isChangeDispatch = false;
            changeDialog(DispatchDialog.class);
        }
    }

    public void changeBuy() {
        openBarcode = false;
        closeDialogCountdown.shutdown();
        changeDialog(BuyDialog.class);
    }

    public void dialogCancel() {
        if (this.isShowing()) {
            MyApp.getInstance().setReportFreeGiftCodeStructure(new ReportFreeGiftCodeStructure("", 0, "", 0, 0, ""));
            getSrcHandlerMain().start(getClass().getSimpleName(), FontendActivity.Option.STOP_COUNTDOWN.getOption(), "");
            openBarcode = false;
//        readBarCodeOnScheduler.setRun(false);
            closeDialogCountdown.shutdown();
            MyApp.getInstance().getBuyList().clear();
            cancel();
        }
    }

    public void cancelOnThreadCountdown() {
        openBarcode = false;
        MyApp.getInstance().getBuyList().clear();
        cancel();
    }

    public void setButtonCancelText(long countdown) {
        binding.buttonCancel.setText("取消("+countdown+")");
    }

    public class ReadBarCodeOnScheduler extends HandlerMainScheduler {
        public ReadBarCodeOnScheduler(HandlerMain handlerMain) {
            super(handlerMain);
        }

        @Override
        protected HandlerMain setHandlerMain() {
            return getSrcHandlerMain();
        }

        @Override
        protected void execute(HandlerMain handlerMain) {
            try {
                int totalPrice = 0;
                for (BuyStructure buy : MyApp.getInstance().getBuyList()) {
                    totalPrice += buy.getFullPrice();
                }

                UsbDev usbDev = new UsbDev(DeviceType.QRCODE_SCANNER);
                byte[] buffer = new byte[1000];
                usbDev.read(buffer, 100);
                for (int i=0; i<7; i++) {
                    if (isRun() && openBarcode) {
                        int size = usbDev.read(buffer, 5000);
                        if (size > 5) {
                            String barCodeString = new String(buffer, 0, size);
                            getHandlerMain().start(getLogs().getClassName(), Option.SET_STATUS_TEXT.getOption(), "收到條碼：" + Tools.randomReplace(barCodeString, 0.3) + "，送出交易");
//                            try {
//                                httpAPI.esunpay(barCodeString, totalPrice);
//                            } catch (LogsSettingEmptyException e) {
//                                getLogs().warning(e);
//                            }

                            openBarcode = false;
                        }
                    }
                }
                usbDev.closePort();
            } catch (LogsUnsupportedOperationException | LogsIOException | LogsEmptyException e) {
                getLogs().warning(e);
            }
        }

        @Override
        protected Class<?> setCls() {
            return getClass();
        }
    }

    public class CloseDialogOnThreadHandler extends HandlerMainCountdown {
        public CloseDialogOnThreadHandler(HandlerMain handlerMain) {
            super(handlerMain);
        }

        @Override
        protected HandlerMain setHandlerMain() {
            return getSrcHandlerMain();
        }

        @Override
        protected void close(HandlerMain handlerMain) {
            // 安全判斷，避免 dialog 已經被關閉後再次 cancel 造成錯誤
            if (CheckoutTapPayDialog.this.isShowing()) {
                handlerMain.start(getClass().getSimpleName(), Option.COUNTDOWN_CANCEL.getOption(), "close dialog");
            }
        }

        @Override
        protected void execute(long countdown, HandlerMain handlerMain) {
            handlerMain.start(getClass().getSimpleName(), Option.SET_COUNTDOWN_TEXT.getOption(), String.valueOf(countdown));
        }

        @Override
        protected Class<?> setCls() {
            return getClass();
        }
    }
}
