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

export interface LogoutRemoteRequest extends ProtocolRequest {
  targetDeviceType?: DeviceType;
  requestDeviceType?: DeviceType;
}
export interface LogoutRemoteResponse extends ProtocolResponse {
  targetDeviceType?: DeviceType;
  resultCode?: number;
}

export interface LogoutRemoteNotification extends ProtocolNotification {
  requestDeviceType?: DeviceType;
}

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

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

  return bodyList;
};

export const decodeLogoutRemote: ProtocolDecoder<LogoutRemoteResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    targetDeviceType: message.bodyList[0],
    resultCode: message.bodyList[1]
  } as LogoutRemoteResponse);
};

export const decodeLogoutRemoteNotification: ProtocolDecoder<LogoutRemoteNotification> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    requestDeviceType: message.bodyList[0]
  } as LogoutRemoteNotification);
};
