package com.dahe.pass.web;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dahe.mylibrary.base.BaseActivity;
import com.dahe.pass.R;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.AgentWebUIControllerImplBase;
import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;

/**
 * 公用的webview
 */
public class WebActivity extends BaseActivity {
    TextView mTextRight;
    FrameLayout mWebViewLayout;
    private AgentWeb mAgentWeb;
    private String mTitle = "";
    private String mUrl = "";
    private String mDescription = "";
    private static final String TAG = "WebActivity";

    @Override
    protected int getLayout() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitle = getIntent().getExtras().getString("title");
        mUrl = getIntent().getExtras().getString("url");
        mDescription = getIntent().getExtras().getString("description");
        mTextRight = (TextView) findViewById(R.id.common_toolBar_text_right);
        mWebViewLayout = (FrameLayout) findViewById(R.id.WebViewLayout);
    }

    @Override
    protected void initDate() {
        setStatusBarColor();
        setTitleBar(mTitle, v -> {
            if (!mAgentWeb.back()) finish();
        });
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mWebViewLayout, new FrameLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .setWebViewClient(mWebViewClient)
                .setWebChromeClient(mWebChromeClient)
                .setSecurityType(AgentWeb.SecurityType.DEFAULT_CHECK)
                .setAgentWebUIController(new AgentWebUIControllerImplBase())
                .createAgentWeb()
                .ready()
                .go(mUrl);


        mAgentWeb.getJsInterfaceHolder().addJavaObject("phone", new AndroidInterfaceWeb());
    }


    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            setTitleBar(TextUtils.isEmpty(mTitle) ?
                    title : mTitle, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!mAgentWeb.back()) finish();
                }
            });
        }
    };

    private WebViewClient mWebViewClient = new WebViewClient() {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }


        //        @Override
//        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//            //该方法在Build.VERSION_CODES.LOLLIPOP以后有效
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                String url = request.getUrl().toString();
//                Log.i(TAG, "shouldOverrideUrlLoading: " + url);
//            }
//            return false;
//        }
    };

//    @OnClick({R.id.common_toolBar_text_right, R.id.common_toolBar_image_right})
//    public void onClick(View view) {
//        if (view.getId() == R.id.common_toolBar_text_right) {
//        } else if (view.getId() == R.id.common_toolBar_image_right) {
//        }
//    }

    @Override
    public void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    public void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }

    class AndroidInterfaceWeb {
        @JavascriptInterface
        public void gohome() {
//            Intent intent = new Intent(mContext, HomeActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//            mContext.startActivity(intent);
        }
    }
}
