import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  PacketBodyValue,
  ProtocolDecoder,
  ProtocolMessage,
  ProtocolNotification,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { UserInfoUpdateType } from '../types/user-info-update.type';

export interface UserRequest extends ProtocolRequest {
  // 타입(s)
  type: UserInfoUpdateType;
  // 정보(s)
  info: string;
}

export interface UserResponse extends ProtocolResponse {
  // 타입(s)
  type: UserInfoUpdateType;
  // 정보(s)
  info: string;
}

export interface UserNotification extends ProtocolNotification {
  // 타입(s)
  type: UserInfoUpdateType;
  // 정보(s)
  info: string;
}

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

  bodyList.push(
    { type: PacketBodyValue.String, value: req.type },
    { type: PacketBodyValue.String, value: req.info }
  );

  return bodyList;
};

export const decodeUser: ProtocolDecoder<UserResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    type: message.bodyList[0] as UserInfoUpdateType,
    info: message.bodyList[1]
  } as UserResponse);
};

export const decodeUserNotification: ProtocolDecoder<UserNotification> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    type: message.bodyList[0] as UserInfoUpdateType,
    info: message.bodyList[1]
  } as UserNotification);
};
