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.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.LinearLayout;

import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.support.AppUtils;

public class PrivacyPolicyDialog extends Dialog {

    private Context context;
    OnClosePageListener listener;
    String url;
    private Button btn_disagree,btn_agree;

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

    }

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

        setContentView(ResourceUtil.getLayoutId(context,
                "yyxx_comm_privacy_dialog"));
        boolean  isLandscape = AppUtils.isLandscape(context);;


         WebView webView;
         WebSettings webSettings;

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




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

        });

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