import {
  ProtocolRequest,
  ProtocolResponse,
  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 { UserDnSearchType } from '../types/user-dn-search.type';
import { UserInfoDN } from '../models/user-info-dn';

export interface UserDnRequest extends ProtocolRequest {
  // DivCD(s)
  divCd: string;
  // 기관코드(s)
  companyCode: string;
  // 부서SEQ(n)
  seq?: number;
  // 검색string(s)
  search?: string;
  // 검색구분(s)
  searchType: UserDnSearchType;
  // 요청리스트(n) 사용안하면 0 최대 200
  pageListCount?: number;
  // 요청페이지(n) 사용안하면 0 처음은 1
  pageCurrent?: number;
  // 발신자회사코드(s)
  senderCompanyCode: string;
  // 발신자임직원유형(s)
  senderEmployeeType: EmployeeType;
}

export interface UserDnData extends ProtocolStream {
  // DivCD(s)
  divCd: string;
  // {부서정보-S}...
  userDnInfos: UserInfoDN[];
}
export interface UserDnResponse extends ProtocolResponse {
  // DivCD(s)
  divCd: string;
  // 전체갯수(n)
  pageTotalCount: number;
  // 요청리스트(n)
  pageListCount?: number;
  // 페이지(n)
  pageCurrent?: number;
}

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

  bodyList.push(
    { type: PacketBodyValue.String, value: req.divCd },
    { type: PacketBodyValue.String, value: req.companyCode },
    { type: PacketBodyValue.Integer, value: req.seq || 0 },
    { type: PacketBodyValue.String, value: req.search || '' },
    { type: PacketBodyValue.String, value: req.searchType },
    {
      type: PacketBodyValue.Integer,
      value: req.pageListCount || 0
    },
    { type: PacketBodyValue.Integer, value: req.pageCurrent || 0 },
    { type: PacketBodyValue.String, value: req.senderCompanyCode },
    {
      type: PacketBodyValue.String,
      value: req.senderEmployeeType
    }
  );

  return bodyList;
};

export const decodeUserDnData: ProtocolDecoder<UserDnData> = (
  message: ProtocolMessage
) => {
  const userDnInfos: UserInfoDN[] = [];
  message.bodyList.slice(1).forEach(userDnInfo => {
    const info = userDnInfo.split(BodyStringDivider);
    userDnInfos.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,
      order: info[10],
      isActive: info[11] === 'Y' ? true : false,
      roleCd: info[12] as RoleCode,
      employeeNum: info[13],
      deptName: info[14],
      madn: info[15],
      hardSadn: info[16],
      fmcSadn: info[17],
      nameEn: info[18],
      nameCn: info[19],
      gradeEn: info[20],
      gradeCn: info[21],
      deptNameEn: info[22],
      deptNameCn: info[23]
    });
  });
  return decodeProtocolMessage(message, {
    divCd: message.bodyList[0],
    userDnInfos
  } as UserDnData);
};

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