package com.daya.live_teaching.ui.adapter;

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

import com.daya.live_teaching.R;
import com.daya.live_teaching.model.ClassMember;
import com.daya.live_teaching.model.Role;
import com.daya.live_teaching.model.UserInfo;
import com.daya.live_teaching.ui.base.BaseRecyclerAdapter;
import com.daya.live_teaching.ui.base.BaseViewHolder;
import com.daya.live_teaching.ui.fragmnet.VideoViewManager;
import com.daya.live_teaching.ui.view.ClassVideoListItem;
import com.daya.live_teaching.ui.view.RadioRtcVideoView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import androidx.annotation.NonNull;


/**
 * 视频列表的 Adapter
 */
public class VideoListAdapter extends BaseRecyclerAdapter<VideoListAdapter.ViewHolder> {


    public  List<ClassMember>      memberList;
    private OnUserUpdateListener   listener;
    private List<String>           updateUserIds = Collections.synchronizedList(new ArrayList<String>());
    private OnItemBtnClickListener itemBtnClicklistener;
    Context mContext;
    private int         removePosition = -1;
    private ClassMember removeMember;
    private UserInfo    userInfo;

    public VideoListAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getItemCount() {
        return memberList == null ? 0 : memberList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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


    public class ViewHolder extends BaseViewHolder {
        ClassVideoListItem item;

        public ViewHolder(View itemView) {
            super(itemView);
            item = itemView.findViewById(R.id.video_view);
        }

        @Override
        protected void onBind(int position) {
            ClassMember classMember = memberList.get(position);
            ClassMember oldData = item.getData();
            item.setUserInfo(userInfo);
            item.setData(classMember);

            item.setOnItemMemberClickListener((type, isChecked, member) -> {
                itemBtnClicklistener.onClick(type, isChecked, member);
            });
            if (oldData != null) {
                //如果不为空，则判读是当前的人是否还是同一个人， 如果不是， 则通知刷新
                boolean update = isUpdate(classMember.getUserId());
                if (classMember == null || update || !oldData.getUserId().equals(classMember.getUserId())) {
                    // 通知解绑和绑定
                    if (listener != null) {
                        listener.onUpdate(item, position, classMember);
                    }
                }
            } else {
                // 通知订阅
                if (listener != null) {
                    listener.onUpdate(item, position, classMember);
                }
            }
            item.setOnClickListener(view -> {
                RadioRtcVideoView videoView = VideoViewManager.getInstance().get(classMember.getUserId());
                boolean has = item.hasVideoView(videoView);
                if (has) {
                    onItemClickListener.onItemClick(classMember);
                }
            });
        }
    }


    public void setListData(List<ClassMember> memberList, UserInfo userInfo, boolean isNotify) {
        this.memberList = memberList;
        this.userInfo = userInfo;
        //        if (isRemoveMember) {
        //            if (removePosition < 0 && this.memberList.size() > 0) {
        //                removeMember = this.memberList.get(0);
        //                removePosition = 0;
        //                this.memberList.remove(0);
        //            } else if (this.memberList.size() > removePosition) {
        //                this.memberList.remove(removePosition);
        //            }
        //        }
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    public void setOnUserUpdateListener(OnUserUpdateListener listener) {
        this.listener = listener;
    }


    public void notifyDataSetChanged(List<String> userIds) {
        if (userIds == null || userIds.size() <= 0) {
            return;
        }
        if (updateUserIds == null) {
            updateUserIds = userIds;
        } else {
            for (String userId : userIds) {
                boolean contains = updateUserIds.contains(userId);
                if (!contains) {
                    updateUserIds.add(userId);
                }
            }
        }
        userIds.clear();
        notifyDataSetChanged();
    }

    public void removeItemSetChanged(ClassMember member) {
        if (null == member) {
            return;
        }
        if (removePosition >= 0 && removeMember != null && (!removeMember.getUserId().equals("-1") || member.getRole() != Role.LECTURER)) {
            memberList.add(removePosition, removeMember);
        }

        Iterator<ClassMember> iterator = memberList.iterator();
        while (iterator.hasNext()) {
            ClassMember members = iterator.next();
            if (members.getUserId().equals(member.getUserId())) {
                removePosition = memberList.indexOf(members);
                removeMember = members;
                iterator.remove();
                notifyItemRemoved(removePosition);
            }
        }
        notifyDataSetChanged();
    }

    public void addRemoveItemNotifyChanged() {
        if (removePosition >= 0 && removeMember != null) {
            memberList.add(removePosition, removeMember);
        }
        notifyItemRangeChanged(removePosition, memberList.size() - removePosition);
        //        notifyDataSetChanged();
        removePosition = -1;
        removeMember = null;
    }

    private boolean isUpdate(String userId) {
        if (updateUserIds == null || updateUserIds.size() <= 0) {
            return false;
        }

        boolean contains = updateUserIds.contains(userId);
        if (contains) {
            int index = updateUserIds.indexOf(userId);
            updateUserIds.remove(index);
        }
        return contains;
    }

    public interface OnUserUpdateListener {
        void onUpdate(ClassVideoListItem view, int position, ClassMember newMember);
    }

    public void setOnItemBtnClickListener(OnItemBtnClickListener itemBtnClicklistener) {
        this.itemBtnClicklistener = itemBtnClicklistener;
    }

    public interface OnItemBtnClickListener {
        void onClick(ClassVideoListItem.BtnOperate type, boolean isChecked, ClassMember member);
    }


    public OnItemClickListener onItemClickListener;

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

    public interface OnItemClickListener {
        void onItemClick(ClassMember member);
    }


}
