package cn.yyxx.commsdk.platform.hnyy.dialog;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
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 java.net.URISyntaxException;

import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.commsdk.platform.hnyy.action.PageJsInteraction;
import cn.yyxx.commsdk.platform.hnyy.fragment.widget.TosUtil;
import cn.yyxx.support.AppUtils;


public class UserCenterDialog extends JsBaseDialog {


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

    private String url;
    public OnClosePageListener listener;

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


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

        this.url = url;

    }

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

        this.url = url;
        this.listener = listener;
    }
    public UserCenterDialog(Context context, int theme, String url, OnClosePageListener listener, boolean isHiddenClose) {
        super(context, theme);
        this.context = context;

        this.url = url;
        this.listener = listener;

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

        if (!AppUtils.isLandscape(context)){
            setContentView(ResourceUtil.getLayoutId(context,
                    "hnyy_usercenter_portait_dialog"));
            getWindow().getAttributes().gravity = Gravity.CENTER;
        }else{
            setContentView(ResourceUtil.getLayoutId(context,
                    "hnyy_usercenter_dialog"));
            getWindow().getAttributes().gravity = Gravity.LEFT;
        }


        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) {

                if (listener != null){
                    listener.onClose();
                }

                webView.destroy();
                dismiss();

            }
        });

        loading = findViewById(ResourceUtil.getId(context, "hnyy_loading"));
        Animation rotateAnimation = AnimationUtils.loadAnimation(context, ResourceUtil.getAnimId(context, "hnyy_rotate_anim"));
        loading.startAnimation(rotateAnimation);

        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);
                loading.clearAnimation();
                loading.setVisibility(View.GONE);
            }

            @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);
    }




    public interface OnClosePageListener {
         void onClose();
         void onDissmiss();
         void onRealNameSuccess(String birthday);
    }



    @Override
    public void zfColse() {
        super.zfColse();
        if (listener != null){
            listener.onClose();
        }
        dismiss();
    }


    @Override
    public void modifyPwd(String message) {
        super.modifyPwd(message);
     }

    @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 (TextUtils.isEmpty(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 switchAccount() {
        super.switchAccount();
        dismiss();

    }



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