package com.taikor.investment.modules.find;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.taikor.investment.R;
import com.taikor.investment.adapter.RandomReportAdapter;
import com.taikor.investment.adapter.ReportAdapter;
import com.taikor.investment.base.BaseFragment;
import com.taikor.investment.bean.Report;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.Constant_URL;
import com.taikor.investment.view.SimpleListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

/**
 * 默认显示的报告
 * Created by Any on 2017/4/11.
 */
public class DefaultReportFragment extends BaseFragment
        implements AdapterView.OnItemClickListener, SimpleListView.OnLoadListener {

    @BindView(R.id.lv_random_report)
    ListView lvRandomReport;//随机报告
    @BindView(R.id.slv_type_report)
    SimpleListView slvTypeReport;//分类报告
    @BindView(R.id.rb_product_investment)
    RadioButton rbProductInvestment;//产品投资
    @BindView(R.id.rb_company_advice)
    RadioButton rbCompanyAdvice;//公司观点
    @BindView(R.id.rb_market)
    RadioButton rbMarket;//市场总览

    private int mPage = 1;
    private String type = "1";
    private ReportAdapter typeReportAdapter;
    private RandomReportAdapter randomReportAdapter;
    private List<Report> typeReportList = new ArrayList<>(), randomReportList = new ArrayList<>();

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

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        randomReportAdapter = new RandomReportAdapter(getActivity());
        lvRandomReport.setAdapter(randomReportAdapter);
        lvRandomReport.setOnItemClickListener(this);

        typeReportAdapter = new ReportAdapter(getActivity());
        slvTypeReport.setAdapter(typeReportAdapter);
        slvTypeReport.setOnItemClickListener(this);
        slvTypeReport.setOnLoadListener(this);
    }

    @Override
    public void initData() {
        getReportData(Constant_URL.REPORT, type, mPage);
    }

    //获取研究报告
    public void getReportData(String url, String type, final int page) {
        OkHttpUtils.get().url(url)
                .addParams("type", type)
                .addParams("count", "5")
                .addParams("skip", String.valueOf(5 * (page - 1)))
                .build().execute(new Callback<List<Report>>() {
            @Override
            public List<Report> parseNetworkResponse(Response response, int id) throws Exception {
                Gson gson = new Gson();
                return gson.fromJson(response.body().string(), new TypeToken<List<Report>>() {
                }.getType());
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(List<Report> response, int id) {
                if (response == null) {
                    return;
                }
                if (response.size() == 0) {
                    slvTypeReport.finishLoad(true);//显示没有更多的数据
                    return;
                } else {
                    //前三条研究报告
                    if (randomReportList.size() == 0) {//添加前三条
                        for (int i = 0; i < 3; i++) {
                            randomReportList.add(randomReportList.size(), response.get(i));
                        }
                        randomReportAdapter.setData(randomReportList, true); //不需要分页加载
                    }
                    if (page == 1) {//更新数据
                        typeReportList.clear();
                    }
                    typeReportList.addAll(response);
                }
                typeReportAdapter.setData(response, page == 1);
                if(slvTypeReport!=null){
                    slvTypeReport.finishLoad(page == 20);//加载更多
                }
            }
        });
    }

    @OnClick({R.id.rb_product_investment, R.id.rb_company_advice, R.id.rb_market})
    public void click(View view) {

        switch (view.getId()) {
            case R.id.rb_product_investment:
                type = "1";
                getReportData(Constant_URL.REPORT, type, 1);
                break;
            case R.id.rb_company_advice:
                type = "2";
                getReportData(Constant_URL.REPORT, type, 1);
                break;
            case R.id.rb_market:
                type = "3";
                getReportData(Constant_URL.REPORT, type, 1);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.slv_type_report:
                Intent intent2 = new Intent(getActivity(), ReportDescActivity.class);
                intent2.putExtra("fileId", typeReportList.get(position).getFileId());
                intent2.putExtra("fileName", typeReportList.get(position).getFileName());
                startActivity(intent2);
                break;
            case R.id.lv_random_report:
                String fileId = randomReportList.get(position).getFileId();
                //埋点
                Map<String, String> map = new HashMap<>();
                map.put("newsID", fileId);
                if (rbProductInvestment.isChecked()) {
                    map.put("type", "3");
                } else if (rbCompanyAdvice.isChecked()) {
                    map.put("type", "4");
                } else if (rbMarket.isChecked()) {
                    map.put("type", "5");
                }
                Gson json = new Gson();
                TAnalytics.onClick("reportRead",json.toJson(map));
                //跳转
                Intent intent = new Intent(getActivity(), ReportDescActivity.class);
                intent.putExtra("fileId", fileId);
                intent.putExtra("fileName", randomReportList.get(position).getFileName());
                startActivity(intent);
                break;
        }

    }

    /**
     * @param isRefresh true为下拉刷新 false为加载更多
     */
    @Override
    public void onLoad(boolean isRefresh) {
        if (isRefresh) {
            mPage = 1;
        } else {
            mPage++;
        }
        switch (type) {
            case "1":
                getReportData(Constant_URL.REPORT, "1", mPage);
                break;
            case "2":
                getReportData(Constant_URL.REPORT, "2", mPage);
                break;
            case "3":
                getReportData(Constant_URL.REPORT, "3", mPage);
                break;
        }
    }

}
