package com.taikor.investment.modules.find;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.taikor.investment.R;
import com.taikor.investment.adapter.CardAdapter;
import com.taikor.investment.base.BaseFragment;
import com.taikor.investment.bean.Group;
import com.taikor.investment.utils.Constant_URL;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

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

/**
 * 达人组合
 * Created by Any on 2017/6/9.
 */
public class CardFragment extends BaseFragment {

    @BindView(R.id.rlv_card)
    RecyclerView rlvCard;

    private CardAdapter cardAdapter;

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

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        LinearLayoutManager layout = new LinearLayoutManager(getActivity());
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        rlvCard.setLayoutManager(layout);
        cardAdapter = new CardAdapter(getActivity());
    }

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

    public void getData(String url) {
        OkHttpUtils.get().url(url)
                .addParams("type", "3")
                .addParams("style", "-1")
                .addParams("proportion", "-1")
                .addParams("count", "5")
                .addParams("sortType", "3")
                .build().execute(new Callback<List<Group>>() {

            @Override
            public List<Group> parseNetworkResponse(Response response, int id) throws Exception {
                Gson gson = new Gson();
                return gson.fromJson(response.body().string(), new TypeToken<List<Group>>() {
                }.getType());
            }

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

            @Override
            public void onResponse(List<Group> response, int id) {
                if (response == null) {
                    return;
                }

                cardAdapter.setData(response);
                rlvCard.setAdapter(cardAdapter);
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if(isVisibleToUser){
//           TAnalytics.onPageStart("card");
        }else{
//           TAnalytics.onPageLoad(getActivity(),"card","","","");
        }
    }
}
