package com.yyrh.ui.dialog;


import android.content.Context;
import android.content.Intent;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;


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

import java.net.URISyntaxException;

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


public class EmailDialog extends BaseDialog {


    private ImageView close_btn;
    private Context context;
    private WebView webView;
    private WebSettings webSettings;

    private String url;


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


    public EmailDialog(Context context, int theme, String url) {
        super(context, theme);
        this.context = context;

        this.url = url;

    }


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


        if (!isLandscape){

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

        }else{
            setContentView(ResourceUtil.getLayoutId(context,
                    "hnyy_email_dialog"));
        }


        setCancelable(false);



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

        close_btn = findViewById(ResourceUtil.getId(context,
                "close_btn"));



        close_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                webView.destroy();
                dismiss();

            }
        });

        webSettings = webView.getSettings();
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setBuiltInZoomControls(false);// 设置支持缩放
        webSettings.setDomStorageEnabled(true);
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示

        webView.addJavascriptInterface(new PageJsInteraction(this), "yyrhsdk");
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {




                if (url.startsWith("http:") || url.startsWith("https")) {
                    return super.shouldOverrideUrlLoading(view, url);
                } else if (url.startsWith("mqqwpa://")) {

                    Intent intent = null;
                    try {
                        intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                        context.startActivity(intent);
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }

                    webView.goBack();
                    return true;

                }else{
                    return true;
                }
            }

        });

        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.setBackgroundColor(0);
        webView.getBackground().setAlpha(0);


        webView.loadUrl(url);
    }






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

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



    @Override
    public void openq0q0(String url) {
        super.openq0q0(url);

        {
            Log.i("openq0q0", url);
            if (Utils.judgeStrNull(url)) {
                return;
            }
            if (url.equals("mqqwpa://")) {
                try {

                    Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
                    context.startActivity(intent);

                } catch (Exception e) {
                    e.printStackTrace();

                }
            } else {
                Intent intent = null;
                try {
                    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                    context.startActivity(intent);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }

            }


        }
    }

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