package com.unibuy.smartdevice.ui.dialog;

import android.content.Context;

import com.unibuy.smartdevice.DialogAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.databinding.DialogInvoiceTypeBinding;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainCountdown;
import com.unibuy.smartdevice.tools.ToastHandlerMain;

import java.util.HashMap;
import java.util.Map;

/**
 * 選擇發票開立方式介面
 * 提供三個選項：輸入載具 / 捐贈發票 / 公司統編
 * 位於 BuyDialog（選擇支付方式）與各發票設定頁面之間
 */
public class InvoiceTypeDialog extends DialogAbstract {

    public enum Option {
        TOAST(0),
        COUNTDOWN_CANCEL(1),
        SET_COUNTDOWN_TEXT(2),
        ;

        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);
        }
    }

    @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);
                    switch (option) {
                        case TOAST:
                            super.execute(context, commandCode, message);
                            break;
                        case COUNTDOWN_CANCEL:
                            cancelOnThreadCountdown();
                            break;
                        case SET_COUNTDOWN_TEXT:
                            setButtonCancelText(Integer.valueOf(message));
                            break;
                    }
                }
            }
        };
    }

    private DialogInvoiceTypeBinding binding;
    private CloseDialogOnThreadHandler closeDialogCountdown;

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

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

        android.view.Window window = getWindow();
        if (window != null) {
            android.view.WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.gravity = android.view.Gravity.CENTER;
            window.setAttributes(layoutParams);
        }

        // 選擇：輸入載具（手機條碼）
        binding.cardVehicle.setOnClickListener(v -> {
            closeDialogCountdown.shutdown();
            changeDialog(InvoiceBarcodeDialog.class);
        });

        // 選擇：捐贈發票
        binding.cardDonation.setOnClickListener(v -> {
            closeDialogCountdown.shutdown();
            changeDialog(InvoiceDonationDialog.class);
        });

        // 選擇：公司統編
        binding.cardCustomerIdentifier.setOnClickListener(v -> {
            closeDialogCountdown.shutdown();
            changeDialog(InvoiceCustomerIdentifierDialog.class);
        });

        // 返回支付方式
        binding.buttonCancel.setOnClickListener(v -> changeBuy());

        closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
        closeDialogCountdown.start(40);
    }

    /**
     * 返回 BuyDialog（選擇支付方式）
     */
    public void changeBuy() {
        closeDialogCountdown.shutdown();
        new BuyDialog(getContext(), getSrcHandlerMain()).show();
        cancel();
    }

    /**
     * 倒數逾時時清空購物車並關閉
     */
    public void cancelOnThreadCountdown() {
        MyApp.getInstance().getBuyList().clear();
        cancel();
    }

    public void dialogCancel() {
        closeDialogCountdown.shutdown();
        MyApp.getInstance().getBuyList().clear();
        cancel();
    }

    public void setButtonCancelText(long countdown) {
        binding.buttonCancel.setText(getCtx().getString(R.string.return_to) + countdown + getCtx().getString(R.string.second));
    }

    public static class CloseDialogOnThreadHandler extends HandlerMainCountdown {
        public CloseDialogOnThreadHandler(HandlerMain handlerMain) {
            super(handlerMain);
            handlerMain.start(getClass().getSimpleName(), Option.SET_COUNTDOWN_TEXT.getOption(), String.valueOf(getSrcCountdown()));
        }

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

        @Override
        protected void close(HandlerMain handlerMain) {
            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();
        }
    }
}
