package com.taikor.investment.modules.find;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
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.HotNewDesc;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.CommonUtils;
import com.taikor.investment.utils.TimeUtils;
import com.taikor.investment.utils.Constant_URL;
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 HotNewsDescActivity extends BaseActivity {
    
    @BindView(R.id.tv_bar_title)
    TextView tvBarTitle;//标题
    @BindView(R.id.tv_news_title)
    TextView tvNewsTitle;//热点资讯标题
    @BindView(R.id.tv_news_date)
    TextView tvNewsDate;//时间
    @BindView(R.id.wv_news)
    WebView wvNews;//内容
    @BindView(R.id.tv_tag1)
    TextView tvTag1;//标签1
    @BindView(R.id.tv_tag2)
    TextView tvTag2;//标签2
    @BindView(R.id.tv_reload)
    TextView tvReload;

    private String newsID;
    private ProgressDialog progressDialog;

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

    @Override
    public void initView() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.progress_hint));
        progressDialog.setCancelable(true);
        tvBarTitle.setText("热点资讯详情");
        Intent intent = getIntent();
        newsID = intent.getStringExtra("newsID");
    }

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

    @Override
    public void initData() {
        getData(Constant_URL.NEWS_DESC);
    }

    //请求数据
    public void getData(String url) {
        progressDialog.show();
        OkHttpUtils.get().url(url)
                .addParams("newsID", newsID)
                .build().execute(new Callback<HotNewDesc>() {

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

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

            @Override
            public void onResponse(HotNewDesc response, int id) {
                progressDialog.dismiss();
                tvReload.setVisibility(View.GONE);
                if (response == null) {
                    return;
                }
                String title = response.getTitle();
                if (!TextUtils.isEmpty(title)) {
                    tvNewsTitle.setText(title);
                }
                String strTime = TimeUtils.getStringTime(response.getPubdate());
                if (!TextUtils.isEmpty(strTime)) {
                    tvNewsDate.setText(strTime);
                }
                //获取对象
                String tag = response.getTag();
                if (!TextUtils.isEmpty(tag)) { //切割字符串
                    String[] split = tag.split(",");
                    if (split.length == 1) {
                        String[] split1 = split[0].split(":");
                        tvTag1.setText(split1[0]);
                        tvTag1.setBackgroundResource(R.drawable.bg_text_gray);
                        tvTag2.setVisibility(View.GONE);
                    }
                    if (split.length == 2) {
                        String[] split1 = split[0].split(":");
                        String[] split2 = split[1].split(":");
                        tvTag1.setText(split1[0]);
                        tvTag1.setBackgroundResource(R.drawable.bg_text_gray);
                        tvTag2.setText(split2[0]);
                        tvTag2.setBackgroundResource(R.drawable.bg_text_gray);
                    }
                } else {
                    tvTag1.setVisibility(View.GONE);
                    tvTag2.setVisibility(View.GONE);
                }

                String html = response.getText();
                if (html.contains("src=\"/p")) {
                    //处理带图片的内容
                    html = html.replace("src=\"/p", "src=\"http://car.zhuji.net/p");
                }
                showWebView(html);
            }
        });

    }

    /**
     * 设置WevView要显示的网页
     *
     * @param html_string html字符串
     */
    private void showWebView(String html_string) {
        WebSettings settings = wvNews.getSettings();
        settings.setJavaScriptEnabled(true);//与JS脚本可交互
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //自适应屏幕  
        settings.setLoadWithOverviewMode(true);

        wvNews.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;
            }
        });
        wvNews.loadDataWithBaseURL(null, CommonUtils.getNewContent(html_string), "text/html", "utf-8", null);

    }

    //处理返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wvNews.canGoBack()) {
            wvNews.goBack();//返回webView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //埋点
    @Override
    protected void onResume() {
        super.onResume();
        TAnalytics.onPageStart("hotPage");
    }

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