package com.wasu.cs.adapter;

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

import com.wasu.cs.ui.ActivityFeiHuCs;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;

import java.util.List;

import cn.com.wasu.main.IntentMap;
import cn.com.wasu.main.R;

/**
 * Created by stone.
 */
public class FeiHuRecyclerAdapter extends
        RecyclerView.Adapter<FeiHuRecyclerAdapter.ViewHolder> {


    // 数据集
    private List<ActivityFeiHuCs.AssetElement> mList;
    private Context mContext;

    public FeiHuRecyclerAdapter(Context context) {
        super();
        this.mContext = context;
    }

    public void setData(List<ActivityFeiHuCs.AssetElement> list) {
        this.mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = View.inflate(viewGroup.getContext(), R.layout.item_feihu_asset, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int i) {
        FrescoImageFetcherModule.getInstance().attachImage(mList.get(i).getPicUrl(), viewHolder.picImageView);
        viewHolder.picImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityFeiHuCs.AssetElement itemData = mList.get(i);
                IntentMap.startIntent(mContext, null, itemData.getLayout(), itemData.getJsonUrl(), null);
            }
        });
        if (!TextUtils.isEmpty(mList.get(i).getTitle())) {
            viewHolder.nameTextView.setText(mList.get(i).getTitle());
            viewHolder.nameTextView.setVisibility(View.VISIBLE);
        }
    }


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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View view;
        public ImageView picImageView;
        public TextView nameTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            picImageView = (ImageView) itemView
                    .findViewById(R.id.pic);
            nameTextView = (TextView) itemView.findViewById(R.id.name);
        }

        public View getView() {
            return view;
        }
    }


}
