package com.sheep.gamegroup.view.activity;

import android.view.View;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sheep.gamegroup.util.Jump2View.EXTRA_WEBVIEW_TITLE;
import static com.sheep.gamegroup.util.Jump2View.EXTRA_WEBVIEW_URL;


/**
 * Created by realicing on 2018/3/23.
 * realicing@sina.com
 * 了解微信小号的引导界面
 */
public class ActGuideDeblocked extends BaseActivity {

    @BindView(R.id.webview)
    WebView mWebView;
    @BindView(R.id.tv_next)
    TextView tv_next;

    private String extraUrl;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_guide_on_hook;
    }

    @Override
    public void initView() {
        extraUrl = getIntent().getStringExtra(EXTRA_WEBVIEW_URL);
        String extraTitle = getIntent().getStringExtra(EXTRA_WEBVIEW_TITLE);

        TitleBarUtils
                .getInstance()
                .setTitle(this, extraTitle)
                .setTitleFinish(this);
        tv_next.setVisibility(View.GONE);
        WebSettings webViewSettings = mWebView.getSettings();
        webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        loadUrl();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mWebView != null){
            mWebView.setVisibility(View.GONE);
            mWebView.removeAllViews();
            mWebView.destroy();
        }
    }

    private void loadUrl() {
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try {
                    view.loadUrl(url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
        });
        mWebView.loadUrl(extraUrl);
    }

    @OnClick({R.id.tv_next})
    public void onClick(View v) {
        int resId = v.getId();
        switch (resId) {
            case R.id.tv_next:
                Jump2View.getInstance().goCommitWxView(this, null);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()){
            mWebView.goBack();
            return;
        }
        this.finish();
    }


}
