import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  PacketBodyValue,
  ProtocolDecoder,
  ProtocolMessage,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { MessageIndexType } from '../types/message-index.type';

export interface MessageUpdateRequest extends ProtocolRequest {
  // 상태타입(s)
  index: MessageIndexType;
  // 상태메시지(s)
  statusMessage: string;
}

export interface MessageUpdateResponse extends ProtocolResponse {
  // 상태타입(s)
  index: MessageIndexType;
  // 상태메시지(s)
  statusMessage: string;
}

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

  bodyList.push(
    { type: PacketBodyValue.Integer, value: req.index },
    { type: PacketBodyValue.String, value: req.statusMessage }
  );
  return bodyList;
};

export const decodeMessageUpdate: ProtocolDecoder<MessageUpdateResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    index: message.bodyList[0] as MessageIndexType,
    statusMessage: message.bodyList[1]
  } as MessageUpdateResponse);
};
