package cn.yyxx.commsdk.core.ui.dialog;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;


import cn.yyxx.commsdk.base.entity.SdkOrderInfo;
import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.support.AppUtils;


public class CommPayDialog extends Dialog implements View.OnClickListener{


    private Context context;
    private WebView webView;
    private WebSettings webSettings;
    private String orderId;
    private ImageView iv_reback_close;
    private SdkOrderInfo sdkOrderInfo;



    public CommPayDialog(Context context, int theme, SdkOrderInfo sdkOrderInfo) {
        super(context, theme);
        this.context = context;
         this.sdkOrderInfo = sdkOrderInfo;

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(ResourceUtil.getLayoutId(context,
                "yyxx_comm_p_webview"));


        if (!AppUtils.isLandscape(context)){
            LinearLayout ll_p_wb = findViewById(ResourceUtil.getId(context, "ll_p_wb"));

            ll_p_wb.setScaleX(0.9f);
            ll_p_wb.setScaleY(0.9f);
        }

        iv_reback_close = findViewById(ResourceUtil.getId(context,
                "iv_reback_close"));
        iv_reback_close.setOnClickListener(this);

        webView = findViewById(ResourceUtil.getId(context,
                "wb_with_xd"));



        webSettings = webView.getSettings();
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setBuiltInZoomControls(false);// 设置支持缩放
        webSettings.setDomStorageEnabled(true);

        webView.addJavascriptInterface(new Object(){
            @JavascriptInterface
            public void zfColse() {   //提供给js调用的方法
                dismiss();
            }
            @JavascriptInterface
            public void zfOrder(String message) {
                orderId = message;
            }
        }, "yyrhsdk");

        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                String httpHead = url.substring(0, 5);
                if (httpHead.equals("http:") || httpHead.equals("https")) {
                    return super.shouldOverrideUrlLoading(view, url);
                } else if (url.equals("yyrhsdk://pay_close")) {

                    dismiss();

                    return true;

                } else if (url.equals("yyrhsdk://pay_success")) {
                    dismiss();
                    return true;
                } else {
                    if (httpHead.equals("yyrhsdk")) {

                        return true;
                    } else {
                        openPay(url);
                        return true;
                    }
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }



            private void openPay(String code_url) {
                try {
                    Intent it = new Intent(Intent.ACTION_VIEW);
                    it.setData(Uri.parse(code_url));
                    context.startActivity(it);
                } catch (Exception e) {

                }
            }
        });

        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                context.startActivity(intent);
            }
        });


        webView.loadUrl(sdkOrderInfo.getPay_web_url());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == ResourceUtil.getId(context, "iv_reback_close")) {
            dismiss();

        }
    }

    @Override
    public void dismiss() {
        super.dismiss();

    }





}
