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;


    @Override
    protected void initView() {
        ButterKnife.bind(this);
        WebSettings wSet = wvContent.getSettings();
        wSet.setJavaScriptEnabled(true);
        wSet.setDomStorageEnabled(true);
//        wSet.setAllowFileAccess(true);
//        wSet.setAppCacheEnabled(true);

    }
    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(, "text/html", "utf-8");
                wvContent.loadDataWithBaseURL(null,dataBean.getString("content"), "text/html",  "utf-8", null);
            }
        });
    }

    @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);
    }
}
