import {
  ProtocolDecoder,
  ProtocolMessage,
  BodyStringDivider,
  decodeProtocolMessage,
  ProtocolResponse
} from '@ucap-webmessenger/protocol';
import { EmployeeType } from '@ucap-webmessenger/protocol-room';
import { RoleCode } from '@ucap-webmessenger/protocol-authentication';
import {
  UserInfoSS,
  DataUserResponse
} from '@ucap-webmessenger/protocol-query';
import { WorkStatusType } from '@ucap-webmessenger/protocol-status';

// export interface UserInfoSSForDaesang extends UserInfoSS {
//   /****** For daesang ******/
//   /** 사업장 */
//   workplace: string;
//   /** 담당업무 */
//   responsibilities: string;
//   /** 업무 상태 */
//   workstatus: string;
// }

// export interface DataUserResponse extends ProtocolResponse {
//   // DivCD(s)
//   divCd: string;
//   // {사용자정보-SS}
//   userInfo?: UserInfoSS;
// }

export const decodeDataUser: ProtocolDecoder<DataUserResponse> = (
  message: ProtocolMessage
) => {
  let userInfo: UserInfoSS;
  if (message.bodyList.length > 1) {
    const info = message.bodyList[1].split(BodyStringDivider);
    userInfo = {
      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,

      // [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],
    userInfo
  } as DataUserResponse);
};
