package com.ym.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by weiqinxiao on 2019/2/28.
 */
public class RoomResult {
    private @Getter @Setter String roomId;
    //课程结束后是否自动关闭课程
    private @Getter @Setter Boolean autoCloseFlag = true;
    //课程结束后多少分钟关闭网络教室
    private @Getter @Setter String autoCloseNetworkRoomTime;
    private @Getter @Setter Date startTime;
    private @Getter @Setter int surplusTime;
    private @Getter @Setter String imToken;
    private @Getter @Setter String authorization;
    private @Getter List<MemberResult> members = new ArrayList<>();
    private @Getter @Setter String display;
    private @Getter @Setter List<WhiteboardResult> whiteboards = new ArrayList<>();
    private @Getter @Setter MemberResult userInfo;

    @Data
    public static class MemberResult {
        String userId;
        String userName;
        String headUrl;
        int role;
        Date joinTime;
        boolean camera;
        boolean microphone;
        boolean handUpOn;
        ExamSongDownloadData examSongDownloadJson;
        CustomMessage playMidiJson;
    }

    @Data
    public static class WhiteboardResult {
        String whiteboardId;
        String name;
        int curPg;
    }

    public void setMembers(List<RoomMember> roomMemberList, Map<Integer,String> midiMap,Map<Integer,String> examSongMap) {
        for (RoomMember member : roomMemberList) {
            MemberResult result = new MemberResult();
            result.setUserId(member.getUid());
            result.setJoinTime(member.getJoinDt());
            result.setRole(member.getRole());
            result.setMicrophone(member.isMic());
            result.setCamera(member.isCamera());
            result.setUserName(member.getName());
            result.setHandUpOn(member.isHand());
            result.setHeadUrl(member.getHeadUrl());
            result.setPlayMidiJson(JSONObject.parseObject(midiMap.get(Integer.parseInt(member.getUid())),CustomMessage.class));
            ExamSongDownloadData examSongDownloadData;
            String json = examSongMap.get(Integer.parseInt(member.getUid()));
            if(StringUtils.isEmpty(json)){
                examSongDownloadData = new ExamSongDownloadData();
            }else {
                examSongDownloadData = JSON.parseObject(json,ExamSongDownloadData.class);
            }
            result.setExamSongDownloadJson(examSongDownloadData);
            members.add(result);
        }
    }

    public void setWhiteboards(List<Whiteboard> whiteboardList) {
        for (Whiteboard wb : whiteboardList) {
            WhiteboardResult r = new WhiteboardResult();
            r.setName(wb.getName());
            r.setWhiteboardId(wb.getWbid());
            r.setCurPg(wb.getCurPg());
            whiteboards.add(r);
        }
    }
}
