#include "roo_display/image/png/png.h"

#include <cstdlib>
#include <memory>
#include <string>

#include "roo_display/color/color.h"
#include "roo_display/core/streamable.h"
#include "roo_io/fs/posix/posix_mount.h"
#include "roo_io/memory/memory_resource.h"

#ifdef Assert
#undef Assert
#endif

#include "testing.h"

using namespace testing;

namespace roo_display {
namespace {

constexpr char kPaletteImagePath[] = "/test/testdata/palette_opaque_8x4.png";
constexpr char kRgbaImagePath[] = "/test/testdata/rgba_alpha_8x4.png";

const unsigned char kRgbaAlpha8x4Png[] = {
    0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
    0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04,
    0x08, 0x06, 0x00, 0x00, 0x00, 0xb3, 0xcd, 0x7e, 0xf0, 0x00, 0x00, 0x00,
    0x20, 0x63, 0x48, 0x52, 0x4d, 0x00, 0x00, 0x7a, 0x26, 0x00, 0x00, 0x80,
    0x84, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00, 0x80, 0xe8, 0x00, 0x00, 0x75,
    0x30, 0x00, 0x00, 0xea, 0x60, 0x00, 0x00, 0x3a, 0x98, 0x00, 0x00, 0x17,
    0x70, 0x9c, 0xba, 0x51, 0x3c, 0x00, 0x00, 0x00, 0x06, 0x62, 0x4b, 0x47,
    0x44, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0xa0, 0xbd, 0xa7, 0x93, 0x00,
    0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xea, 0x05, 0x10, 0x0d,
    0x18, 0x32, 0xa1, 0xe6, 0x10, 0x21, 0x00, 0x00, 0x00, 0x6b, 0x49, 0x44,
    0x41, 0x54, 0x08, 0xd7, 0x2d, 0xcb, 0xa1, 0x19, 0x83, 0x40, 0x10, 0x05,
    0xe1, 0xb9, 0x2f, 0x2a, 0x12, 0x1b, 0x89, 0x8d, 0x3c, 0x8b, 0xc4, 0x46,
    0x6e, 0x0b, 0xd7, 0x42, 0xec, 0xca, 0xb4, 0x90, 0x16, 0xb6, 0x8a, 0x2d,
    0xe0, 0xb0, 0x48, 0x6c, 0x24, 0x16, 0xf7, 0x22, 0x60, 0xdc, 0x2f, 0xe6,
    0x36, 0x8c, 0x55, 0xf7, 0xe1, 0x51, 0x8e, 0xfd, 0x97, 0xb3, 0x35, 0x8d,
    0xcf, 0x5a, 0xb6, 0x75, 0xc9, 0xf7, 0xe7, 0xab, 0x69, 0x7e, 0x15, 0x00,
    0xc2, 0x50, 0x18, 0x7e, 0x41, 0x84, 0x39, 0x40, 0x66, 0x8a, 0xcc, 0x74,
    0x00, 0x23, 0x64, 0x84, 0x03, 0xa8, 0x23, 0xf5, 0x6b, 0x00, 0x04, 0x27,
    0x74, 0xe6, 0x00, 0xbd, 0x36, 0xf5, 0xda, 0xfc, 0x0f, 0x8d, 0xd9, 0x31,
    0x1d, 0x37, 0xcc, 0xea, 0x01, 0x00, 0x00, 0x00, 0x25, 0x74, 0x45, 0x58,
    0x74, 0x64, 0x61, 0x74, 0x65, 0x3a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65,
    0x00, 0x32, 0x30, 0x32, 0x36, 0x2d, 0x30, 0x35, 0x2d, 0x31, 0x36, 0x54,
    0x31, 0x33, 0x3a, 0x32, 0x34, 0x3a, 0x35, 0x30, 0x2b, 0x30, 0x30, 0x3a,
    0x30, 0x30, 0xad, 0x72, 0x89, 0x08, 0x00, 0x00, 0x00, 0x25, 0x74, 0x45,
    0x58, 0x74, 0x64, 0x61, 0x74, 0x65, 0x3a, 0x6d, 0x6f, 0x64, 0x69, 0x66,
    0x79, 0x00, 0x32, 0x30, 0x32, 0x36, 0x2d, 0x30, 0x35, 0x2d, 0x31, 0x36,
    0x54, 0x31, 0x33, 0x3a, 0x32, 0x34, 0x3a, 0x35, 0x30, 0x2b, 0x30, 0x30,
    0x3a, 0x30, 0x30, 0xdc, 0x2f, 0x31, 0xb4, 0x00, 0x00, 0x00, 0x28, 0x74,
    0x45, 0x58, 0x74, 0x64, 0x61, 0x74, 0x65, 0x3a, 0x74, 0x69, 0x6d, 0x65,
    0x73, 0x74, 0x61, 0x6d, 0x70, 0x00, 0x32, 0x30, 0x32, 0x36, 0x2d, 0x30,
    0x35, 0x2d, 0x31, 0x36, 0x54, 0x31, 0x33, 0x3a, 0x32, 0x34, 0x3a, 0x35,
    0x30, 0x2b, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x8b, 0x3a, 0x10, 0x6b, 0x00,
    0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
};

std::string RunfilesRoot() {
  const char* test_srcdir = std::getenv("TEST_SRCDIR");
  const char* test_workspace = std::getenv("TEST_WORKSPACE");
  if (test_srcdir == nullptr || test_workspace == nullptr) {
    return {};
  }
  return std::string(test_srcdir) + "/" + test_workspace;
}

class PosixTestFilesystem : public roo_io::Filesystem {
 public:
  explicit PosixTestFilesystem(std::string mount_point)
      : mount_point_(std::move(mount_point)) {}

