package com.datarecovery.master.module.order;

import android.os.Bundle;
import android.util.Pair;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.atmob.app.lib.base.BaseFragment;
import com.datarecovery.master.R;
import com.datarecovery.master.data.api.bean.OrderBean;
import com.datarecovery.master.data.api.bean.PayOptions;
import com.datarecovery.master.databinding.FragmentOrderBinding;
import com.datarecovery.master.dialog.AlipayQrCodeDialog;
import com.datarecovery.master.dialog.CommonLoadingDialog;
import com.datarecovery.master.dialog.CommonSureDialog;
import com.datarecovery.master.dialog.WechatPayQrCodeDialog;
import com.datarecovery.master.utils.BoxingUtil;
import com.gyf.immersionbar.ImmersionBar;

import java.util.Objects;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class OrderFragment extends BaseFragment<FragmentOrderBinding> {


    private OrderViewModel orderViewModel;

    private OrderItemAdapter orderItemAdapter;
    private CommonLoadingDialog loadingDialog;
    private AlipayQrCodeDialog alipayQrCodeDialog;
    private WechatPayQrCodeDialog wechatPayQrCodeDialog;
    private CommonSureDialog cancelOrderDialog;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initObserver();
    }

    private void initView() {
        orderItemAdapter = new OrderItemAdapter(getViewLifecycleOwner());
        binding.ryOrder.setAdapter(orderItemAdapter);
        orderItemAdapter.setOrderActionHandler(new OrderItemAdapter.OrderActionHandler() {
            @Override
            public void cancel(OrderBean orderBean) {
                showCancelDialog(orderBean);
            }

            @Override
            public void payOrder(OrderBean orderBean) {
                orderViewModel.payOrder(orderBean);
            }
        });
        binding.ryOrder.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.swiperLayout.setOnRefreshListener(() -> orderViewModel.refreshOrderPageList());
    }

    private void showCancelDialog(OrderBean orderBean) {
        if (cancelOrderDialog == null) {
            cancelOrderDialog = new CommonSureDialog(requireActivity());
            cancelOrderDialog.setDialogTitle(R.string.dialog_kind_tips).setDialogContent(R.string.order_cancel_content);
        }
        cancelOrderDialog.setOnDialogClickListener(() -> orderViewModel.cancelOrder(orderBean));
        cancelOrderDialog.show();

    }

    private void initObserver() {
        orderViewModel.getOnSubscribeSuccessEvent().observe(getViewLifecycleOwner(), this::dismissQrCodeDialog);
        orderViewModel.getShowWxQRPaymentEvent().observe(getViewLifecycleOwner(), this::showWxQrCodeDialog);
        orderViewModel.getShowAliQRPaymentEvent().observe(getViewLifecycleOwner(), this::showAlipayQrCodeDialog);
        orderViewModel.getLoading().observe(getViewLifecycleOwner(), this::showLoadingDialog);
        orderViewModel.getOrderList().observe(getViewLifecycleOwner(), list -> orderItemAdapter.submit(list));
        orderViewModel.getRefreshOrderListEvent().observe(getViewLifecycleOwner(), o -> binding.swiperLayout.setRefreshing(false));
    }

    private void showWxQrCodeDialog(PayOptions payOptions) {
        if (payOptions == null || payOptions.getWxQrCode() == null) {
            return;
        }
        Pair<String, String> params = payOptions.getWxQrCode();
        if (wechatPayQrCodeDialog == null) {
            wechatPayQrCodeDialog = new WechatPayQrCodeDialog(requireActivity());
        }
        wechatPayQrCodeDialog.show(params.first, params.second, (orderId, payWay, showLoading, isReportPay) -> orderViewModel.queryOrderStatus(orderId, payWay, payOptions, showLoading, isReportPay));
    }

    private void dismissQrCodeDialog(String orderId) {
        if (alipayQrCodeDialog != null) {
            if (Objects.equals(orderId, alipayQrCodeDialog.getCurrentOrderId())) {
                alipayQrCodeDialog.dismiss();
            }
        }
        if (wechatPayQrCodeDialog != null) {
            if (Objects.equals(orderId, wechatPayQrCodeDialog.getCurrentOrderId())) {
                wechatPayQrCodeDialog.dismiss();
            }
        }
    }

    private void showAlipayQrCodeDialog(PayOptions payOptions) {
        if (payOptions == null || payOptions.getAlQrCode() == null) {
            return;
        }
        Pair<String, String> params = payOptions.getAlQrCode();
        if (alipayQrCodeDialog == null) {
            alipayQrCodeDialog = new AlipayQrCodeDialog(requireActivity());
        }
        alipayQrCodeDialog.show(params.first, params.second, (orderId, payWay, showLoading, isReportPay) -> orderViewModel.queryOrderStatus(orderId, payWay, payOptions, showLoading, isReportPay));
    }

    public void showLoadingDialog(Boolean show) {
        if (BoxingUtil.boxing(show)) {
            if (loadingDialog == null) {
                loadingDialog = new CommonLoadingDialog(requireActivity());
                loadingDialog.setMessageGone();
            }
            loadingDialog.show();
        } else {
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
        }
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        orderViewModel = getViewModelProvider().get(OrderViewModel.class);
        binding.setOrderViewModel(orderViewModel);
    }

    @Override
    protected void configImmersion(@NonNull ImmersionBar immersionBar) {
        immersionBar.statusBarDarkFont(true);
    }
}
