#include <iostream>
#include "bmpimage.h"
#include "psf.h"

const std::string FILENAME = "../elef.bmp";
const std::string FILENAME_OUT = "../elef_out.bmp";
auto font = readPSF("../fonts/ruscii_8x16_2.psf");

//
void lab01() {
    auto og_image = readBMPImage("../elef.bmp");
    auto pixels1 = og_image.pixels_copy();
    auto pixels2 = og_image.pixels_copy();
    for (int i = 0; i < og_image.height(); ++i) {
        for (int j = 0; j < og_image.width(); ++j) {
            pixels1(i, j) = pixels1(i, j) / 2;
            pixels2(i, j) = pixels2(i, j) * 2;
        }
    }
    auto orig_text = textImg(u"Оригинал", &font, 3);
    auto darken_text = textImg(u"Темнее", &font, 3);
    auto lighter_text = textImg(u"Светлее", &font, 3);
    auto invert_text = textImg(u"Инвертирован", &font, 3);
    auto grayscale_text = textImg(u"Оттенки серого", &font, 3);
    auto darkenImg = BMPImage(pixels1).overlay(darken_text, 0, 0);
    auto lighterImg = BMPImage(pixels2).overlay(lighter_text, 0, 0);
    auto invert_img = invertColors(og_image).overlay(invert_text, 0, 0); // TODO
    auto grayscale_img = grayscale(og_image).overlay(grayscale_text, 0, 0);

    og_image.appendRight(lighterImg).appendRight(darkenImg).appendRight(invert_img).appendRight(grayscale_img).overlay(
            orig_text, 0, 0).save("../lab01/elef1.bmp");
}

//
void lab02_01() {
    auto img = readBMPImage("../tea.bmp");
    auto text1024 = textImg(u"1024", &font, 3);
    auto text512 = textImg(u"512", &font, 3);
    auto text256 = textImg(u"256", &font, 3);
    auto text128 = textImg(u"128", &font, 3);
    auto img_512 = downscale2x(img);
    auto img_256 = downscale2x(img_512);
    auto img_128 = downscale2x(img_256);
    img_512.overlay(text512, 0, 0).save("../lab02/tea_512.bmp");
    img_256.overlay(text256, 0, 0).save("../lab02/tea_256.bmp");
    img_128.overlay(text128, 0, 0).save("../lab02/tea_128.bmp");
    img.overlay(text1024, 0, 0).appendRight(img_512).overlay(text512, 1024, 0).appendRight(img_256).
            overlay(text256, 1536, 0).appendRight(img_128).overlay(text128, 1792, 0).save(
            "../lab02/tea_downscale.bmp");;
    auto img_from_512 = upscale2x(img_512).overlay(text512, 0, 0);
////    auto img_from_256 = upscale2x(upscale2x(img_256));
////    auto img_from_128 = upscale2x(upscale2x(upscale2x(img_128)));
    auto tmp = upscale2x(img_256);
    auto img_from_256 = upscale2x(tmp).overlay(text256, 0, 0);
    auto tmp2 = upscale2x(img_128);
    auto tmp3 = upscale2x(tmp2);
    auto img_from_128 = upscale2x(tmp3).overlay(text128, 0, 0);
    img_from_512.save("../lab02/tea_from_512.bmp");
    img_from_256.save("../lab02/tea_from_256.bmp");
    img_from_128.save("../lab02/tea_from_128.bmp");
    img.overlay(text1024, 0, 0).appendRight(img_from_512).appendRight(img_from_256).appendRight(img_from_128).save(
            "../lab02/tea_upscale.bmp");
//    newImg2->save("../lab02/tea_upscale.bmp");
}

