#include <daguerre.hpp>

namespace daguerre {

  image<hilbert_color> get_hilbert_framebuffer() {
    uint32_t width, height, pitch;
    hilbert_color *ptr = __euler_get_framebuffer(width, height, pitch);
    return image<hilbert_color>(ptr, width, height, pitch, false);
  }

  //TODO: make this more robust
  unsigned read_text_int(std::FILE *input) {
    unsigned n = 0;
    char ch;
    std::fread(&ch, 1, 1, input);
    if (ch == '#') {
      do
        std::fread(&ch, 1, 1, input);
      while (ch != '\n');
      std::fread(&ch, 1, 1, input);
    }
    do {
      n = n * 10 + ch - '0';
      std::fread(&ch, 1, 1, input);
    } while (ch >= '0' && ch <= '9');
    return n;
  }

  //only supports p6 format
  bool try_load_ppm(std::FILE *input, image<rgb24> &into) {

    char header[3];
    if (std::fread(header, 1, 3, input) != 3)
      return false;

    if (header[0] != 'P' || header[1] != '6' || header[2] != '\n')
      return false;

    unsigned width = read_text_int(input);
    unsigned height = read_text_int(input);
    unsigned max = read_text_int(input);

    into = image<rgb24>(width, height);

    for (unsigned y = 0; y < height; ++y)
      for (unsigned x = 0; x < width; ++x) {
        if (std::fread(&into.buffer[y * width + x].r, 1, 1, input) != 1)
          return false;
        if (std::fread(&into.buffer[y * width + x].g, 1, 1, input) != 1)
          return false;
        if (std::fread(&into.buffer[y * width + x].b, 1, 1, input) != 1)
          return false;
      }

    if (max != 255)
      for (unsigned v = 0; v < width * height; ++v) {
        into.buffer[v].r = ((uint16_t)into.buffer[v].r * 255) / max;
        into.buffer[v].g = ((uint16_t)into.buffer[v].g * 255) / max;
        into.buffer[v].b = ((uint16_t)into.buffer[v].b * 255) / max;
      }

    return true;

  }

  //assumes the font is in psf2 format, and has a unicode table
  bool try_load_psf(std::FILE *input, fixed_bitmap_font<bool> &into) {

    uint32_t header[8];
    if (std::fread(header, 4, 8, input) != 8)
      return false;

    const uint32_t glyphs_start = header[2];
    const uint32_t glyph_count = header[4];
    const uint32_t glyph_length = header[5];
    into.height = header[6];
    into.width = header[7];

    const uint32_t unicode_start = glyphs_start + glyph_count * glyph_length;
    std::fseek(input, unicode_start, SEEK_SET);

    uint32_t indices[128];

    for (uint32_t index = 0; index < glyph_count; ++index) {
      uint8_t ch;
      std::fread(&ch, 1, 1, input);
      if (ch < 128)
        indices[ch] = index;
      do
        std::fread(&ch, 1, 1, input);
      while (ch != 0xff);
    }

    for (uint8_t ch = 0; ch < 128; ++ch) {
      std::fseek(input, glyphs_start + glyph_length * indices[ch], SEEK_SET);
      into.glyphs[ch] = image<bool>(into.width, into.height);
      for (unsigned h = 0; h < into.height; ++h)
        for (unsigned wb = 0; wb < into.width; wb += 8) {
          uint8_t byte;
          std::fread(&byte, 1, 1, input);
          for (unsigned x = 0; x < 8 && wb + x < into.width; ++x)
            into.glyphs[ch].set(wb + x, h, (byte >> (7 - x)) & 1);
        }
    }

    return true;

  }

}
