package com.yyrh.ui.dialog;

import static com.yyrh.constant.SDKSettings.isLandscape;

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.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yyrh.sdk.YyrhSdkManager;
import com.yyrh.ui.PageJsInteraction;
import com.yyrh.utils.ResourceUtil;
import com.yyrh.utils.TosUtil;
import com.yyrh.utils.Utils;

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

import java.util.HashMap;


public class WvPayYyrhDialog extends BaseDialog implements View.OnClickListener{


    private Context context;
    private WebView webView;
    private WebSettings webSettings;
    private String url,orderId;
    private ImageView iv_reback_close;
    private  HashMap<String, Object> payMap;

    public WvPayYyrhDialog(Context context) {
        super(context);
        this.context = context;
    }


    public WvPayYyrhDialog(Context context, int theme, String url, HashMap<String, Object> map) {
        super(context, theme);
        this.context = context;
        this.payMap = map;
        this.url = url;

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

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


        if (!isLandscape){
            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 PageJsInteraction(this), "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")) {
                        toDoHttp_https(url);
                        return true;
                    } else {
                        openWXPay(url);
                        return true;
                    }
                }
            }

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

            }

            private void toDoHttp_https(String url) {
                String[] strArray = url.split("=");
                String strurl = Utils.decoBase64(strArray[1]);
                String code_url = null;
                try {
                    JSONObject json = new JSONObject(strurl);
                    code_url = json.getString("code_url");
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                openWXPay(code_url);


            }

            private void openWXPay(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(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();
        if (!Utils.judgeStrNull(orderId)){
            YyrhSdkManager.defaultManager().queryOrder(orderId,this.payMap);
        }
    }


    @Override
    public void zfOrder(String message) {
        super.zfOrder(message);
        orderId = message;
    }

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

    @Override
    public void showJsMsg(String message) {
        super.showJsMsg(message);
        new TosUtil(context,TosUtil.TOAST_ERROR,  message).show();
    }
}
