import PlaybackDevice from "./playback-device.js";
import User from "./user.js";
import Integration from "./integration.js";
import Command from "./command.js";
import Remote from "./remote.js";

const Serializer = (function () {
  function deserializeUser(object) {
    object.isAdmin = object.is_admin;
    return new User(object);
  }

  function deserializeUsers(objects) {
    return objects.map((object) => deserializeUser(object));
  }

  function deserializeDevice(object) {
    return new PlaybackDevice(object);
  }

  function deserializeDevices(objects) {
    if (!objects) return [];
    return objects.map((object) => deserializeDevice(object));
  }

  function deserializeIntegration(object) {
    return new Integration(object);
  }

  function deserializeIntegrations(objects) {
    if (!objects) return [];
    return objects.map((object) => deserializeIntegration(object));
  }

  function serializeCommand(command) {
    return {
      id: command.getId(),
      protocol: command.getProtocol(),
      commandNumber: command.getCommandNumber(),
      device: command.getDevice(),
      commandType: command.getCommandType(),
      title: command.getTitle(),
    };
  }

  function serializeCommands(commands) {
    if (!commands) return [];
    return commands.map((command) => serializeCommand(command));
  }

  function deserializeCommand(object) {
    return new Command(object);
  }

  function deserializeCommands(objects) {
    if (!objects) return [];
    return objects.map((object) => deserializeCommand(object));
  }

  function serializeRemote(remote) {
    return {
      id: remote.getId(),
      title: remote.getTitle(),
      commands: serializeCommands(remote.getCommands()),
    };
  }

  function deserializeRemote(object) {
    return new Remote({
      id: object.id,
      title: object.title,
      commands: deserializeCommands(object.commands),
    });
  }

  function deserializeRemotes(objects) {
    if (!objects) return [];
    return objects.map((object) => deserializeRemote(object));
  }

  return {
    deserializeUser,
    deserializeUsers,
    deserializeDevice,
    deserializeDevices,
    deserializeIntegration,
    deserializeIntegrations,
    serializeCommand,
    serializeCommands,
    deserializeCommand,
    deserializeCommands,
    serializeRemote,
    deserializeRemote,
    deserializeRemotes,
  };
})();

export default Serializer;
