package com.yyrh.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
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.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.yyrh.utils.ResourceUtil;

import com.yyrh.utils.Utils;


public class PrivacyPolicyDialog extends Dialog {


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

    private Button btn_disagree,btn_agree;
    private String url;
    public OnClosePageListener listener;

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


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

        this.url = url;

    }

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

        this.url = url;
        this.listener = listener;
    }
    public PrivacyPolicyDialog(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);
        boolean isLandscape = Utils.isLandscape((Activity) context);
        setContentView(ResourceUtil.getLayoutId(context,
                "hnyy_privacy_policy_dialog"));
        if (!isLandscape){
            LinearLayout ll_private_policy = findViewById(ResourceUtil.getId(context, "ll_private_policy"));
             ll_private_policy.setScaleX(0.9f);
            ll_private_policy.setScaleY(0.9f);
        }

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



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


        btn_disagree = findViewById(ResourceUtil.getId(context, "btn_disagree"));
        btn_agree = findViewById(ResourceUtil.getId(context, "btn_agree"));

        setCancelable(false);

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

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

                webView.destroy();
                dismiss();

            }
        });
        btn_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

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

                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.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                yyrh_loading.clearAnimation();
                yyrh_loading.setVisibility(View.GONE);
                if (webView.canGoBack()){

                }
            }

        });

        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 onUserAgreePolicy();
         void onUserDisagreePolicy();
    }

}
