package com.sheep.gamegroup.view.activity;

import android.os.Build;
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 android.widget.TextView;

import com.kfzs.duanduan.BaseCompatActivity;
import com.sheep.gamegroup.model.entity.CreditCard;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

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

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

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

    private CreditCard creditCard;
    private WebSettings webViewSettings;

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

        creditCard = (CreditCard) getIntent().getSerializableExtra(CreditCard.class.getSimpleName());

        ButterKnife.bind(this);
        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);
        }
        if (getIntent().hasExtra(EXTRA_WEBVIEW_NO_TITLE)) {//不显示标题
            findViewById(R.id.appbar_act_webview).setVisibility(View.GONE);
        }
        tv_next.setVisibility(View.GONE);
        initJsData();
    }

    private void initJsData() {
        OkHttpUtils.get()
                .url(creditCard.getScriptUrl())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        G.showToast( "服务器配置错误，请联系客服");
                    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        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();
            }
        });
        webViewSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl(creditCard.getLoadUrl());
    }

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