package com.bokecc.livemodule.live.qa.qainputlayout;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.bokecc.dwlivedemo.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;


public class QaInputImageListAdapter extends RecyclerView.Adapter<QaInputImageListAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<QaInputItemBean> list;
    private LayoutInflater mInflater;


    public QaInputImageListAdapter(Context context) {
        list = new ArrayList<>();
        mContext = context;
        mInflater = LayoutInflater.from(context);

    }




    public void add(ArrayList<QaInputItemBean> data) {
        this.list.addAll(data);
        notifyDataSetChanged();
    }

    public void setList(ArrayList<QaInputItemBean> data) {
        this.list.clear();
        this.list.addAll(data);
        notifyDataSetChanged();
    }




    /**
     * 清楚聊天数据
     */
    public void clearData() {
        if (list == null) {
            return;
        }
        list.clear();
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     */
    public void add(QaInputItemBean data) {
        list.add(data);
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.item_qa_input_img_list, parent, false);
        return new ViewHolder(itemView, viewType);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        QaInputItemBean bean = list.get(position);

        Glide.with(mContext).load(bean.getUri()).into(holder.ivQaInputItemImg);
        //重置状态为IDLE
        setViewState(bean,holder,position);
        holder.ivQaInputImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemRemoveClick(position);
                }
            }
        });
        holder.ivQaInputImgWarning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemWarningClick(position);
                }
            }
        });

        holder.rlQaInputImgItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });

    }

    private void setViewState(QaInputItemBean bean, ViewHolder holder, int position) {
        if (bean.getState()==QaInputItemBean.IDLE){
            //正常显示模式
            holder.ivQaInputImgWarning.setVisibility(View.GONE);
            holder.rlQaInputImgLoading.setVisibility(View.GONE);
            holder.ivQaInputImgClose.setVisibility(View.VISIBLE);
        }else if (bean.getState()==QaInputItemBean.STATE_UPLOADING){
            //正在上传中
            holder.ivQaInputImgWarning.setVisibility(View.GONE);
            holder.rlQaInputImgLoading.setVisibility(View.VISIBLE);
            holder.ivQaInputImgClose.setVisibility(View.GONE);
        }else if (bean.getState()==QaInputItemBean.STATE_UPLOAD_SUCCESS){
            //上传成功
            holder.ivQaInputImgWarning.setVisibility(View.GONE);
            holder.rlQaInputImgLoading.setVisibility(View.GONE);
            holder.ivQaInputImgClose.setVisibility(View.VISIBLE);
        }else if (bean.getState()==QaInputItemBean.STATE_UPLOAD_FAILED){
            //上传失败
            holder.ivQaInputImgWarning.setVisibility(View.VISIBLE);
            holder.rlQaInputImgLoading.setVisibility(View.GONE);
            holder.ivQaInputImgClose.setVisibility(View.VISIBLE);
        }else {
            holder.ivQaInputImgWarning.setVisibility(View.GONE);
            holder.rlQaInputImgLoading.setVisibility(View.GONE);
            holder.ivQaInputImgClose.setVisibility(View.VISIBLE);
        }
    }


    private OnItemClickListener onItemClickListener;

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

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



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

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

    final class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout rlQaInputImgLoading;
        RelativeLayout rlQaInputImgItem;
        ImageView ivQaInputItemImg;
        int viewType;

        ImageView ivQaInputImgClose;
        ImageView ivQaInputImgWarning;



        ViewHolder(View itemView, int viewType) {
            super(itemView);
            this.viewType = viewType;

            rlQaInputImgItem = itemView.findViewById(R.id.rl_qa_input_img_item);
            ivQaInputItemImg = itemView.findViewById(R.id.iv_qa_input_item_img);
            ivQaInputImgClose = itemView.findViewById(R.id.iv_qa_input_img_close);
            ivQaInputImgWarning = itemView.findViewById(R.id.iv_qa_input_img_warning);
            rlQaInputImgLoading = itemView.findViewById(R.id.rl_qa_input_img_loading);


        }
    }
}