import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  PacketBodyValue,
  ProtocolDecoder,
  ProtocolMessage,
  BodyStringDivider,
  ProtocolStream,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { UserInfo } from '../types/userInfo';
import { EmployeeType } from '@ucap-webmessenger/protocol-room';
import { BuddyInfo } from '../models/buddy-info';

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

export interface BuddyData extends ProtocolStream {
  // n. {동료정보-S}...
  buddyInfos: BuddyInfo[];
}

export interface BuddyDetailData extends ProtocolStream {
  // n. {동료정보-S}...
  buddyInfos: UserInfo[];
}

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

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

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

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

export const decodeBuddyData: ProtocolDecoder<BuddyData> = (
  message: ProtocolMessage
) => {
  const buddyInfos: BuddyInfo[] = [];
  message.bodyList.forEach(buddyinfo => {
    const info = buddyinfo.split(BodyStringDivider);
    buddyInfos.push({
      seq: info[0],
      isFavorit: info[1] === 'Y' ? true : false,
      isBuddy: info[2] === 'Y' ? true : false
    });
  });
  return decodeProtocolMessage(message, {
    buddyInfos
  } as BuddyData);
};

export const decodeBuddyDetailData: ProtocolDecoder<BuddyDetailData> = (
  message: ProtocolMessage
) => {
  const buddyInfos: UserInfo[] = [];
  message.bodyList.forEach(buddyinfo => {
    const info = buddyinfo.split(BodyStringDivider);

    buddyInfos.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],
      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]
    });
  });
  return decodeProtocolMessage(message, {
    buddyInfos
  } as BuddyDetailData);
};