  MediaPresence checkMediaPresence() override { return kMediaPresent; }

 protected:
  roo_io::MountImpl::MountResult mountImpl(
      std::function<void()> unmount_fn) override {
    return roo_io::MountImpl::Mounted(
        std::unique_ptr<roo_io::MountImpl>(new roo_io::PosixMountImpl(
            mount_point_.c_str(), true, std::move(unmount_fn))));
  }

  void unmountImpl() override {}

 private:
  std::string mount_point_;
};

void Draw(DisplayDevice& output, const Drawable& object,
          FillMode fill_mode = FillMode::kVisible,
          BlendingMode blending_mode = BlendingMode::kSourceOver,
          Color bgcolor = color::Transparent) {
  output.begin();
  Box clip_box(0, 0, output.effective_width() - 1,
               output.effective_height() - 1);
  Surface s(output, 0, 0, clip_box, false, bgcolor, fill_mode, blending_mode);
  s.drawObject(object);
  output.end();
}

}  // namespace

TEST(Png, PaletteOpaqueFile) {
  std::string runfiles_root = RunfilesRoot();
  ASSERT_FALSE(runfiles_root.empty());

  PosixTestFilesystem fs(runfiles_root);
  PngDecoder decoder;
  PngFile image(decoder, fs, kPaletteImagePath);

  FakeOffscreen<Argb8888> test_screen(8, 4, color::Transparent);
  Draw(test_screen, image, FillMode::kVisible, BlendingMode::kSource);

  EXPECT_THAT(test_screen,
              MatchesContent(Argb8888(), 8, 4,
                             "FFFF0000 FF00FF00 FF0000FF FFFFFF00 "
                             "FF00FFFF FFFF00FF FFFFFFFF FF000000"
                             "FF804000 FFFF8000 FF80FF00 FF008080 "
                             "FF8000FF FFFF80C0 FF808080 FF004080"
                             "FF000000 FFFFFFFF FFFF00FF FF00FFFF "
                             "FFFFFF00 FF0000FF FF00FF00 FFFF0000"
                             "FF004080 FF808080 FFFF80C0 FF8000FF "
                             "FF008080 FF80FF00 FFFF8000 FF804000"));
}

TEST(Png, RgbaFile) {
  std::string runfiles_root = RunfilesRoot();
  ASSERT_FALSE(runfiles_root.empty());

  PosixTestFilesystem fs(runfiles_root);
  PngDecoder decoder;
  PngFile image(decoder, fs, kRgbaImagePath);

  FakeOffscreen<Argb8888> test_screen(8, 4, color::Transparent);
  Draw(test_screen, image, FillMode::kVisible, BlendingMode::kSource);

  EXPECT_THAT(test_screen,
              MatchesContent(Argb8888(), 8, 4,
                             "FF112233 80112233 00000000 FF445566 "
                             "80445566 00000000 FF778899 80778899"
                             "00000000 FFAA5500 80AA5500 00000000 "
                             "FF00AA55 8000AA55 00000000 FFC0C0C0"
                             "80C0C0C0 00000000 FF5500AA 805500AA "
                             "00000000 FFFFCC00 80FFCC00 00000000"
                             "FF000000 80000000 00000000 FFFFFFFF "
                             "80FFFFFF 00000000 FFCC3366 80CC3366"));
}

TEST(Png, RgbaMemoryResource) {
  roo_io::MemoryResource<const roo::byte*> resource(
      reinterpret_cast<const roo::byte*>(kRgbaAlpha8x4Png),
      reinterpret_cast<const roo::byte*>(kRgbaAlpha8x4Png) +
          sizeof(kRgbaAlpha8x4Png));
  PngDecoder decoder;
  PngImage image(decoder, resource);

  FakeOffscreen<Argb8888> test_screen(8, 4, color::Transparent);
  Draw(test_screen, image, FillMode::kVisible, BlendingMode::kSource);

  EXPECT_THAT(test_screen,
              MatchesContent(Argb8888(), 8, 4,
                             "FF112233 80112233 00000000 FF445566 "
                             "80445566 00000000 FF778899 80778899"
                             "00000000 FFAA5500 80AA5500 00000000 "
                             "FF00AA55 8000AA55 00000000 FFC0C0C0"
                             "80C0C0C0 00000000 FF5500AA 805500AA "
                             "00000000 FFFFCC00 80FFCC00 00000000"
                             "FF000000 80000000 00000000 FFFFFFFF "
                             "80FFFFFF 00000000 FFCC3366 80CC3366"));
}

}  // namespace roo_display