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 { DeptSearchType } from '../types/dept-search.type';
import { CallMode } from '@ucap-webmessenger/core';
import { UserInfoSS } from '../models/user-info-ss';
import { WorkStatusType } from '@ucap-webmessenger/protocol-status';

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

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

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

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

  return bodyList;
};

export const decodeDeptUserData: ProtocolDecoder<DeptUserData> = (
  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] as CallMode,
      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,

      // [daesang]
      companyName: !!info[29] ? info[29].trim() : '',
      responsibilities: !!info[30] ? info[30].trim() : '',
      workstatus: info[31] as WorkStatusType,
      job: !!info[32] ? info[32].trim() : '',
      customerInfo: !!info[33] ? info[33].trim() : '',
      workplace: !!info[34] ? info[34].trim() : ''
    });
  });
  return decodeProtocolMessage(message, {
    divCd: message.bodyList[0],
    userInfos
  } as DeptUserData);
};

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);
};
