#include "roo_display/image/jpeg/jpeg.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"
#include "testing.h"

using namespace testing;

namespace roo_display {
namespace {

constexpr char kColorImagePath[] = "/test/testdata/color_blocks_9x9.jpg";
constexpr char kGrayscaleImagePath[] =
    "/test/testdata/grayscale_blocks_9x9.jpg";

const unsigned char kColorBlocks9x9Jpg[] = {
    0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
    0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
    0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x01, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0x09, 0x00, 0x09, 0x03,
    0x01, 0x11, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00,
    0x17, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x08, 0x09, 0xff, 0xc4,
    0x00, 0x24, 0x10, 0x00, 0x01, 0x04, 0x02, 0x01, 0x02, 0x07, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, 0x05, 0x06, 0x07,
    0x02, 0x08, 0x00, 0x01, 0x14, 0x18, 0x38, 0x56, 0x77, 0x95, 0xb5, 0xd6,
    0xff, 0xc4, 0x00, 0x17, 0x01, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x07, 0x0a,
    0x06, 0xff, 0xc4, 0x00, 0x2b, 0x11, 0x00, 0x01, 0x03, 0x03, 0x02, 0x02,
    0x0a, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x04,
    0x06, 0x07, 0x00, 0x03, 0x08, 0x02, 0x09, 0x01, 0x96, 0x0a, 0x14, 0x19,
    0x38, 0x39, 0x56, 0x58, 0x75, 0x78, 0xb6, 0xb7, 0xd2, 0xd8, 0xff, 0xda,
    0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00,
    0x16, 0xe8, 0xd3, 0x9a, 0x0d, 0x58, 0x52, 0x33, 0x7d, 0x4f, 0xad, 0x2e,
    0xcb, 0x59, 0xfc, 0xdb, 0x30, 0x48, 0x9c, 0xfc, 0x71, 0x24, 0x8d, 0xe5,
    0x64, 0xfe, 0xb1, 0x62, 0xce, 0x59, 0x15, 0x37, 0x00, 0x9e, 0xd0, 0xa8,
    0x23, 0xf1, 0x90, 0xc0, 0x45, 0xba, 0xbb, 0xee, 0x3a, 0x0e, 0xe2, 0x8e,
    0x46, 0x66, 0xa6, 0x26, 0xe0, 0x99, 0x0a, 0x66, 0x50, 0x28, 0xa7, 0x89,
    0xe8, 0xff, 0x00, 0xc6, 0x0e, 0x67, 0x12, 0xa1, 0x9b, 0xa5, 0x4c, 0xe3,
    0x2e, 0xb4, 0x9c, 0xb8, 0xdb, 0x30, 0xc9, 0x91, 0x02, 0x1b, 0x0d, 0xd2,
    0x02, 0x94, 0x46, 0xba, 0x80, 0xb9, 0x21, 0x20, 0x8d, 0xd4, 0x8a, 0xdc,
    0x0d, 0x4b, 0x2a, 0x5c, 0xf2, 0x09, 0x43, 0x77, 0x88, 0xcf, 0x04, 0xd3,
    0x5b, 0x54, 0x09, 0xc2, 0x84, 0x7d, 0x8d, 0x56, 0x81, 0x5f, 0x52, 0x3f,
    0x4a, 0x64, 0x05, 0xee, 0x2e, 0x2d, 0xb8, 0x5b, 0x2a, 0x61, 0x9f, 0x77,
    0x73, 0xc6, 0x1c, 0xb7, 0xcd, 0xc6, 0x90, 0xf8, 0x83, 0x0b, 0xe3, 0x1c,
    0x68, 0xbf, 0x1b, 0x4c, 0x93, 0x14, 0x58, 0x50, 0x42, 0x92, 0xad, 0x05,
    0xb7, 0x55, 0x64, 0xad, 0xc6, 0x2a, 0x74, 0x6c, 0xbe, 0x07, 0xe5, 0x69,
    0x00, 0xc2, 0xb3, 0x52, 0x44, 0x92, 0xc1, 0x6f, 0xaf, 0x58, 0x0a, 0x3f,
    0x3e, 0x39, 0x38, 0xb3, 0xda, 0x94, 0x28, 0xe2, 0x2d, 0x38, 0xd2, 0xe6,
    0x86, 0xe2, 0x57, 0x82, 0x7a, 0x03, 0xd6, 0x76, 0x67, 0xc8, 0x30, 0xfe,
    0x17, 0x95, 0xb7, 0xda, 0x5e, 0xc5, 0xf3, 0x1b, 0x53, 0x93, 0x24, 0x7f,
    0xd2, 0x9d, 0x9d, 0x6b, 0x61, 0xcf, 0x5b, 0x19, 0x01, 0xc8, 0x4f, 0xef,
    0xe5, 0x3a, 0xbf, 0x37, 0x7b, 0xcc, 0xc4, 0x07, 0xda, 0xb0, 0x7e, 0xf2,
    0xc4, 0xe4, 0xe2, 0xec, 0xaf, 0xe0, 0xd5, 0x95, 0x3f, 0x2f, 0xd6, 0x7d,
    0x63, 0x15, 0x69, 0xbb, 0xbb, 0xd7, 0x76, 0x29, 0x47, 0xd8, 0x23, 0x4f,
    0xcc, 0x6d, 0xda, 0x46, 0xf1, 0x8b, 0x52, 0x41, 0x5f, 0xff, 0xd9,
};

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(Jpeg, ColorFile) {
  std::string runfiles_root = RunfilesRoot();
  ASSERT_FALSE(runfiles_root.empty());

  PosixTestFilesystem fs(runfiles_root);
  JpegDecoder decoder;
  JpegFile image(decoder, fs, kColorImagePath);

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

  EXPECT_THAT(test_screen,
              MatchesContent(Argb8888(), 9, 9,
                             "FFFD0100 FFFB0000 FFFB0100 FF01FF02 FF00FF00 "
                             "FF00FF00 FF0001FB FF0001FD FF0001FD"
                             "FFFC0000 FFFB0100 FFFD0100 FF00FF02 FF00FE00 "
                             "FF00FE00 FF0001FC FF0001FB FF0001FB"
                             "FFFC0000 FFFC0200 FFFB0100 FF01FF00 FF01FF00 "
                             "FF00FF00 FF0002FD FF0001FD FF0000FB"
                             "FFFEFE00 FFFFFF00 FFFFFF00 FF00FFFD FF00FEFC "
                             "FF00FFFE FFFE00FD FFFE01FC FFFC00FB"
                             "FFFDFE00 FFFDFF00 FFFDFF00 FF00FFFF FF00FFFF "
                             "FF00FFFC FFFC00FC FFFD01FD FFFD01FC"
                             "FFFDFE00 FFFEFF00 FFFEFE00 FF00FFFC FF00FFFF "
                             "FF00FFFC FFFE00FD FFFD01FD FFFC00FC"
                             "FFFC8000 FFFE8001 FFFD8100 FF7C00FC FF7F00FE "
                             "FF7D01FD FF01807F FF008080 FF008080"
                             "FFFE8000 FFFE8000 FFFD8100 FF7C00FD FF7C00FC "
                             "FF7C00FD FF00817F FF018080 FF007F7F"
                             "FFFC8000 FFFD7F00 FFFE8001 FF7C00FD FF7D01FE "
                             "FF7E00FD FF007F7F FF01807F FF018080"));
}

TEST(Jpeg, GrayscaleFile) {
  std::string runfiles_root = RunfilesRoot();
  ASSERT_FALSE(runfiles_root.empty());

  PosixTestFilesystem fs(runfiles_root);
  JpegDecoder decoder;
  JpegFile image(decoder, fs, kGrayscaleImagePath);

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

  EXPECT_THAT(test_screen,
              MatchesContent(Argb8888(), 9, 9,
                             "FF000000 FF000000 FF000000 FF3F3F3F FF3F3F3F "
                             "FF3F3F3F FF7F7F7F FF7F7F7F FF808080"
                             "FF000000 FF000000 FF000000 FF404040 FF404040 "
                             "FF3F3F3F FF7F7F7F FF7F7F7F FF7F7F7F"
                             "FF000000 FF000000 FF000000 FF404040 FF3F3F3F "
                             "FF3F3F3F FF808080 FF808080 FF7F7F7F"
                             "FF202020 FF202020 FF202020 FF606060 FF606060 "
                             "FF5F5F5F FFA0A0A0 FF9F9F9F FF9F9F9F"
                             "FF1F1F1F FF202020 FF1F1F1F FF606060 FF5F5F5F "
                             "FF606060 FFA0A0A0 FFA0A0A0 FFA0A0A0"
                             "FF202020 FF202020 FF1F1F1F FF5F5F5F FF5F5F5F "
                             "FF606060 FF9F9F9F FF9F9F9F FF9F9F9F"
                             "FF2F2F2F FF303030 FF2F2F2F FF707070 FF707070 "
                             "FF6F6F6F FFF0F0F0 FFEFEFEF FFF0F0F0"
                             "FF2F2F2F FF2F2F2F FF2F2F2F FF6F6F6F FF707070 "
                             "FF707070 FFEFEFEF FFEFEFEF FFEFEFEF"
                             "FF2F2F2F FF2F2F2F FF2F2F2F FF6F6F6F FF707070 "
                             "FF6F6F6F FFF0F0F0 FFF0F0F0 FFF0F0F0"));
}

TEST(Jpeg, ColorMemoryResource) {
  roo_io::MemoryResource<const roo::byte*> resource(
      reinterpret_cast<const roo::byte*>(kColorBlocks9x9Jpg),
      reinterpret_cast<const roo::byte*>(kColorBlocks9x9Jpg) +
          sizeof(kColorBlocks9x9Jpg));
  JpegDecoder decoder;
  JpegImage image(decoder, resource);

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

  EXPECT_THAT(test_screen,
              MatchesContent(Argb8888(), 9, 9,
                             "FFFD0100 FFFB0000 FFFB0100 FF01FF02 FF00FF00 "
                             "FF00FF00 FF0001FB FF0001FD FF0001FD"
                             "FFFC0000 FFFB0100 FFFD0100 FF00FF02 FF00FE00 "
                             "FF00FE00 FF0001FC FF0001FB FF0001FB"
                             "FFFC0000 FFFC0200 FFFB0100 FF01FF00 FF01FF00 "
                             "FF00FF00 FF0002FD FF0001FD FF0000FB"
                             "FFFEFE00 FFFFFF00 FFFFFF00 FF00FFFD FF00FEFC "
                             "FF00FFFE FFFE00FD FFFE01FC FFFC00FB"
                             "FFFDFE00 FFFDFF00 FFFDFF00 FF00FFFF FF00FFFF "
                             "FF00FFFC FFFC00FC FFFD01FD FFFD01FC"
                             "FFFDFE00 FFFEFF00 FFFEFE00 FF00FFFC FF00FFFF "
                             "FF00FFFC FFFE00FD FFFD01FD FFFC00FC"
                             "FFFC8000 FFFE8001 FFFD8100 FF7C00FC FF7F00FE "
                             "FF7D01FD FF01807F FF008080 FF008080"
                             "FFFE8000 FFFE8000 FFFD8100 FF7C00FD FF7C00FC "
                             "FF7C00FD FF00817F FF018080 FF007F7F"
                             "FFFC8000 FFFD7F00 FFFE8001 FF7C00FD FF7D01FE "
                             "FF7E00FD FF007F7F FF01807F FF018080"));
}

}  // namespace roo_display