package com.datarecovery.master.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;

import com.atmob.app.lib.base.BaseDialog;
import com.atmob.common.ui.SizeUtil;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.Constants;
import com.datarecovery.master.databinding.DialogAlipayQrCodeBinding;
import com.datarecovery.master.module.member.MemberViewModel;

import atmob.reactivex.rxjava3.disposables.Disposable;

@BaseDialog.FullScreen
public class AlipayQrCodeDialog extends BaseDialog<DialogAlipayQrCodeBinding> {
    private static final String HTML_TEMPLATE = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/><title>Alipay QR Code</title></head><body>${qr_code_html}</body></html>";
    private @atmob.reactivex.rxjava3.annotations.NonNull Disposable queryOrderDisposable;
    private String orderId;
    private MemberViewModel memberViewModel;

    @SuppressLint("SetJavaScriptEnabled")
    public AlipayQrCodeDialog(@NonNull Context context) {
        super(context, R.style.Theme_Common_Dialog);
        setCanceledOnTouchOutside(false);
        binding.alipayQrCodeWebView.getSettings().setJavaScriptEnabled(true);
        binding.alipayQrCodeWebView.getSettings().setLoadsImagesAutomatically(true);
        binding.alipayQrCodeWebView.setInitialScale((int) (SizeUtil.getScreenWidth() * 63.88888888888889f / 110));
        binding.alipayQrCodeWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }
        });
        binding.setOnCloseClickListener(v -> dismiss());
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        if (queryOrderDisposable != null && !queryOrderDisposable.isDisposed()) {
            queryOrderDisposable.dispose();
            memberViewModel.queryOrderStatus(orderId, Constants.PAYMENT_WAY_ALIPAY_SCAN, true, true);
        }
        binding.alipayQrCodeWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
    }

    public void show(String qrCodeHtml, String orderId, MemberViewModel memberViewModel) {
        this.memberViewModel = memberViewModel;
        this.orderId = orderId;
        this.queryOrderDisposable = memberViewModel.queryOrderStatus(orderId, Constants.PAYMENT_WAY_ALIPAY_SCAN, false, true);
        binding.alipayQrCodeWebView.loadDataWithBaseURL(null, HTML_TEMPLATE.replace("${qr_code_html}", qrCodeHtml), "text/html", "utf-8", null);
        super.show();
    }

    public String getCurrentOrderId() {
        return orderId;
    }
}