import { LocaleCode } from '@ucap-webmessenger/core';
import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  PacketBodyValue,
  ProtocolDecoder,
  ProtocolMessage,
  BodyStringDivider,
  ProtocolStream,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import {
  RoomInfo,
  UserInfoShort,
  UserInfo,
  EmployeeType
} from '@ucap-webmessenger/protocol-room';
import { RoleCode } from '@ucap-webmessenger/protocol-authentication';
import { EventType } from '@ucap-webmessenger/protocol-event';

export interface RoomRequest extends ProtocolRequest {
  // 0. 동료 씽크일시(s) cf)2019-09-24 16:51:42
  syncDate: string;
  // 언어코드
  localeCode: LocaleCode;
}

export interface RoomData extends ProtocolStream {
  // n. {대화방정보}...
  roomInfos: RoomInfo[];
}

export interface RoomUserData extends ProtocolStream {
  // 0. 대화방SEQ(s)
  roomSeq: string;
  // 1n. {참여자정보}
  userInfos: UserInfoShort[];
}

export interface RoomUserDetailData extends ProtocolStream {
  // 0. 대화방SEQ(s)
  roomSeq: string;
  // 1n. {참여자정보-D}
  userInfos: UserInfo[];
}

export interface RoomResponse extends ProtocolResponse {
  // 0. 동료 씽크일시(s) cf)2019-09-24 16:51:42
  syncDate: string;
}

export const encodeRoom: ProtocolEncoder<RoomRequest> = (req: RoomRequest) => {
  const bodyList: PacketBody[] = [];

  bodyList.push({ type: PacketBodyValue.String, value: req.syncDate || '' });
  bodyList.push({ type: PacketBodyValue.String, value: req.localeCode });
  return bodyList;
};

export const decodeRoom: ProtocolDecoder<RoomResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    syncDate: message.bodyList[0]
  } as RoomResponse);
};

export const decodeRoomData: ProtocolDecoder<RoomData> = (
  message: ProtocolMessage
) => {
  const roomInfos: RoomInfo[] = [];

  for (const body of message.bodyList) {
    const info = body.split(BodyStringDivider);
    if (info.length > 11) {
      roomInfos.push({
        roomSeq: info[0],
        roomType: info[1],
        roomName: info[2],
        finalEventType: info[3] as EventType,
        finalEventMessage: info[4],
        finalEventDate: info[5],
        joinUserCount: Number(info[6]),
        noReadCnt: Number(info[7]),
        receiveAlarm: info[8] !== 'N' ? true : false,
        isJoinRoom: info[9] === 'Y' ? true : false,
        expiredFileStdSeq: Number(info[10]),
        isTimeRoom: info[11] === 'Y' ? true : false,
        timeRoomInterval: info[11] !== 'Y' ? 0 : Number(info[12]) || 0
      });
    }
  }

  return decodeProtocolMessage(message, {
    roomSeq: message.bodyList[0],
    roomInfos
  } as RoomData);
};

export const decodeRoomUserData: ProtocolDecoder<RoomUserData> = (
  message: ProtocolMessage
) => {
  const userInfos: UserInfoShort[] = [];
  message.bodyList.slice(1).forEach(userInfo => {
    const info = userInfo.split(BodyStringDivider);
    userInfos.push({
      seq: Number(info[0]),
      name: info[1],
      profileImageFile: info[2],
      isJoinRoom: info[3],
      lastReadEventSeq: Number(info[4]),
      madn: info[5],
      hardSadn: info[6],
      fmcSadn: info[7],
      nameEn: info[8],
      nameCn: info[9],
      isPrivacyAgree: info[10] === 'Y' ? true : false,
      isValidLogin: info[11] === 'Y' ? true : false,
      employeeType: info[12] as EmployeeType,
      fontColor: info[13]
    });
  });

  return decodeProtocolMessage(message, {
    roomSeq: message.bodyList[0],
    userInfos
  } as RoomUserData);
};

export const decodeRoomUserDataDetail: ProtocolDecoder<RoomUserDetailData> = (
  message: ProtocolMessage
) => {
  const userInfos: UserInfo[] = [];
  message.bodyList.slice(1).forEach(userInfo => {
    const info = userInfo.split(BodyStringDivider);
    userInfos.push({
      seq: Number(info[0]),
      name: info[1],
      profileImageFile: info[2],
      grade: info[3],
      intro: info[4],
      companyCode: info[5],
      hpNumber: info[6],
      lineNumber: info[7],
      email: info[8],
      isMobile: info[9] === 'Y' ? true : false,
      deptName: info[10],
      isJoinRoom: info[11] === 'Y' ? true : false,
      lastReadEventSeq: info[12],
      isActive: info[13] === 'Y' ? true : false,
      roleCd: info[14] as RoleCode,
      employeeNum: info[15],
      madn: info[16],
      hardSadn: info[17],
      fmcSadn: info[18],
      nameEn: info[19],
      nameCn: info[20],
      gradeEn: info[21],
      gradeCn: info[22],
      deptNameEn: info[23],
      deptNameCn: info[24],
      callMode: info[25],
      isPrivacyAgree: info[26] === 'Y' ? true : false,
      isValidLogin: info[27] === 'Y' ? true : false,
      employeeType: info[28] as EmployeeType,
      id: info[29]
    });
  });

  return decodeProtocolMessage(message, {
    roomSeq: message.bodyList[0],
    userInfos
  } as RoomUserDetailData);
};
