package com.sheep.gamegroup.view.activity;

import android.text.TextUtils;
import android.view.View;
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 android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.android.view.widget.SafeWebView;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.AboutUsEntity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.WebviewEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ljy on 2018/4/16.
 */

public class WebviewAct extends BaseActivity {

    @BindView(R.id.activity_webview)
    SafeWebView activityWebview;
    @BindView(R.id.title_name_tv)
    TextView title_name_tv;
    private WebviewEntity webviewEntity;

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

    @Override
    public void initView() {
        webviewEntity = (WebviewEntity) getIntent().getSerializableExtra("webview_entity");
        if(webviewEntity == null){
            return;
        }
        TitleBarUtils.getInstance()
                .setShowOrHide(this, true)
                .setTitleFinish(this)
                .setTitle(this,
                        TextUtils.isEmpty(webviewEntity.getTitle()) ?"专属福利":webviewEntity.getTitle());

        WebSettings webSettings = activityWebview.getSettings();

        //允许webview对文件的操作
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        //用于js调用Android
        webSettings.setJavaScriptEnabled(true);
        //设置编码方式
        webSettings.setDefaultTextEncodingName("utf-8");
//        activityWebview.setWebChromeClient(new chromClient());



        activityWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }
    private class chromClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if(newProgress==100){
                //页面加载完成执行的操作
                String path= "file:///android_asset/" + "home_sheep_main_img.png";
                String action="javascript:imgpath('"+path+"')";
                runWebView(action);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    private void runWebView(final String url){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                activityWebview.loadUrl(url);
            }
        });
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        //封装头文件
        final String sHead=   "<html><head><meta name=\"viewport\" content=\"width=device-width, " +
                "initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes\" />"+
                "<style>img{max-width:100% !important;height:auto !important;}</style>"
                +"<style>body{max-width:100% !important;}</style>"+"</head><body>";
        switch (webviewEntity.getType()){
            case 0:
            default:
                title_name_tv.setVisibility(View.GONE);
                activityWebview.loadUrl(webviewEntity.getUrl()+"");
                break;
            case 1://关于我们
                showProgress(true);
                SheepApp.getInstance()
                        .getNetComponent()
                        .getApiService()
                        .aboutUs()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(this) {
                            @Override
                            public void onError(BaseMessage baseMessage) {
                                hideProgress();
                                G.showToast(baseMessage.getMsg() + "");
                            }

                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                hideProgress();
                                AboutUsEntity aboutUsEntity = JSONObject.parseObject(JSONObject.toJSONString(baseMessage.getData()), AboutUsEntity.class);
                                if(aboutUsEntity != null){
                                    title_name_tv.setVisibility(View.VISIBLE);
                                    title_name_tv.setText(aboutUsEntity.getTitle()+"");
                                    activityWebview.loadDataWithBaseURL(
                                            null,
                                            sHead + aboutUsEntity.getContent() + "</body></html>",
                                            "text/html",
                                            "utf-8",
                                            null);
                                }

                            }
                        });

                break;
        }

    }

    @Override
    protected void onDestroy() {
        if(activityWebview != null){
            activityWebview.destroy();
        }
        super.onDestroy();
    }
}
