package com.taikor.investment.modules.find;

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

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.taikor.investment.R;
import com.taikor.investment.adapter.ReportAdapter;
import com.taikor.investment.base.BaseFragment;
import com.taikor.investment.bean.Report;
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.List;

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

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

    @BindView(R.id.slv_report)
    SimpleListView slvReport;

    private int mPage = 1;
    private ReportAdapter reportAdapter;
    private List<Report> reportList = new ArrayList<>();

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

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        reportAdapter = new ReportAdapter(getActivity());
        slvReport.setAdapter(reportAdapter);
        slvReport.setOnItemClickListener(this);
        slvReport.setOnLoadListener(this);
    }

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

    /**
     * 获取研究报告
     *
     * @param url
     * @param page
     */
    public void getReportData(String url, final int page) {
        OkHttpUtils.get().url(url)
                .addParams("type", "1")
                .addParams("count", "8")
                .addParams("skip", String.valueOf(8 * (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;
                }
                reportAdapter.setData(response, page == 1);
                slvReport.finishLoad(page == 20);
                reportList.addAll(response);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), ReportDescActivity.class);
        intent.putExtra("fileId", reportList.get(position).getFileId());
        intent.putExtra("fileName", reportList.get(position).getFileId());
        startActivity(intent);
    }

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