package com.sheep.gamegroup.view.activity;

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

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.CreditCard;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.util.WebViewJs;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;
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 com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;


/**
 * 信用卡申请web
 * Created by zengjiebin on 2018/4/25.
 */
public class ActCreditCardWeb extends BaseActivity {

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

    private CreditCard creditCard;
    private WebSettings webViewSettings;

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

    @Override
    public void initView() {
        creditCard = (CreditCard) getIntent().getSerializableExtra(CreditCard.class.getSimpleName());

        if(creditCard == null) {
            G.showToast("数据错误");
            finish();
            return;
        }
        TitleBarUtils
                .getInstance()
                .setTitle(this, creditCard.getName()+"-申请流程")
                .setTitleFinish(this);

        webViewSettings = mWebView.getSettings();
        webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            webViewSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        tv_next.setVisibility(View.GONE);
        initJsData();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    private void initJsData() {
        OkHttpUtils.get()
                .url(creditCard.getScriptUrl())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        G.showToast( R.string.service_data_error);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        js = response;
                        loadUrl();
                    }
                });
    }

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

    private void loadUrl() {
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                TitleBarUtils
                        .getInstance()
                        .setTitle(ActCreditCardWeb.this, title);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try {
                    view.loadUrl(url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                loadJs();
//                loadTestJs();
            }
        });

        // 开启DOM缓存，开启LocalStorage存储（html5的本地存储方式）
        webViewSettings.setDomStorageEnabled(true);
        webViewSettings.setDatabaseEnabled(true);
        webViewSettings.setDatabasePath(getApplicationContext().getCacheDir().getAbsolutePath());

        webViewSettings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new WebViewJs(creditCard.getRelease_id()), "kfzsjs");
        mWebView.loadUrl(creditCard.getLoadUrl());
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()){
            mWebView.goBack();
            return;
        }
        this.finish();
    }
    // 注入js函数监听
    public void loadJs() {
        ViewUtil.loadJs(mWebView, getJs());
    }
    private String js;
    private String getJs(){
        return js;
    }
}
