import {
  ProtocolResponse,
  ProtocolDecoder,
  ProtocolMessage,
  BodyStringDivider,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { StatusCode } from '@ucap-webmessenger/core';
import { StatusInfo } from '../models/status-info';

export interface BuddyResponse extends ProtocolResponse {
  // {상태정보}...
  statusInfos: StatusInfo[];
}

export const decodeBuddy: ProtocolDecoder<BuddyResponse> = (
  message: ProtocolMessage
) => {
  const statusInfos: StatusInfo[] = [];
  message.bodyList.forEach(statusInfo => {
    const info = statusInfo.split(BodyStringDivider);
    statusInfos.push({
      userSeq: Number(info[0]),
      pcStatus: info[1] as StatusCode,
      phoneStatus: info[2] as StatusCode,
      mobileStatus: info[3] as StatusCode,
      conferenceStatus: info[4] as StatusCode,
      statusMessage: info[5],
      mobileConferenceStatus: info[6] as StatusCode,
      imessengerStatus: info[7] as StatusCode
    });
  });
  return decodeProtocolMessage(message, {
    statusInfos
  } as BuddyResponse);
};
