import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  ProtocolDecoder,
  ProtocolMessage,
  PacketBodyValue,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { CallMode } from '@ucap-webmessenger/core';

export interface DataUserExtRequest extends ProtocolRequest {
  // DivCD(s)
  divCd: string;
  // 사용자SEQ(n)
  seq: number;
}
export interface DataUserExtResponse extends ProtocolResponse {
  // DivCD(s)
  divCd: string;
  // 사용자SEQ(n)
  seq: number;
  // CallMode(s)
  callMode: CallMode;
  // HARDPHONE_SADN
  hardSadn: string;
  // FMC_SADN
  fmcSadn: string;
}

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

  bodyList.push(
    { type: PacketBodyValue.String, value: req.divCd },
    { type: PacketBodyValue.Integer, value: req.seq }
  );

  return bodyList;
};

export const decodeDataUserExt: ProtocolDecoder<DataUserExtResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    divCd: message.bodyList[0],
    seq: message.bodyList[1],
    callMode: message.bodyList[2] as CallMode,
    hardSadn: message.bodyList[3],
    fmcSadn: message.bodyList[4]
  } as DataUserExtResponse);
};