//
void lab02_02() {
    auto img = readBMPImage("../tea.bmp");
    auto ogText = textImg(u"Оригинал", &font, 3);
    auto text256 = textImg(u"256 оттенков", &font, 3);
    auto text128 = textImg(u"128 оттенков", &font, 3);
    auto text64 = textImg(u"64 оттенка", &font, 3);
    auto text32 = textImg(u"32 оттенка", &font, 3);
    auto gsImg = grayscale(img);
    gsImg.overlay(text256, 0, 0).save("../lab02/tea_grayscale_256.bmp");
    auto gsPixels = gsImg.pixels_copy();
    auto width = gsImg.width();
    auto height = gsImg.height();
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            gsPixels(i, j) = (gsPixels(i, j) / 2) * 2;
        }
    }
    auto gsImg128 = BMPImage(gsPixels).overlay(text128, 0, 0);
    gsPixels = gsImg.pixels_copy();
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            gsPixels(i, j) = (gsPixels(i, j) / 4) * 4;
        }
    }
    auto gsImg64 = BMPImage(gsPixels).overlay(text64, 0, 0);
    gsPixels = gsImg.pixels_copy();
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            gsPixels(i, j) = (gsPixels(i, j) / 8) * 8;
        }
    }
    auto gsImg32 = BMPImage(gsPixels).overlay(text32, 0, 0);
    gsImg128.save("../lab02/tea_grayscale_128.bmp");
    gsImg64.save("../lab02/tea_grayscale_64.bmp");
    gsImg32.save("../lab02/tea_grayscale_32.bmp");
    img.overlay(ogText, 0, 0).appendRight(gsImg).overlay(text256, 1024, 0).appendRight(gsImg128).appendRight(
            gsImg64).appendRight(gsImg32).save("../lab02/tea_grayscale.bmp");
}

//
void lab02_03() {
    auto img = readBMPImage("../tea.bmp");
    auto text = textImg(u"Оригинал", &font, 3);
    auto text_avrg = textImg(u"Среднее 1.5x", &font, 5);
    auto text_avrg2 = textImg(u"Среднее 2x", &font, 6);
    auto text_avrg3 = textImg(u"Среднее 3x", &font, 9);
    auto text_lin = textImg(u"Линейное 1.5x", &font, 5);
    auto text_lin2 = textImg(u"Линейное 2x", &font, 6);
    auto text_lin3 = textImg(u"Линейное 3x", &font, 9);
    auto img_1_5 = upscale1_5x(img);
    auto img_1_5_ver2 = upscale1_5x_ver2(img);
    auto img_2 = upscale2x(img);
    auto img_2_ver2 = upscale2x_ver2(img);
    auto img_3 = upscale2x(img_1_5);
    auto img_3_ver2 = upscale2x_ver2(img_1_5_ver2);
    img_1_5.overlay(text_avrg, 0, 0).appendRight(img_1_5_ver2).overlay(text_lin, 1536, 0).save(
            "../lab02/lab02_03/tea_x1_5.bmp");
    img_2.overlay(text_avrg2, 0, 0).appendRight(img_2_ver2).overlay(text_lin2, 2048, 0).save(
            "../lab02/lab02_03/tea_x2.bmp");
    img_3.overlay(text_avrg3, 0, 0).appendRight(img_3_ver2).overlay(text_lin3, 3072, 0).save(
            "../lab02/lab02_03/tea_x3.bmp");
    img.overlay(text, 0, 0).appendRight(img_1_5).overlay(text_avrg, 1024, 0).appendRight(img_2)
            .overlay(text_avrg2, 2560, 0).appendRight(img_3).overlay(text_avrg3, 4608, 0).save(
                    "../lab02/lab02_03/tea_upscale.bmp");
    img.overlay(text, 0, 0).appendRight(img_1_5_ver2).overlay(text_lin, 1024, 0).appendRight(img_2_ver2)
            .overlay(text_lin, 2560, 0).appendRight(img_3_ver2).overlay(text_lin, 4608, 0).save(
                    "../lab02/lab02_03/tea_upscale_ver2.bmp");
}

