package com.unibuy.smartdevice.ui.dialog;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.unibuy.smartdevice.DialogAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.databinding.DialogMarketingPlanBinding;
import com.unibuy.smartdevice.structure.BuyStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainCountdown;
import com.unibuy.smartdevice.tools.ToastHandlerMain;
import com.unibuy.smartdevice.ui.recycler.RecyclerDialogMarketingPlanListAdpter;

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

public class MarketingPlanDialog extends DialogAbstract {
    public enum Option {
        TOAST(0),
        COUNTDOWN_CANCEL(1),
        SET_COUNTDOWN_TEXT(2),
        SET_PRICE(3),
        RESET_COUNTDOWM(4),
        RESET_COUNTDOWM_TOAST(5),
        CHANGE_BUY(6),
        ;

        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(getContext(), getLogs()) {
            @Override
            protected void execute(Context context, int commandCode, String message) {
                if (MyApp.getInstance().getMarketingPlanList().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;
                        case SET_PRICE:
                                closeDialogCountdown.setCountdown(40);
                            break;
                        case RESET_COUNTDOWM:
                                closeDialogCountdown.setCountdown(40);
                            break;
                        case RESET_COUNTDOWM_TOAST:
                                super.execute(context, commandCode, message);
                                closeDialogCountdown.setCountdown(40);
                            break;
                        case CHANGE_BUY:
                            changeBuy();
                            break;
                    }
                }
            }
        };
    }

    private DialogMarketingPlanBinding binding;
    private RecyclerDialogMarketingPlanListAdpter recyclerDialogMarketingPlanListAdpter;
    private CloseDialogOnThreadHandler closeDialogCountdown;

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

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

        this.recyclerDialogMarketingPlanListAdpter = new RecyclerDialogMarketingPlanListAdpter(getCtx(), MyApp.getInstance().getMarketingPlanList(), getHandlerMain());
        this.binding.recyclerDialogMarketingPlanList.setAdapter(recyclerDialogMarketingPlanListAdpter);
        this.binding.recyclerDialogMarketingPlanList.setLayoutManager(new LinearLayoutManager(context));

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

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

    public int getTotalPrice() {
        int totalPrice = 0;
        if (MyApp.getInstance().getBuyList().size() == 0) return totalPrice;
        for(BuyStructure buy: MyApp.getInstance().getBuyList()) {
            totalPrice += buy.getFullPrice();
        }
        return totalPrice;
    }

    public void dialogCancel() {
        closeDialogCountdown.shutdown();
        int buyListSize = MyApp.getInstance().getBuyList().size();
        MyApp.getInstance().getBuyList().clear();
        if (buyListSize > 0) {
            recyclerDialogMarketingPlanListAdpter.notifyItemRangeRemoved(0, buyListSize);
        }
        cancel();
    }

    public void cancelOnThreadCountdown() {
        int buyListSize = MyApp.getInstance().getBuyList().size();
        MyApp.getInstance().getBuyList().clear();
        if (buyListSize > 0) {
            recyclerDialogMarketingPlanListAdpter.notifyItemRangeRemoved(0, buyListSize);
        }
        getTools().dialogClose();
        cancel();
    }

    public void setButtonCancelText(long countdown) {
        binding.buttonCancel.setText(getContext().getString(R.string.cancel)+"("+countdown+")");//取消
    }

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

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

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