// MIT License
// Copyright (c) 2026 by Peter Siegmund (mars3142)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "cartridge/media.h"

#include <utility>
#include <format>

namespace cartridge {

Media::Media(std::string objectType, const std::vector<uint8_t> &data)
    : objectType(std::move(objectType)), data(data) {}

std::unique_ptr<Media> Media::create(BinaryReader &reader, const int objectId,
                                     const int address) {
  try {
    int objectType = 0;
    reader.seek(address, SeekOrigin::Begin);
    std::vector<uint8_t> data = (objectId == 0)
        ? readMediaData(reader)
        : (reader.getByte() != 0)
            ? (objectType = reader.getLong(), readMediaData(reader))
            : std::vector<uint8_t>{};

    if (!data.empty()) {
      return std::make_unique<Media>(getObjectTypeString(objectType), data);
    }
    return nullptr;
  } catch ([[maybe_unused]] const std::exception &ex) {
    return nullptr;
  }
}

std::vector<uint8_t> Media::readMediaData(BinaryReader &reader) {
  const int32_t length = reader.getLong();
  std::vector<uint8_t> data(length);
  for (int32_t i = 0; i < length; ++i) {
    data[i] = reader.getByte();
  }
  return data;
}

std::string Media::getObjectTypeString(const int objectType) {
  switch (objectType) {
  case -1: return "deleted";
  case 0:  return "luac";
  case 1:  return "bmp";
  case 2:  return "png";
  case 3:  return "jpg";
  case 4:  return "gif";
  case 17: return "wav";
  case 18: return "mp3";
  case 19: return "fdl";
  case 20: return "snd";
  case 21: return "ogg";
  case 33: return "swf";
  case 49: return "txt";
  default: return std::format("invalid ({})", objectType);
  }
}

bool Media::operator==(const Media &other) const {
  return objectType == other.objectType && data == other.data;
}

std::size_t Media::hash() const {
  const std::size_t h1 = std::hash<std::string>{}(objectType);
  std::size_t h2 = 0;
  for (const auto b : data)
    h2 ^= std::hash<uint8_t>{}(b) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
  return h1 ^ (h2 << 1);
}

} // namespace cartridge
