package com.taikor.investment.modules.find;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.google.gson.Gson;
import com.taikor.investment.R;
import com.taikor.investment.base.BaseActivity;
import com.taikor.investment.bean.MarketDesc;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.CommonUtils;
import com.taikor.investment.utils.Constant_URL;
import com.taikor.investment.utils.TimeUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 市场波动解析详情
 * Created by Any on 2017/4/5.
 */
public class MarketDescActivity extends BaseActivity {
    
    @BindView(R.id.tv_market_title)
    TextView tvMarketTitle;//市场解析详情
    @BindView(R.id.tv_market_time)
    TextView tvMarketTime;//时间
    @BindView(R.id.tv_bar_title)
    TextView tvBarTitle;//标题
    @BindView(R.id.wv_market)
    WebView wvMarket;//内容
    @BindView(R.id.tv_reload)
    TextView tvReload;

    private String fluctuationID;
    private ProgressDialog progressDialog;

    @Override
    public int getContentViewId() {
        return R.layout.activity_find_market_desc;
    }

    @Override
    public void initView() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.progress_hint));
        progressDialog.setCancelable(true);
        tvBarTitle.setText("市场解析详情");
        Intent intent = getIntent();
        fluctuationID = intent.getStringExtra("fluctuationID");
    }

    @OnClick({R.id.tv_bar_back,R.id.tv_reload})
    public void click(View view){
        switch (view.getId()){
            case R.id.tv_bar_back:
                finish();
                break;
            case R.id.tv_reload:
                getData(Constant_URL.MARKET_DESC);
                break;
        }
    }
    
    @Override
    public void initData() {
        getData(Constant_URL.MARKET_DESC);
    }

    //获取详情数据
    public void getData(String url) {
        progressDialog.show();
        OkHttpUtils.get().url(url)
                .addParams("fluctuationID", fluctuationID)
                .build().execute(new Callback<MarketDesc>() {

            @Override
            public MarketDesc parseNetworkResponse(Response response, int id) throws Exception {
                Gson gson = new Gson();
                return gson.fromJson(response.body().string(), MarketDesc.class);
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                progressDialog.dismiss();
                tvReload.setVisibility(View.VISIBLE);
            }

            @Override
            public void onResponse(MarketDesc response, int id) {
                progressDialog.dismiss();
                tvReload.setVisibility(View.GONE);
                if (response == null) {
                    return;
                }
                tvMarketTitle.setText(response.getTitle());
                tvMarketTime.setText(TimeUtils.getStringTime(response.getPubdate()));
                String html = response.getText();
                if (html.contains("src=\"/p")) {
                    //处理带图片的内容
                    html = html.replace("src=\"/p", "src=\"http://car.zhuji.net/p");
                }
                showWebView(html);
            }
        });
    }

    private void showWebView(String html_string) {
        WebSettings settings = wvMarket.getSettings();
        settings.setJavaScriptEnabled(true); //设置支持Javascript
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);  //将图片缩放至屏幕宽度
        settings.setLoadWithOverviewMode(true);

        wvMarket.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return true;
            }
        });
        wvMarket.loadDataWithBaseURL(null, CommonUtils.getNewContent(html_string), "text/html", "utf-8", null);
    }

   //处理返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wvMarket.canGoBack()) {
            wvMarket.goBack();//返回webView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    //埋点
    @Override
    protected void onResume() {
        super.onResume();
        TAnalytics.onPageStart("marketPage");
    }

    @Override
    protected void onPause() {
        super.onPause();
        TAnalytics.onPageLoad("marketPage","homePage","","");
    }
}
