package com.sheep.gamegroup.view.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kfzs.duanduan.BaseCompatActivity;
import com.kfzs.duanduan.bean.KFIntentKeys;
import com.orhanobut.logger.Logger;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.kfzs.duanduan.bean.KFIntentKeys.EXTRA_WEBVIEW_NO_TITLE;

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

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

    private String extraUrl;
    private String extraTitle;
    private WebSettings webViewSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_on_hook);

        extraUrl = getIntent().getStringExtra(KFIntentKeys.EXTRA_WEBVIEW_URL);
        extraTitle = getIntent().getStringExtra(KFIntentKeys.EXTRA_WEBVIEW_TITLE);

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

        webViewSettings = mWebView.getSettings();
        webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        if (getIntent().hasExtra(EXTRA_WEBVIEW_NO_TITLE)) {//不显示标题
            findViewById(R.id.appbar_act_webview).setVisibility(View.GONE);
        }
        loadUrl();
    }

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

    private void loadUrl() {
        Logger.d("loadUrl: " + extraUrl);
        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();
    }


}
