package com.datarecovery.master.dialog;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;


import com.atmob.app.lib.base.BaseDialog;
import com.datarecovery.master.R;
import com.datarecovery.master.data.api.bean.PayOptionsBean;
import com.datarecovery.master.databinding.DialogChoosePaymentWayBinding;
import com.datarecovery.master.databinding.LayoutItemPaymentWayBinding;
import com.datarecovery.master.utils.ToastUtil;

import java.util.List;

@BaseDialog.FullScreen(height = false, gravity = Gravity.BOTTOM)
public class ChoosePaymentWayDialog extends BaseDialog<DialogChoosePaymentWayBinding> {

    private ActionHandler actionHandler;

    private final List<PayOptionsBean> paymentWayList;

    private final MutableLiveData<Integer> selectedPosition = new MutableLiveData<>(0);

    @SuppressLint("UseCompatLoadingForDrawables")
    public ChoosePaymentWayDialog(@NonNull Context context, LifecycleOwner lifecycleOwner, LiveData<String> payNowTxt, List<PayOptionsBean> paymentWayList) {
        super(context, R.style.Theme_Common_BottomSheetDialog);
        this.paymentWayList = paymentWayList;
        setCancelable(false);
        binding.setPayNowTxt(payNowTxt);
        binding.setOnCloseClickListener(v -> dismiss());
        binding.setOnPayClickListener(v -> {
            Integer index = selectedPosition.getValue();
            if (actionHandler != null && index != null) {
                PayOptionsBean payOptionsBean = paymentWayList.get(index);
                actionHandler.onPayClick(payOptionsBean.getPayPlatform(), payOptionsBean.getPayMethod());
            }
            dismiss();
        });
        if (paymentWayList == null) {
            return;
        }
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        binding.llPay.removeAllViews();
        for (int i = 0; i < paymentWayList.size(); i++) {
            PayOptionsBean paymentWayBean = paymentWayList.get(i);
            LayoutItemPaymentWayBinding itemPaymentWayBinding = LayoutItemPaymentWayBinding.inflate(layoutInflater, binding.llPay, true);
            if (paymentWayBean.getPayWayIcon() != 0) {
                itemPaymentWayBinding.setPaymentWayIcon(context.getResources().getDrawable(paymentWayBean.getPayWayIcon()));
            }
            itemPaymentWayBinding.setSelectedPosition(selectedPosition);
            itemPaymentWayBinding.setPosition(i);
            itemPaymentWayBinding.setPaymentWayName(paymentWayBean.getTitle());
            itemPaymentWayBinding.setLifecycleOwner(lifecycleOwner);
            int finalI = i;
            itemPaymentWayBinding.getRoot().setOnClickListener(v -> selectedPosition.setValue(finalI));
        }
    }


    public void paymentShow() {
        if (paymentWayList == null || paymentWayList.size() == 0) {
            ToastUtil.show(R.string.no_pay_way, ToastUtil.LENGTH_SHORT);
            return;
        }
        if (paymentWayList.size() == 1) {

        } else {
            show();
        }
    }


    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    public interface ActionHandler {
        void onPayClick(int payPlatform, int payMethod);
    }

    public ChoosePaymentWayDialog setActionHandler(ActionHandler actionHandler) {
        this.actionHandler = actionHandler;
        return this;
    }
}
