package com.bc_chat.circle.adapetr;

import android.content.Context;
import android.view.View;

import com.bc_chat.contacts.R;
import com.bc_chat.contacts.databinding.ItemPhotoLayoutBinding;
import com.zhaohaoting.framework.abs.adapter.ListToAdapter;
import com.zhaohaoting.framework.imagepicker.bean.MediaItem;
import com.zhaohaoting.framework.utils.glid.GlideUtils;

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

/**
 * author：Created by zhaohaoting on 2019/6/13
 * email：526309416@qq.com
 * desc：
 */
public class PhotoAdapter extends ListToAdapter<MediaItem, List<MediaItem>> {

    private MediaItem addItem;
    private int maxImgCount;

    public PhotoAdapter(Context context, int maxImgCount) {
        super(context);
        addItem = new MediaItem();
        this.maxImgCount = maxImgCount;
    }

    @Override
    protected int getItemViewTypeHF(int realPosition) {
        return mDataList.get(realPosition) == addItem ? 1 : 0;
    }

    @Override
    protected int getItemLayoutId(int type) {
        return R.layout.item_photo_layout;
    }

    @Override
    protected void onBindViewHolderHF(BaseViewHolder baseViewHolder, int realPosition, MediaItem mediaItem) {
        ItemPhotoLayoutBinding binding = (ItemPhotoLayoutBinding) baseViewHolder.getDataBinding();
        if (getItemViewType(realPosition) == 0) {
            binding.ivDelete.setVisibility(View.VISIBLE);
            GlideUtils.loadRoundImage(context, mediaItem.path, binding.ivPhoto,4);
        } else {
            binding.ivDelete.setVisibility(View.GONE);
            GlideUtils.loadImage(context, R.mipmap.ic_add_photo, binding.ivPhoto);
        }
        if (onItemViewClickListener!=null){
            binding.ivDelete.setOnClickListener(v -> onItemViewClickListener.onItemClick(baseViewHolder,v,realPosition));
        }
    }

    @Override
    public void notifyListDataChanged(List<MediaItem> mediaItems, boolean isRefresh) {
        mDataList.remove(addItem);
        if (isRefresh) {
            mDataList.clear();
        }
        if (mediaItems != null) {
            mDataList.addAll(mediaItems);
        }
        if (mDataList.size() < maxImgCount) {
            mDataList.add(addItem);
        }
        notifyDataSetChanged();
    }

    @Override
    public List<MediaItem> getData() {
        ArrayList<MediaItem> mediaItemList = new ArrayList<>();
        for (MediaItem mediaItem : mDataList) {
            if (mediaItem != addItem) {
                mediaItemList.add(mediaItem);
            }
        }
        return mediaItemList;
    }
}
