package com.unibuy.smartdevice.ui.dialog;
import com.unibuy.smartdevice.R;

import android.content.Context;

import com.unibuy.smartdevice.DialogAbstract;
import com.unibuy.smartdevice.databinding.DialogPickupSuccessBinding;
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;

public class PickupSuccessDialog extends DialogAbstract {
    public enum Option {
        TOAST(0),
        COUNTDOWN_CANCEL(1),
        SET_COUNTDOWN_TEXT(2),
        RESET_COUNTDOWN(3),
        ;

        private int option;

        public int getOption() {
            return option;
        }

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

    public PickupSuccessDialog(Context context, HandlerMain srcHandlerMain) {
        super(context, srcHandlerMain);
        getLogs().info("PickupSuccessDialog:" + getClass().getSimpleName());
    }

    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 this.getContext();
    }

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

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getContext(), getLogs()) {
            @Override
            protected void execute(Context context, int commandCode, String message) {
                Option option = optionMap.get(commandCode);
                if (option != null) {
                    switch (option) {
                        case TOAST:
                            super.execute(context, commandCode, message);
                            break;
                        case COUNTDOWN_CANCEL:
                            cancelOnThreadCountdown();
                            break;
                        case SET_COUNTDOWN_TEXT:
                            setButtonConfirmText(Integer.valueOf(message));
                            break;
                        case RESET_COUNTDOWN:
                            closeDialogCountdown.setCountdown(15);
                            break;
                    }
                }
            }
        };
    }

    private DialogPickupSuccessBinding binding;
    private CloseDialogOnThreadHandler closeDialogCountdown;
    private String laneNumber = "99"; // 預設貨道號碼

    public void setLaneNumber(String laneNumber) {
        this.laneNumber = laneNumber;
        if (binding != null) {
            binding.tvLaneNumber.setText(laneNumber);
            binding.tvSubtitle.setText(getCtx().getString(R.string.go_to_cabinet_format, laneNumber));
        }
    }

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

        // 初始化貨道數字與副標文字
        setLaneNumber(laneNumber);

        binding.btnConfirm.setOnClickListener(v -> dialogCancel());

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

    public void dialogCancel() {
        if (closeDialogCountdown != null) {
            closeDialogCountdown.shutdown();
        }
        cancel();
    }

    public void cancelOnThreadCountdown() {
        getTools().dialogClose();
        cancel();
    }

    public void setButtonConfirmText(long countdown) {
        binding.btnConfirm.setText(getCtx().getString(R.string.confirm_taken_format, countdown));
    }

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

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