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

export interface UpdateRequest extends ProtocolRequest {
  // 알람여부(y)
  receiveAlarm: boolean;
  // 알람VIEW여부(y)
  displayAlarm: boolean;
  // 그룹알람여부(y)
  receiveGroupAlarm: boolean;
  // PC알람여부(y)
  receivePcAlarm: boolean;
}

export interface UpdateResponse extends ProtocolResponse {
  // 알람여부(y)
  receiveAlarm?: boolean;
  // 알람VIEW여부(y)
  displayAlarm?: boolean;
  // 그룹알람여부(y)
  receiveGroupAlarm?: boolean;
  // PC알람여부(y)
  receivePcAlarm?: boolean;
}

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

  bodyList.push({
    type: PacketBodyValue.String,
    value: req.receiveAlarm ? 'Y' : 'N'
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.displayAlarm ? 'Y' : 'N'
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.receiveGroupAlarm ? 'Y' : 'N'
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.receivePcAlarm ? 'Y' : 'N'
  });

  return bodyList;
};

export const decodeUpdate: ProtocolDecoder<UpdateResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    receiveAlarm: message.bodyList[0] === 'Y' ? true : false,
    displayAlarm: message.bodyList[1] === 'Y' ? true : false,
    receiveGroupAlarm: message.bodyList[2] === 'Y' ? true : false,
    receivePcAlarm: message.bodyList[3] === 'Y' ? true : false
  } as UpdateResponse);
};
