package com.daya.live_teaching.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cooleshow.base.utils.GlideImageLoaderUtils;
import com.daya.live_teaching.R;
import com.luck.picture.lib.entity.LocalMedia;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Description:
 * Copyright  : Copyright (c) 2019
 * Company    : 大雅乐盟
 * Author     : r
 * Date       : 2019/9/18 19:49
 */
public class AddPhotoListAdapter extends RecyclerView.Adapter<AddPhotoListAdapter.ViewHolder> {


    private Context mContext;
    List<LocalMedia> selectList = new ArrayList<>();
    private int     maxSelectNum;
    private boolean isAddPhoto = true;

    public AddPhotoListAdapter(Context mContex, int maxSelectNum, boolean isAddPhoto) {
        this.mContext = mContex;
        this.maxSelectNum = maxSelectNum;
        this.isAddPhoto = isAddPhoto;

    }

    public AddPhotoListAdapter(Context mContex, int maxSelectNum) {
        this.mContext = mContex;
        this.maxSelectNum = maxSelectNum;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflater = LayoutInflater.from(mContext).inflate(R.layout.complaint_photo_list_item, parent, false);
        return new ViewHolder(inflater);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.llIcon.setOnClickListener(view -> onItemClickListener.onItemClick(position));

        if (selectList.size() < maxSelectNum && isAddPhoto) {
            if (selectList.size() != position) {
                GlideImageLoaderUtils.getInstance().loadCustRoundCircleImage(mContext, selectList.get(position).getPath(), holder.ivIcon, 5);
                holder.ivIcon.setVisibility(View.VISIBLE);
                holder.tvIcon.setVisibility(View.GONE);
            } else {
                holder.ivIcon.setVisibility(View.GONE);
                holder.tvIcon.setVisibility(View.VISIBLE);
                holder.ivIcon.setBackgroundResource(R.drawable.bg_phone_selecter);
                holder.ivIcon.setImageResource(R.drawable.ic_add_photo);
            }
        } else {
            holder.ivIcon.setVisibility(View.VISIBLE);
            holder.tvIcon.setVisibility(View.GONE);
            GlideImageLoaderUtils.getInstance().loadCustRoundCircleImage(mContext, selectList.get(position).getPath(), holder.ivIcon, 5);
        }

    }


    @Override
    public int getItemCount() {
        if (!isAddPhoto) {
            return selectList.size();
        }
        if (selectList.size() == 0) {
            return 1;
        }
        if (selectList.size() < maxSelectNum) {
            return selectList.size() + 1;
        }
        return maxSelectNum;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView    ivIcon;
        TextView     tvIcon;
        LinearLayout llIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.iv_icon);
            tvIcon = itemView.findViewById(R.id.tv_icon);
            llIcon = itemView.findViewById(R.id.ll_icon);
        }


    }

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setData(List<LocalMedia> list) {
        selectList.clear();
        selectList.addAll(list);
        notifyDataSetChanged();
    }
}
