import {
  ProtocolRequest,
  ProtocolEncoder,
  PacketBody,
  ProtocolDecoder,
  ProtocolMessage,
  PacketBodyValue,
  BodyStringDivider,
  ProtocolStream,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { EmployeeType } from '@ucap-webmessenger/protocol-room';
import { RoleCode } from '@ucap-webmessenger/protocol-authentication';
import { UserInfoF } from '../models/user-info-f';
import { UserInfoSS } from '../models/user-info-ss';

export interface UserSeqRequest extends ProtocolRequest {
  // DivCD(s)
  divCd: string;
  // 로그인IDs(s)
  loginIds: string[];
  // 기관코드(s)
  companyCode: string;
  // 상세정보여부(s)
  isDetail: boolean;
  // 발신자회사코드(s)
  senderCompanyCode: string;
  // 발신자임직원유형(s)
  senderEmployeeType: EmployeeType;
}

export interface UserSeqData extends ProtocolStream {
  // DivCD(s)
  divCd: string;
  // {사용자정보-F}...
  userInfos: UserInfoF[];
}
export interface UserSeqData2 extends ProtocolStream {
  // DivCD(s)
  divCd: string;
  // {사용자정보-SS}...
  userInfos: UserInfoSS[];
}

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

  bodyList.push(
    { type: PacketBodyValue.String, value: req.divCd },
    { type: PacketBodyValue.String, value: req.loginIds.join(',') },
    { type: PacketBodyValue.String, value: req.companyCode },
    { type: PacketBodyValue.String, value: req.isDetail ? 'Y' : 'N' },
    { type: PacketBodyValue.String, value: req.senderCompanyCode },
    { type: PacketBodyValue.String, value: req.senderEmployeeType }
  );

  return bodyList;
};

export const decodeUserSeqData: ProtocolDecoder<UserSeqData> = (
  message: ProtocolMessage
) => {
  const userInfos: UserInfoF[] = [];
  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],
      isFavorit: info[11] === 'Y' ? true : false,
      isBuddy: info[12] === 'Y' ? true : false,
      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],
      isPrivacyAgree: info[25] === 'Y' ? true : false,
      isValidLogin: info[26] === 'Y' ? true : false,
      employeeType: info[27] as EmployeeType,
      nickName: info[28]
    } as UserInfoF);
  });
  return decodeProtocolMessage(message, {
    divCd: message.bodyList[0],
    userInfos
  } as UserSeqData);
};

export const decodeUserSeqData2: ProtocolDecoder<UserSeqData2> = (
  message: ProtocolMessage
) => {
  const userInfos: UserInfoSS[] = [];
  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],
      order: info[11],
      isActive: info[12] === 'Y' ? true : false,
      roleCd: info[13] as RoleCode,
      employeeNum: info[14],
      madn: info[15],
      hardSadn: info[16],
      fmcSadn: info[17],
      callMode: info[18],
      nameEn: info[19],
      nameCn: info[20],
      gradeEn: info[21],
      gradeCn: info[22],
      deptNameEn: info[23],
      deptNameCn: info[24],
      deptSeq: info[25],
      isPrivacyAgree: info[26] === 'Y' ? true : false,
      isValidLogin: info[27] === 'Y' ? true : false,
      employeeType: info[28] as EmployeeType
    });
  });
  return decodeProtocolMessage(message, {
    divCd: message.bodyList[0],
    userInfos
  } as UserSeqData2);
};

// export const decodeDeptUser: ProtocolDecoder<DeptUserResponse> = (
//   message: ProtocolMessage
// ) => {
//   return decodeProtocolMessage(message, {
//     divCd: message.bodyList[0],
//     pageTotalCount: message.bodyList[1],
//     pageListCount: message.bodyList[2],
//     pageCurrent: message.bodyList[3]
//   } as DeptUserResponse);
// };
