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.DialogPickupErrorTipBinding;
import com.unibuy.smartdevice.structure.BuyStructure;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainCountdown;
import com.unibuy.smartdevice.tools.ToastHandlerMain;

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

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

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

        private int option;

        public int getOption() {
            return option;
        }

        Option(int option) {
            this.option = option;
        }
    }
    public PickupErrorTipDialog(Context context, HandlerMain srcHandlerMain) {
        super(context, srcHandlerMain);
        getLogs().info("PickupErrorTipDialog:" + 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) {
//                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;
                        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;
                    }
               // }
            }
        };
    }

    private DialogPickupErrorTipBinding binding;
    //private RecyclerDialogTakeMedicineListAdpter recyclerDialogTakeMedicineListAdpter;
    private CloseDialogOnThreadHandler closeDialogCountdown;

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

        String decryptedStr = MyApp.getInstance().getDecryptedData();
        getLogs().info(decryptedStr);
        try {
            JSONObject jsonObject = new JSONObject(decryptedStr);
            binding.tvDrgnoid.setText("領藥號："+jsonObject.optString("DRGNO"));
        } catch (JSONException e) {
            getLogs().warning(e);
        }
        String errorMessage = MyApp.getInstance().getCmuhPickupErrorMessage();
        if (errorMessage == null || errorMessage.trim().isEmpty()) {
            errorMessage = getCtx().getString(R.string.cmuh_pickup_failed_contact_counter);
        }
        binding.tvErrorMessage.setText(errorMessage);

        closeDialogCountdown = new CloseDialogOnThreadHandler(getHandlerMain());
        closeDialogCountdown.start(40);
        binding.btnNonPerson.setOnClickListener(v -> dialogCancel());
    }

    public void addMedicineToBuyList(String MedicinePidStr,String MedicineNameStr,int SlotNumber,String MedicineImg){
        ProductStructure productStructure = new ProductStructure(MedicinePidStr,MedicineImg,MedicineNameStr,0, 0, 0, 0, "", "", "", "");
        //getLogs().info("aaaa"+SlotNumber);
        BuyStructure buyProduct = new BuyStructure(0, SlotNumber, productStructure, 1);
        MyApp.getInstance().getBuyList().add(buyProduct);
    }

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

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

    public void setButtonCancelText(long countdown) {
        binding.btnNonPerson.setText(getCtx().getString(R.string.close_countdown, 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();
        }
    }

}
