package com.mq.oaapp.ui;

import android.view.View;
import android.webkit.CookieSyncManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.mq.oaapp.R;
import com.mq.oaapp.base.BaseActivity;
import com.mq.oaapp.utils.X5WebView;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebViewClient;
import com.xyxsbj.mylibrary.tool.LogTool;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 创建人 mQ
 * 创建时间 2019/7/4 0004 17:44
 * 文件名称 WebActivity
 * 说明: web页面
 **/
public class WebActivity extends BaseActivity {


    @BindView(R.id.bar_back)
    ImageView mBarBack;
    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;
    @BindView(R.id.webView)
    X5WebView mWebView;
    @BindView(R.id.btnBack)
    ImageButton mBtnBack;
    @BindView(R.id.btnForward)
    ImageButton mBtnForward;
    @BindView(R.id.btnHome)
    ImageButton mBtnHome;
    @BindView(R.id.bottom_layout)
    LinearLayout mBottomLayout;

    private final int disable = 120;
    private final int enable = 255;

    private String mHomeUrl = "http://www.baidu.com";

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

    @Override
    protected void init() {
        setStatusHeightForContent();
        mProgressBar.setMax(100);
        initWebView();
    }

    @Override
    protected void workTask() {
        mWebView.loadUrl(mHomeUrl);
//        mWebView.loadData(mHomeUrl, "text/html", "UTF-8");
        mBtnHome.setEnabled(false);
        mBtnHome.setAlpha(disable);
    }


    private void initWebView() {

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView webView, String s) {
                return super.shouldOverrideUrlLoading(webView, s);
            }

            @Override
            public void onPageFinished(com.tencent.smtt.sdk.WebView webView, String s) {
                super.onPageFinished(webView, s);
                changGoForwardButton(webView);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(com.tencent.smtt.sdk.WebView webView, int i) {
                super.onProgressChanged(webView, i);
                if (i == 100) {
                    mProgressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mProgressBar.setProgress(i);//设置进度值
                }
            }
        });

        com.tencent.smtt.sdk.WebSettings webSetting = mWebView.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setAppCachePath(getDir("appcache", 0).getPath());
        webSetting.setDatabasePath(getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(getDir("geolocation", 0)
                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(com.tencent.smtt.sdk.WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        // webSetting.setPreFectch(true);

        CookieSyncManager.createInstance(getContext());
        CookieSyncManager.getInstance().sync();
    }

    private void changGoForwardButton(com.tencent.smtt.sdk.WebView view) {
        if (view.canGoBack())
            mBtnBack.setAlpha(enable);
        else
            mBtnBack.setAlpha(disable);
        if (view.canGoForward())
            mBtnForward.setAlpha(enable);
        else
            mBtnForward.setAlpha(disable);
        LogTool.i("changGoForwardButton加载地址: " + view.getUrl());
        if (view.getUrl() != null && view.getUrl().equalsIgnoreCase(mHomeUrl)) {
            mBtnHome.setAlpha(disable);
            mBtnHome.setEnabled(false);
        } else {
            mBtnHome.setAlpha(enable);
            mBtnHome.setEnabled(true);
        }
    }

    @OnClick({R.id.bar_back, R.id.btnBack, R.id.btnForward, R.id.btnHome})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                onBackPressed();
                break;
            case R.id.btnBack:
                if (mWebView != null && mWebView.canGoBack())
                    mWebView.goBack();
                break;
            case R.id.btnForward:
                if (mWebView != null && mWebView.canGoForward())
                    mWebView.goForward();
                break;
            case R.id.btnHome:
                workTask();
                break;
        }
    }
}
