package cn.gcs.base.gcsapp.activity.main.fg_center.ac_myProject.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
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.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;

import cn.gcs.base.gcsapp.R;
import cn.gcs.base.gcsapp.activity.main.fg_center.ac_myProject.callback.ICollection;
import cn.gcs.base.gcsapp.activity.main.fg_friendCircle.entre_project.ProjectInfoActivity;
import cn.gcs.base.gcsapp.activity.main.fg_friendCircle.entre_project.m.ProjectInfo;
import cn.gcs.base.gcsapp.config.BuildConfig;
import cn.gcs.base.gcsapp.utils.StringUtils;

/**
 * Created by  on 2016/9/2.
 */
public class Rv_MyProjectAdapter extends RecyclerView.Adapter<Rv_MyProjectAdapter.AnchorHotViewHolder> {
    private ArrayList<ProjectInfo> mData;
    private Context mContext;
    public ICollection mListener;

    public Rv_MyProjectAdapter(ArrayList<ProjectInfo> data, Context context, ICollection mListener) {
        this.mContext = context;
        this.mData = data;
        this.mListener = mListener;
    }

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

    @Override
    public void onBindViewHolder(final AnchorHotViewHolder holder, final int position) {
        ProjectInfo info = mData.get(position);
        holder.tvName.setText(info.getProjectName());
        holder.tvCity.setText(info.getCity());

//        if(StringUtils.isNotEmpty(info.getRounds())){
//            holder.tvRound.setText(info.getRounds() + "轮");
//
//        } else {
//        }
        holder.tvRound.setText(StringUtils.getRoundString(info.getRounds()));
        holder.tvStatus.setText(1 == info.getStatus() ? "待审批" : 2 == info.getStatus() ? "审批成功" : "审批失败");
        if (1 != info.getStatus() && 2 != info.getStatus()) {
            holder.tvStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mListener) {
                        mListener.onStatus(mData.get(position), position);
                    }
                }
            });
        }
        holder.tvRemark.setText(info.getIntroduction());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ProjectInfoActivity.class);
                intent.putExtra("ProjectID", mData.get(position));
                mContext.startActivity(intent);
            }
        });
        if (null != info.getIconUrl() && info.getIconUrl().contains("http://")) {
            Glide.with(mContext).load(info.getIconUrl()).error(R.mipmap.icon_default_pic).diskCacheStrategy(DiskCacheStrategy.RESULT).into(holder.pic);
        } else {
            Glide.with(mContext).load(BuildConfig.QiniuBase + info.getIconUrl()).error(R.mipmap.icon_default_pic).diskCacheStrategy(DiskCacheStrategy.RESULT).into(holder.pic);
        }
        holder.tvCommentCount.setText("评论 " + info.getCommentCount());
        if (info.getCollectionStatus().equals("1")) {
            Drawable drawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_gcs_entre_project_list_item_collection_d);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            holder.tv_project_sorting_collection.setCompoundDrawables(drawable, null, null, null);
        } else {
            Drawable drawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_gcs_entre_project_list_item_collection_u);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            holder.tv_project_sorting_collection.setCompoundDrawables(drawable, null, null, null);
        }
        holder.tv_project_sorting_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != mListener) {
                    mListener.onCollection(mData.get(position), position);
                }
            }
        });
    }

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

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class AnchorHotViewHolder extends RecyclerView.ViewHolder {
        ImageView pic;
        TextView tvName;
        TextView tvRound;
        TextView tvStatus;
        TextView tvCity;
        TextView tvRemark;
        TextView tvCommentCount;
        TextView tv_project_sorting_collection;

        public AnchorHotViewHolder(final View itemView) {
            super(itemView);
            pic = (ImageView) itemView.findViewById(R.id.img_layout_project_sorting_item_pic);
            tvName = (TextView) itemView.findViewById(R.id.tv_item_project_list_name);
            tvRound = (TextView) itemView.findViewById(R.id.tv_item_project_list_round);
            tvStatus = (TextView) itemView.findViewById(R.id.tv_status);
            tvCity = (TextView) itemView.findViewById(R.id.tv_item_project_list_city);
            tvRemark = (TextView) itemView.findViewById(R.id.tv_item_project_list_remark);
            tvCommentCount = (TextView) itemView.findViewById(R.id.tv_project_sorting_comments);
            tv_project_sorting_collection = (TextView) itemView.findViewById(R.id.tv_project_sorting_collection);
        }
    }
}