package com.danding.yang800.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Entity.ImageItem;
import com.danding.yang800.PictureCheckActivity;
import com.danding.yang800.R;

import java.util.ArrayList;
import java.util.List;


public class RefundImagePickerAdapter extends RecyclerView.Adapter<RefundImagePickerAdapter.SelectedPicViewHolder> {
    private int maxImgCount;
    private Context mContext;
    private List<ImageItem> mData;
    private LayoutInflater mInflater;
    private OnRecyclerViewItemClickListener listener;
    private boolean isAdded;   //是否额外添加了最后一个图片

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);

        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

    public void setImages(List<ImageItem> data) {
        mData = new ArrayList<>(data);
        if (getItemCount() < maxImgCount) {
            mData.add(new ImageItem());
            isAdded = true;
        } else {
            isAdded = false;
        }
        notifyDataSetChanged();
    }

    public List<ImageItem> getImages() {
        //由于图片未选满时，最后一张显示添加图片，因此这个方法返回真正的已选图片
        if (isAdded) return new ArrayList<>(mData.subList(0, mData.size() - 1));
        else return mData;
    }

    public RefundImagePickerAdapter(Context mContext, List<ImageItem> data, int maxImgCount) {
        this.mContext = mContext;
        this.maxImgCount = maxImgCount;
        this.mInflater = LayoutInflater.from(mContext);
        setImages(data);
    }

    @Override
    public SelectedPicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectedPicViewHolder(mInflater.inflate(R.layout.refund_item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(SelectedPicViewHolder holder, int position) {
        holder.bind(position);
    }

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

    public class SelectedPicViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView refundiv_img;
        private ImageView refundiv_cancelIv;
        private int clickPosition;

        public SelectedPicViewHolder(View itemView) {
            super(itemView);
            refundiv_img = (ImageView) itemView.findViewById(R.id.refundiv_img);
            refundiv_cancelIv = (ImageView) itemView.findViewById(R.id.refundiv_cancelIv);
        }

        public void bind(final int position) {
            //设置条目的点击事件
            itemView.setOnClickListener(this);
            refundiv_cancelIv.setOnClickListener(this);
            //根据条目位置设置图片
            ImageItem item = mData.get(position);
            if (isAdded && position == getItemCount() - 1) {
                refundiv_img.setImageResource(R.drawable.shouhou_sc);
                clickPosition = PictureCheckActivity.IMAGE_ITEM_ADD;
                refundiv_cancelIv.setVisibility(View.INVISIBLE);
            } else {
                // ImagePicker.getInstance().getImageLoader().displayImage((Activity) mContext, item.path, refundiv_img, 0, 0);
                refundiv_cancelIv.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(item.path).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(refundiv_img);
                clickPosition = position;
            }
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.refundiv_cancelIv:
                    if (listener != null) listener.onItemClick(clickPosition);
                    break;
                default:
                    if (listener != null) listener.onItemClick(v, clickPosition);
                    break;
            }


        }
    }
}