package com.bokecc.room.ui.view.userList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bokecc.common.utils.DensityUtil;
import com.bokecc.common.utils.Tools;
import com.bokecc.room.ui.R;
import com.bokecc.room.ui.model.RoomUser;
import com.bokecc.room.ui.view.adapter.BaseRecycleAdapter;
import com.bokecc.sskt.base.CCAtlasClient;
import com.bokecc.sskt.base.bean.CCUser;

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

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    //Model：定义的数据
    private String[] groups;

    private Context mContext;

    private int mLianmaiMode = 0;

    /**
     * 用户
     * 一级分类，二级用户列表
     */
    private List<List<RoomUser>> mRoomUsersList = new ArrayList<>();

    public ExpandableListViewAdapter(Context context,String[] groups,List<List<RoomUser>> mRoomUsersList){
        this.mContext=context;
        this.groups=groups;
        this.mRoomUsersList=mRoomUsersList;
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int i) {
        if(mRoomUsersList != null){
            if(mRoomUsersList.get(i) != null){
                return mRoomUsersList.get(i).size();
            }
        }
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        return groups[i];
    }

    @Override
    public Object getChild(int i, int i1) {
        return mRoomUsersList.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    public boolean hasStableIds() {
        return true;
    }

    @Override
    /**
    *
    * 获取显示指定组的视图对象
    *
    * @param groupPosition 组位置
    * @param isExpanded 该组是展开状态还是伸缩状态，true=展开
    * @param convertView 重用已有的视图对象
    * @param parent 返回的视图对象始终依附于的视图组
    */
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_user_item_parent_layout,parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.parent_user_name = convertView.findViewById(R.id.id_user_name);
            groupViewHolder.parent_user_num = convertView.findViewById(R.id.id_user_num);
            groupViewHolder.parent_image = convertView.findViewById(R.id.id_user_arrow);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }
        groupViewHolder.parent_user_name.setText(groups[groupPosition]);

        if(mRoomUsersList != null && mRoomUsersList.size() > groupPosition && mRoomUsersList.get(groupPosition) != null){
            groupViewHolder.parent_user_num.setText(mRoomUsersList.get(groupPosition).size()+Tools.getString(R.string.cc_user_list_people));
        }

        //如果是展开状态，
        if (isExpanded){
            groupViewHolder.parent_image.setImageResource(R.mipmap.expandable_lv_arrow_h);
        }else{
            groupViewHolder.parent_image.setImageResource(R.mipmap.expandable_lv_arrow);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        RoomUserViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_user_item_layout,parent,false);
            holder = new RoomUserViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (RoomUserViewHolder)convertView.getTag();
        }

        if(mRoomUsersList != null && mRoomUsersList.size() > groupPosition && mRoomUsersList.get(groupPosition) != null){
            RoomUser roomUser = mRoomUsersList.get(groupPosition).get(childPosition);
            CCUser user = roomUser.getUser();

            int cupNum = CCAtlasClient.getInstance().getCupNum(user.getUserId());
            if(cupNum>0){
//                holder.mReward.setVisibility(View.VISIBLE);
                holder.mCup.setVisibility(View.VISIBLE);
                holder.mCup.setText("x" + cupNum);
            }else {
                holder.mCup.setVisibility(View.GONE);
            }
            if(user.getRewardHammerIndex()>0){
//                holder.mReward.setVisibility(View.VISIBLE);
                holder.mHammer.setVisibility(View.VISIBLE);
                holder.mHammer.setText("x" + user.getRewardHammerIndex());
            }else {
                holder.mHammer.setVisibility(View.GONE);
            }
            if (user.getSendFlower()) {
//                holder.mReward.setVisibility(View.VISIBLE);
                holder.mFlower.setVisibility(View.VISIBLE);
                holder.mFlower.setText("x" + user.getFlowerIndex());
            }else {
                holder.mFlower.setVisibility(View.GONE);
            }
//            if(user.getCupIndex()==0&&user.getRewardHammerIndex()==0&&user.getFlowerIndex()==0){
//                holder.mReward.setVisibility(View.GONE);
//            }

            int mType = CCAtlasClient.getInstance().getRole();
            if (mType == CCAtlasClient.PRESENTER && user.getUserRole() == CCAtlasClient.TALKER) {
                holder.mArrow.setVisibility(View.VISIBLE);
            } else if(mType == CCAtlasClient.ASSISTANT && user.getUserRole()  == CCAtlasClient.TALKER) {
                holder.mArrow.setVisibility(View.VISIBLE);
            } else {
                if (mType == CCAtlasClient.TALKER) {
                    holder.mArrow.setVisibility(View.GONE);
                } else {
                    holder.mArrow.setVisibility(View.INVISIBLE);
                }
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mLianmai.getLayoutParams();
//                params.addRule(RelativeLayout.ALIGN_PARENT_END);
//                params.rightMargin = DensityUtil.dp2px(mContext, 10);
//                holder.mLianmai.setLayoutParams(params);
            }
            if (user.getPlatForm() == CCAtlasClient.MOBILE) {
                holder.mDevice.setVisibility(View.VISIBLE);
                holder.mDevice.setImageResource(R.mipmap.user_phone);
            } else if (user.getPlatForm() == CCAtlasClient.PC){
                holder.mDevice.setVisibility(View.VISIBLE);
                holder.mDevice.setImageResource(R.mipmap.user_computer);
            } else {
                holder.mDevice.setVisibility(View.GONE);
            }
            if (user.getLianmaiStatus() == CCAtlasClient.LIANMAI_STATUS_MAI_ING) {
                holder.mLianmai.setVisibility(View.VISIBLE);
                holder.mStatusWait.setVisibility(View.GONE);
                holder.mStatusing.setVisibility(View.VISIBLE);
            } else if (user.getLianmaiStatus() == CCAtlasClient.LIANMAI_STATUS_IN_MAI) {
                holder.mLianmai.setVisibility(View.VISIBLE);
                holder.mStatusing.setVisibility(View.GONE);
                holder.mStatusWait.setVisibility(View.VISIBLE);
                int resId;
                String value;
                Drawable drawable;
                if (mLianmaiMode == CCAtlasClient.LIANMAI_MODE_FREE) {
                    resId = R.mipmap.user_wait_icon;
                    value = Tools.getString(R.string.cc_lianmai_ing_tip) + roomUser.getMaiIndex() + Tools.getString(R.string.cc_lianmai_ing2);
                } else {
                    resId = R.mipmap.user_hand_icon;
                    value = Tools.getString(R.string.cc_lianmai_ing_tip) + roomUser.getMaiIndex() + Tools.getString(R.string.cc_lianmai_handup);
                }
                drawable = mContext.getResources().getDrawable(resId);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                holder.mStatusWait.setCompoundDrawables(drawable, null, null, null);
                holder.mStatusWait.setText(value);
            } else if (user.getLianmaiStatus() == CCAtlasClient.LIANMAI_STATUS_IDLE) {
                holder.mLianmai.setVisibility(View.GONE);
                holder.mStatusWait.setVisibility(View.GONE);
                holder.mStatusing.setVisibility(View.GONE);
            } else if (user.getLianmaiStatus() == CCAtlasClient.LIANMAI_STATUS_INVITE_MAI) {
                holder.mLianmai.setVisibility(View.VISIBLE);
                holder.mStatusing.setVisibility(View.GONE);
                holder.mStatusWait.setVisibility(View.VISIBLE);
                Drawable drawable = mContext.getResources().getDrawable(R.mipmap.user_invite_icon);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                holder.mStatusWait.setCompoundDrawablePadding(DensityUtil.dp2px(mContext, 3));
                holder.mStatusWait.setCompoundDrawables(drawable, null, null, null);
                holder.mStatusWait.setText(R.string.cc_invite_mai);
            }
            holder.mUserName.setText(user.getUserName());
            if (user.getUserRole() == CCAtlasClient.PRESENTER) {
                holder.mIdentity.setVisibility(View.VISIBLE);
                holder.mIdentity.setImageResource(R.mipmap.user_identity_teacher);
                holder.mGag.setVisibility(View.GONE);
                holder.mLianmai.setVisibility(View.VISIBLE);
                holder.mDraw.setVisibility(View.GONE);
            } else if(user.getUserRole() == CCAtlasClient.ASSISTANT) {
                holder.mDraw.setVisibility(View.GONE);
                holder.mIdentity.setVisibility(View.VISIBLE);
                holder.mIdentity.setImageResource(R.mipmap.assistant_ident);
                holder.mLianmai.setVisibility(View.VISIBLE);
                if (!user.getUserSetting().isAllowChat()) {
//                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mGag.getLayoutParams();
//                    if (holder.mHand.getVisibility() == View.GONE) {
//                        if (holder.mLianmai.getVisibility() == View.GONE) {
//                            if (holder.mArrow.getVisibility() == View.GONE) {
//                                params.addRule(RelativeLayout.ALIGN_PARENT_END);
//                            } else {
//                                params.addRule(RelativeLayout.LEFT_OF, holder.mArrow.getId());
//                            }
//                        } else {
//                            params.addRule(RelativeLayout.LEFT_OF, holder.mHand.getId());
//                        }
//                    } else {
//                        params.addRule(RelativeLayout.LEFT_OF, holder.mLianmai.getId());
//                    }
//                    params.rightMargin = DensityUtil.dp2px(mContext, 10);
//                    holder.mGag.setLayoutParams(params);
                    holder.mGag.setVisibility(View.VISIBLE);
                } else {
                    holder.mGag.setVisibility(View.GONE);
                }
            } else {
                holder.mIdentity.setVisibility(View.GONE);

                if(user.getUserSetting() != null){
                    if (user.getUserSetting().isHandUp()) {
//                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mHand.getLayoutParams();
//                        if (holder.mLianmai.getVisibility() == View.GONE) {
//                            if (holder.mArrow.getVisibility() == View.GONE) {
//                                params.addRule(RelativeLayout.ALIGN_PARENT_END);
//                            } else {
//                                params.addRule(RelativeLayout.LEFT_OF, holder.mArrow.getId());
//                            }
//                        } else {
//                            params.addRule(RelativeLayout.LEFT_OF, holder.mLianmai.getId());
//                        }
//                        holder.mHand.setLayoutParams(params);
                        holder.mHand.setVisibility(View.VISIBLE);
                    } else {
                        holder.mHand.setVisibility(View.GONE);
                    }
                    if (!user.getUserSetting().isAllowChat()) {
//                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mGag.getLayoutParams();
//                        if (holder.mHand.getVisibility() == View.GONE) {
//                            if (holder.mLianmai.getVisibility() == View.GONE) {
//                                if (holder.mArrow.getVisibility() == View.GONE) {
//                                    params.addRule(RelativeLayout.ALIGN_PARENT_END);
//                                } else {
//                                    params.addRule(RelativeLayout.LEFT_OF, holder.mArrow.getId());
//                                }
//                            } else {
//                                params.addRule(RelativeLayout.LEFT_OF, holder.mHand.getId());
//                            }
//                        } else {
//                            params.addRule(RelativeLayout.LEFT_OF, holder.mLianmai.getId());
//                        }
//                        params.rightMargin = DensityUtil.dp2px(mContext, 10);
//                        holder.mGag.setLayoutParams(params);
                        holder.mGag.setVisibility(View.VISIBLE);
                    } else {
                        holder.mGag.setVisibility(View.GONE);
                    }
                    if (user.getUserSetting().isAllowDraw()) {
//                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mDraw.getLayoutParams();
//                        params.removeRule(RelativeLayout.ALIGN_PARENT_END);//移除旧的规则，否则新规则不生效
//                        params.removeRule(RelativeLayout.LEFT_OF);
//                        if (holder.mGag.getVisibility() == View.GONE) {
//                            if (holder.mHand.getVisibility() == View.GONE) {
//                                if (holder.mLianmai.getVisibility() == View.GONE) {
//                                    if (holder.mArrow.getVisibility() == View.GONE) {
//                                        params.addRule(RelativeLayout.ALIGN_PARENT_END);
//                                    } else {
//                                        params.addRule(RelativeLayout.LEFT_OF, holder.mArrow.getId());
//                                    }
//                                } else {
//                                    params.addRule(RelativeLayout.LEFT_OF, holder.mLianmai.getId());
//                                }
//                            } else {
//                                params.addRule(RelativeLayout.LEFT_OF, holder.mHand.getId());
//                            }
//                        } else {
//                            params.addRule(RelativeLayout.LEFT_OF, holder.mGag.getId());
//                        }
//                        params.rightMargin = DensityUtil.dp2px(mContext, 10);
//                        holder.mDraw.setLayoutParams(params);
                        holder.mDraw.setVisibility(View.VISIBLE);
                    } else {
                        holder.mDraw.setVisibility(View.GONE);
                    }
                }
            }

            if(!CCAtlasClient.getInstance().isRoomLive()){
                holder.mLianmai.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    static class GroupViewHolder {
        TextView parent_user_name;
        TextView parent_user_num;
        ImageView parent_image;
    }


    final class RoomUserViewHolder extends BaseRecycleAdapter.BaseViewHolder {

        private ImageView mDevice;
        private TextView mUserName;
        private ImageView mIdentity;
        private RelativeLayout mLianmai;
        private TextView mStatusWait;
        private ImageView mStatusing;
        private ImageView mGag;
        private ImageView mDraw;
        private ImageView mArrow;
        private ImageView mHand;
        private TextView mFlower;
        private TextView mCup;
        private TextView mHammer;
        private View itemView;

        RoomUserViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            mDevice = itemView.findViewById(R.id.id_user_device_icon);
            mUserName = itemView.findViewById(R.id.id_user_name);
            mIdentity = itemView.findViewById(R.id.id_user_identity);
            mLianmai = itemView.findViewById(R.id.id_user_status_lianmai);
            mStatusWait = itemView.findViewById(R.id.id_user_status_wait);
            mStatusing = itemView.findViewById(R.id.id_user_status_ing);
            mGag = itemView.findViewById(R.id.id_user_status_gag);
            mDraw = itemView.findViewById(R.id.id_user_status_draw);
            mArrow = itemView.findViewById(R.id.id_user_arrow);
            mHand = itemView.findViewById(R.id.id_user_status_hand);
            mFlower = itemView.findViewById(R.id.id_user_status_flower);
            mCup = itemView.findViewById(R.id.id_user_status_cup);
            mHammer = itemView.findViewById(R.id.id_user_status_hammer);
        }
    }

}
