package com.sheep.gamegroup.view.activity;

import android.os.Bundle;
import android.view.View;

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.ButterKnife;
import butterknife.OnClick;

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


/**
 * all web view
 * Created by HooRang on 2017/2/17.
 */
public class ActGuideOnHook extends BaseActivity {

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

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

        ButterKnife.bind(this);
        TitleBarUtils
                .getInstance()
                .setTitle(this, extraTitle)
                .setTitleFinish(this);

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


}
