package com.taikor.investment.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taikor.investment.R;
import com.taikor.investment.bean.Group;
import com.taikor.investment.modules.group.GroupDescActivity;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.DegreeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 达人组合
 * Created by Any on 2017/6/9.
 */
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.DefaultViewHolder> {

    private Context context;
    private List<Group> groups;

    public CardAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Group> products) {
        this.groups = products;
        notifyDataSetChanged();
    }

    @Override
    public DefaultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_find_card, parent, false);
        return new DefaultViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final DefaultViewHolder holder, int position) {
        holder.ivPhoto.setImageResource(R.drawable.head_img);
        Group group = groups.get(position);
        holder.tvName.setText(group.getManager());
        if (TextUtils.isEmpty(group.getCompany())) {
            holder.tvCompany.setText("——");
        } else {
            holder.tvCompany.setText(group.getCompany());
        }
        holder.tvGroupName.setText(group.getPortfolioName());
        holder.tvNetFit.setText(DegreeUtils.setDoubleFour(group.getNetWorth()));
        //添加到我的组合
        holder.tvAddGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        //条目点击，跳转详情
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String portfolioID = groups.get(holder.getAdapterPosition()).getPortfolioID();
                //埋点
                TAnalytics.onClick("enterGroupDetail",portfolioID);
                Intent intent = new Intent(context, GroupDescActivity.class);
                intent.putExtra("itemId", portfolioID);
                intent.putExtra("from","cardPage");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return groups.size();
    }

    public class DefaultViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_photo)
        ImageView ivPhoto;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_company)
        TextView tvCompany;
        @BindView(R.id.tv_group_name)
        TextView tvGroupName;
        @BindView(R.id.tv_net_fit)
        TextView tvNetFit;
        @BindView(R.id.tv_add_group)
        TextView tvAddGroup;

        View rootView;

        private DefaultViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
}
