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

export interface BuddyUpdateRequest extends ProtocolRequest {
  // 0. 사용자SEQ(n)
  seq: number;
  // 1. 즐겨찾기여부(y)
  isFavorit: boolean;
}

export interface BuddyUpdateResponse extends ProtocolResponse {
  // 0. 사용자SEQ(n)
  seq: number;
  // 1. 즐겨찾기여부(y)
  isFavorit: boolean;
}

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

  bodyList.push({ type: PacketBodyValue.Integer, value: req.seq });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.isFavorit ? 'Y' : 'N'
  });

  return bodyList;
};

export const decodeBuddyUpdate: ProtocolDecoder<BuddyUpdateResponse> = (
  message: ProtocolMessage
) => {
  return {
    seq: message.bodyList[0],
    isFavorit: message.bodyList[1] === 'Y' ? true : false
  } as BuddyUpdateResponse;
};
