package com.bc_chat.bc_base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.abs.contract.BaseContract;

/**
 * author：Created by ZHT on 2016/8/25 10:55
 * email：526309416@qq.com
 * desc：WebView网页
 */
public class WebViewActivity extends BaseActivity {

    protected static final String TAG = WebViewActivity.class
            .getSimpleName();

//    private RelativeLayout mProgress;

    private String urlOrContent;

    private WebView mWebView;
    private ProgressBar progressBar;
    private int type;
    //    private TextView tv_progress;


    public static void startActivity(Activity activity, String urlOrContent, String title, int type) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra("urlOrContent", urlOrContent);
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }

    @Override
    protected BaseContract.BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initialize() {
        mWebView = (WebView) findViewById(R.id.wv_webview);
//        mProgress = (RelativeLayout) findViewById(R.id.pb_advert_detail);
//        tv_progress = (TextView) findViewById(R.id.tv_progress);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        urlOrContent = getIntent().getStringExtra("urlOrContent");
        titleView.setText(getIntent().getStringExtra("title"));
        type = getIntent().getIntExtra("type", 0);
        initView();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {

        WebSettings settings = mWebView.getSettings();
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setJavaScriptEnabled(true);// 打开js功能
//        settings.setBuiltInZoomControls(false);// 显示放大缩小的按钮
//        settings.setUseWideViewPort(false);// 双击缩放
        mWebView.setWebViewClient(new WebViewClient() {

            // 监听网页加载结束的事件
            @Override
            public void onPageFinished(WebView view, String url) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    progressBar.setVisibility(View.GONE);
                } else {
                    // 加载中
                    progressBar.setProgress(newProgress);
//                        tv_progress.setText(newProgress+"%");
                }
            }
        });
//            mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//优先使用缓存
        if (type == 0) {
            mWebView.loadUrl(urlOrContent);
        } else {
            mWebView.loadDataWithBaseURL(null, urlOrContent, "text/html", "utf-8", null);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mWebView.onPause();
        mWebView.pauseTimers();
    }

    @Override
    public void onResume() {
        super.onResume();
        mWebView.resumeTimers();
        mWebView.onResume();
    }
//    /**
//     * 防止webview缩放时退出崩溃。
//     */
//    @Override
//    public void finish() {
//        ViewGroup view = (ViewGroup) getWindow().getDecorView();
//        view.removeAllViews();
//        super.finish();
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
        mWebView = null;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_webpage;
    }
}
