package com.daya.live_teaching.ui.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cooleshow.base.utils.SizeUtils;
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.permission.ClassPermission;
import com.daya.live_teaching.widget.OnOperateItemListener;
import com.daya.live_teaching.widget.OperateButtonGroupView;
import com.daya.live_teaching.widget.OperateItem;

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

import cn.rongcloud.rtc.api.stream.RCRTCVideoView;


/**
 * 视频列表 item 布局
 */
public class ClassVideoListItem extends RadioRelativeLayout {
    private LinearLayout           videoViewContainer;
    private TextView               roleText;
    private TextView               nameText;
    private TextView               contentText;
    private ClassMember            data;
    private boolean                isHideRole = false;
    private OperateButtonGroupView btnOperateView;
    private ImageView rightText;
    private UserInfo               userInfo;

    public ClassVideoListItem(Context context) {
        super(context);
        initView();
    }

    public ClassVideoListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ClassVideoListItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public enum BtnOperate { // 控制按钮类型
        /**
         * 麦克风
         */
        MIC(0),
        /**
         * 在线成员
         */
        CAMERA(1),
        /**
         * 退出
         */
        MUSIC_MODE(2);

        private int value;

        BtnOperate(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static BtnOperate getType(int value) {
            for (BtnOperate type : BtnOperate.values()) {
                if (value == type.getValue()) {
                    return type;
                }
            }
            return MIC;
        }
    }

    //    // 按钮的资源布局
    private int[] btnBgResIds = new int[]{
            R.drawable.fragment_share_screen_operate_mic_selector,
            R.drawable.fragment_share_screen_operate_camera_selector,
            R.drawable.fragment_share_screen_music_mode_selector};

    private void initView() {
        View view = View.inflate(getContext(), R.layout.class_item_video_list, this);
        videoViewContainer = view.findViewById(R.id.class_video_item_videoview_container);
        roleText = view.findViewById(R.id.class_tv_video_item_role);
        nameText = view.findViewById(R.id.class_tv_video_item_name);
        contentText = view.findViewById(R.id.class_tv_video_item_content);
        rightText = view.findViewById(R.id.class_tv_video_item_right_text);
        if (!isHideRole) {
            btnOperateView = view.findViewById(R.id.btn_operate_view);
            // 初始化动态添加控制按钮
            List<OperateItem> rightItems = new ArrayList<>();
            int width = SizeUtils.dp2px(34);
            int height = SizeUtils.dp2px(34);
            int marginLeft = SizeUtils.dp2px(5);
            for (BtnOperate operate : BtnOperate.values()) {
                OperateItem item = new OperateItem();
                item.id = operate.getValue();
                item.bgResId = btnBgResIds[operate.getValue()];
                item.width = width;
                item.height = height;
                item.left = marginLeft;
                item.type = OperateItem.Type.CHECKBOX;
                rightItems.add(item);
            }

            // 初始化操作布局
            btnOperateView.initView(rightItems, new OnOperateItemListener() {
                @Override
                public void onItemClicked(View v, OperateItem item) {
                }

                @Override
                public void onCheckedChanged(View v, OperateItem item, boolean isChecked) {
                    if (v.isPressed()) {
                        BtnOperate type = BtnOperate.getType(item.id);
                        listener.onClick(type, isChecked, data);
                    }
                }
            });
            btnOperateView.setItemVisibility(BtnOperate.MIC.getValue(), View.GONE);
            btnOperateView.setItemVisibility(BtnOperate.CAMERA.getValue(), View.GONE);
            btnOperateView.setItemVisibility(BtnOperate.MUSIC_MODE.getValue(), View.GONE);
        }
    }

    public void addVideoView(RCRTCVideoView videoView, LinearLayout.LayoutParams params) {
        videoViewContainer.removeAllViews();
        videoViewContainer.addView(videoView, params);
    }

    public void addVideoView(RCRTCVideoView videoView) {
        videoViewContainer.removeAllViews();
        videoViewContainer.addView(videoView);
    }

    public int getVideoViewCount() {
        return videoViewContainer.getChildCount();
    }

    public boolean hasVideoView(RCRTCVideoView videoView) {
        int i = videoViewContainer.indexOfChild(videoView);
        return i < 0 ? false : true;
    }


    public void removeVideoView() {
        videoViewContainer.removeAllViews();
    }

    public void setRole(int strResId) {
        roleText.setText(strResId);
    }

    public void setRole(String role) {
        roleText.setText(role);
    }

    public void setName(int strResId) {
        nameText.setText(strResId);
    }

    public void setName(String name) {
        nameText.setText(name);
    }

    public void contentText(int strResId) {
        contentText.setText(strResId);
    }

    public void contentText(String content) {
        contentText.setText(content);
    }

    public void setRoleVisibility(int visibility) {
        roleText.setVisibility(visibility);
    }

    public void setContentVisibility(int visibility) {
        contentText.setVisibility(visibility);
    }


    public void setVideViewToCenter() {
        videoViewContainer.setGravity(Gravity.CENTER);
    }


    public void setVideViewToCenter(boolean center) {
        if (center) {
            videoViewContainer.setGravity(Gravity.CENTER);
        } else {
            videoViewContainer.setGravity(Gravity.LEFT);
        }
    }

    public void setVisibiliySpeech(boolean applyForSpeech) {
        if (applyForSpeech) {
            rightText.setVisibility(VISIBLE);
        } else {
            rightText.setVisibility(GONE);
        }
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void setVisibiliyMic(boolean isHideMic) {
        if (isHideMic) {
            btnOperateView.setItemVisibility(BtnOperate.MIC.getValue(), View.VISIBLE);
        } else {
            btnOperateView.setItemVisibility(BtnOperate.MIC.getValue(), View.GONE);
        }
    }

    public void setData(ClassMember item) {
        reset();
        if (item == null) {
            return;
        }
        this.data = item;
        if (!isHideRole) {
            if (item.getRole().hasPermission(ClassPermission.LECTURE)) {
                if (item.getUserId().equals("-1")) {
                    setRoleVisibility(View.GONE);
                } else {
                    setRoleVisibility(View.VISIBLE);
                    setRole(R.string.class_role_lecturer);
                }
                roleText.setBackgroundResource(R.drawable.member_list_role_lecturer_bg);
            } else if (item.getRole().hasPermission(ClassPermission.TRANSFER_ROLE)) {
                setRoleVisibility(View.VISIBLE);
                setRole(R.string.class_role_assistant);
                roleText.setBackgroundResource(R.drawable.member_list_role_assistant_bg);
            } else {
                setRoleVisibility(View.GONE);
            }
            String userName = item.getUserName();
            userName = TextUtils.isEmpty(userName) ? "" : userName;
            if (userName.length() > 5) {
                userName = userName.substring(0, 5) + "...";
            }
            setName(userName);
            if (userInfo != null && userInfo.getRole() == Role.LECTURER) {
                setVisibiliySpeech(item.isHandUpOn());
            }
            setVisibiliyMic(item.isVisibiliyMic());
            if (data.isOnStage() && userInfo.getRole() == Role.LECTURER) {
                contentText("演示中");
                setContentVisibility(View.VISIBLE);
            } else {
                contentText("");
                setContentVisibility(View.GONE);
            }
        }
    }

    private void reset() {
        setRole("");
        setRoleVisibility(View.GONE);
        setName("");
        //        setContentVisibility(View.GONE);
    }

    public ClassMember getData() {
        return data;
    }

    public void setHideRole(boolean isHideRole) {
        this.isHideRole = isHideRole;
    }

    public void clear() {
        reset();
        removeVideoView();
        data = null;
    }

    public RadioRtcVideoView getVideoView() {
        if (videoViewContainer.getChildCount() > 0) {
            return (RadioRtcVideoView) videoViewContainer.getChildAt(0);
        }
        return null;
    }

    private OnItemMemberClickListener listener;

    public void setOnItemMemberClickListener(OnItemMemberClickListener listener) {
        this.listener = listener;
    }

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