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

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

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

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

export const encodeUpdate: ProtocolEncoder<UpdateRequest> = (
  req: UpdateRequest
) => {
  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 decodeUpdate: ProtocolDecoder<UpdateResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    seq: message.bodyList[0],
    isFavorit: message.bodyList[1] === 'Y' ? true : false
  } as UpdateResponse);
};

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