void lab03() {
    auto img = readBMPImage("../tea.bmp");
    auto imgGS = grayscale(img);
    int avrg_mask[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
    int previtt_mask1[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
    int previtt_mask2[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
    int sobel_mask1[9] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
    int sobel_mask2[9] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
    auto avrg_text = textImg(u"Осреднение", &font, 3);
    auto previtt_text = textImg(u"Фильтр Превитта", &font, 3);
    auto sobel_text = textImg(u"Фильтр Собеля", &font, 3);
    auto text = textImg(u"Оригинал", &font, 3);
    auto avrg_image = filter(imgGS, avrg_mask).overlay(avrg_text, 0, 0);
    auto previtt_image1 = filter(imgGS, previtt_mask1, 1);//.overlay(previtt_text, 0, 0);
    auto previtt_image2 = filter(imgGS, previtt_mask2, 1);//.overlay(previtt_text, 0, 0);
    previtt_image1.save("../lab03/previtt1.bmp");
    previtt_image2.save("../lab03/previtt2.bmp");
    PixelArray previttPixels(previtt_image1.width(), previtt_image1.height());
    for (int y = 0; y < previtt_image1.height(); ++y) {
        for (int x = 0; x < previtt_image1.width(); ++x) {
            previttPixels(y, x) = previtt_image1.pixels()(y, x) + previtt_image2.pixels()(y, x);
        }
    }
    auto previtt_image = BMPImage(previttPixels).overlay(previtt_text, 0, 0);
    auto sobel_image1 = filter(imgGS, sobel_mask1, 1).overlay(sobel_text, 0, 0);
    auto sobel_image2 = filter(imgGS, sobel_mask2, 1).overlay(sobel_text, 0, 0);
    PixelArray sobelPixels(sobel_image1.width(), sobel_image2.height());
    for (int y = 0; y < sobel_image1.height(); ++y) {
        for (int x = 0; x < sobel_image1.width(); ++x) {
            previttPixels(y, x) = sobel_image1.pixels()(y, x) + sobel_image2.pixels()(y, x);
        }
    }
    auto sobel_image = BMPImage(previttPixels).overlay(sobel_text, 0, 0);
    imgGS.overlay(text, 0, 0).appendRight(avrg_image).save("../lab03/avrg_filter.bmp");
    imgGS.overlay(text, 0, 0).appendRight(previtt_image).save("../lab03/previtt_filter.bmp");
    imgGS.overlay(text, 0, 0).appendRight(sobel_image).save("../lab03/sobel_filter.bmp");
}

void test() {
//    Pixel **test;
    PixelArray test(3, 3);
//    test = new Pixel *[3];
//    test[0] = new Pixel[3];
//    test[1] = new Pixel[3];
//    test[2] = new Pixel[3];
    test(0, 0) = Pixel{0, 0, 0};
    test(0, 1) = Pixel{127, 127, 127};
    test(0, 2) = Pixel{255, 255, 255};
    test(1, 0) = Pixel{127, 127, 127};
    test(1, 1) = Pixel{63, 63, 63};
    test(1, 2) = Pixel{127, 127, 127};
    test(2, 0) = Pixel{255, 255, 255};
    test(2, 1) = Pixel{127, 127, 127};
    test(2, 2) = Pixel{0, 0, 0};
    auto test_img = BMPImage(test);
    test_img.save("../test.bmp");
    auto test_scale = upscale1_5x_ver2(test_img);
    test_scale.save("../test_scale.bmp");
}

void test2() {
    auto background_color = Pixel{0, 0, 0};
    auto font_color = Pixel{255, 255, 255};
    auto glyph = font._glyphs[u'б'];
    auto w = glyph.width;
    auto h = glyph.height;

    uint32_t imgWidth = 16 * glyph.width;
    uint32_t imgHeight = 16 * glyph.height;
    PixelArray test(imgWidth, imgHeight);
    std::u16string str = u"Hello, World! Привет, Мир!";
    uint32_t k = 0;
    for (auto it: font._glyphs) {
        if (k == 256) continue;
//        glyph = font._glyphs[u'☺'];
//        glyph = font._glyphs[str[k % str.size()]];
        glyph = it.second;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (glyph.glyph[i][j])
                    test((k / 16) * h + i, (k % 16) * w + j) = font_color;
                else
                    test((k / 16) * h + i, (k % 16) * w + j) = background_color;
            }
        }
        k++;
    }
    BMPImage(test).save("test_font.bmp");
}

void test3() {
    textImg(u"Привет, Мир!", &font).save("../font_test.bmp");
}

int main() {
    //lab01();
//    lab02_01();
//    lab02_02();
//    lab02_03();
    lab03();
//    test();
//    test2();
//    test3();
//    auto t = readBMPImage("../lab02/lab02_03/tea_x3.bmp");
    return 0;
}
