package com.wd.workoffice.ui.activity;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;

import butterknife.BindView;
import butterknife.ButterKnife;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * Created by flexible on 2016/8/21 0021.
 */
public class NoticeDetailActivity extends WorkToolBarActivity {

    @BindView(R.id.wv_content)
    WebView wvContent;
    private boolean loadingFinished;
    private boolean redirect;


    @Override
    protected void initView() {
        ButterKnife.bind(this);
        WebSettings wSet = wvContent.getSettings();
        wSet.setJavaScriptEnabled(true);
        wSet.setDomStorageEnabled(true);
//        wSet.setAllowFileAccess(true);
//        wSet.setAppCacheEnabled(true);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            WebView.setWebContentsDebuggingEnabled(true);
//
//            android.webkit.WebView.setWebContentsDebuggingEnabled(true);
//
//        }
//        wvContent.setWebChromeClient(new WebChromeClient() {
//
//            @Override
//            public void onProgressChanged(WebView webView, int i) {
//
//                if (i == 100) {
//                    Map<String, Object> param = new HashMap<>();
//                    param.put("token", UserKeeper.getInstance().getToken());
//                    wvContent.loadUrl("javascript:postToken(" + JSON.toJSONString(param) + ")");
//                }
//                super.onProgressChanged(webView, i);
//            }
//        });

        wvContent.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
//                if (!loadingFinished) {
//                    redirect = true;
//                }

//                loadingFinished = false;
//                LogUtils.LOGE("bb", urlNewString);
//                String tag="tel";

//                if (urlNewString.contains(tag)) {
//                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(urlNewString));
//                    startActivity(intent);
//                    //这个超连接,java已经处理了，webview不要处理了
//                    return true;
//                }
                wvContent.loadUrl(urlNewString);
                return true;
            }

            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
//                loadingFinished = false;
            }


            @Override
            public void onPageFinished(WebView view, String url) {
//                wvContent.addJavascriptInterface(this, "android");
//                if (!redirect) {
//                    loadingFinished = true;
//                }
//
//                if (loadingFinished && !redirect) {
//                    //HIDE LOADING IT HAS FINISHED
//                    Map<String, Object> param = new HashMap<>();
//                    param.put("token", UserKeeper.getInstance().getToken());
//                    wvContent.loadUrl("javascript:postToken(" + JSON.toJSONString(param) + ")");
//                } else {
//                    redirect = false;
//                }
            }
        });
//        wvContent.setWebViewClient(new WebViewClient() {
//            public boolean shouldOverrideUrlLoading(WebView view, String url) { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
//                view.loadUrl(url);
//                return true;
//            }
//
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                Map<String, Object> param = new HashMap<>();
//                param.put("token", UserKeeper.getInstance().getToken());
//                wvContent.loadUrl("javascript:postToken(" + JSON.toJSONString(param) + ")");
//                super.onPageFinished(view, url);
//            }
//        });

    }
    private void getData() {
        RtfUtils.getRtf().noticeDetail(getIntent().getStringExtra("id")).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                JSONObject dataBean = JSON.parseObject(data.getData().toString());
                wvContent.loadData(dataBean.getString("content"), "text/html", "utf-8");
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }


    @Override
    protected void initEvent() {

    }

    @Override
    protected int layoutId() {
        return R.layout.activity_webview;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (wvContent.canGoBack()) {
                    wvContent.goBack();
                    return true;
                }
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //这是一个监听用的按键的方法，keyCode 监听用户的动作，如果是按了返回键，同时Webview要返回的话，WebView执行回退操作，因为mWebView.canGoBack()返回的是一个Boolean类型，所以我们把它返回为true
        if (keyCode == KeyEvent.KEYCODE_BACK && wvContent.canGoBack()) {
            wvContent.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
