/*
The GPLv3 License (GPLv3)

Copyright (c) 2022 Akos Horvath

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "graphics.h"
#include "utils.h"
#include <SDL2/SDL_render.h>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace Mandelbrot;
using namespace mpfr;

Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title),
    from(Vec2mp(0.0, 0.0)), to(Vec2mp(0.0, 0.0)), m_start(Vec2i(0, 0)),
    m_end(Vec2i(0, 0)), mouseDown(false)
{

    SDL_Window *window = SDL_CreateWindow(this->title.c_str(), 
                                          SDL_WINDOWPOS_CENTERED,
                                          SDL_WINDOWPOS_CENTERED,
                                          this->w, this->h, 0);

    if (!window)
    {
        std::cout << "Could not create window. "
                  << SDL_GetError() << std::endl;
        exit(EXIT_FAILURE);
    }

    this->window = window;
   
    SDL_Renderer *r = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    if (!r)
    {
        std::cout << "Could not create renderer. "
                  << SDL_GetError() << std::endl;
        exit(EXIT_FAILURE);
    }
    
    this->renderer = r;
    this->running = true;
}

void Graphics::drawPoint(const Vec2i &pos, const rgb &col)
{
    SDL_RenderDrawPoint(this->renderer, pos.x, pos.y);
}

rgb hsv2rgb(hsv in)
{
    double      hh, p, q, t, ff;
    long        i;
    rgb         out;

    if(in.s <= 0.0) {       // < is bogus, just shuts up warnings
        out.r = in.v;
        out.g = in.v;
        out.b = in.v;
        return out;
    }
    hh = in.h;
    if(hh >= 360.0) hh = 0.0;
    hh /= 60.0;
    i = (long)hh;
    ff = hh - i;
    p = in.v * (1.0 - in.s);
    q = in.v * (1.0 - (in.s * ff));
    t = in.v * (1.0 - (in.s * (1.0 - ff)));

    switch(i) {
    case 0:
        out.r = in.v;
        out.g = t;
        out.b = p;
        break;
    case 1:
        out.r = q;
        out.g = in.v;
        out.b = p;
        break;
    case 2:
        out.r = p;
        out.g = in.v;
        out.b = t;
        break;

    case 3:
        out.r = p;
        out.g = q;
        out.b = in.v;
        break;
    case 4:
        out.r = t;
        out.g = p;
        out.b = in.v;
        break;
    case 5:
    default:
        out.r = in.v;
        out.g = p;
        out.b = q;
        break;
    }

    //std::cout << "ret" << out.g << out.b << out.b << std::endl;
    return out;     
}
void Graphics::plot(Mandelbrotc const &m)
{
    // std::vector<uint8_t> screen = std::vector<uint8_t>();
    // std::vector<uint8_t> screen1 = std::vector<uint8_t>();
    // #pragma omp parallel sections
    // {
    //     #pragma omp section
    //     {
    //         for (int i = 0; i < this->w/2; i+=2) {
    //             for (int j = 0; j < this->h/2; j+=2) {
    //                 mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x;
    //                 mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y;
    //     //             std::cout << "px: " << x.to_str() << std::endl;
    //     //          std::cout << "py: " << y.to_str() << std::endl;
    //                 uint32_t max_iter = 50;
    //                 uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter);
    //                 std::cout << "iter: " << iter << std::endl;

    //                 uint8_t color = 255 - (int)(iter*255/max_iter);
    //                 screen.push_back(color);
    //         SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
    //         SDL_RenderDrawPoint(renderer, (int)i, (int)j);
    //             }
    //         }
    //     }
    //     #pragma omp section
    //     {
    //         for (int i = 1; i < this->w/2; i+=2) {
    //             for (int j = 1; j < this->h/2; j+=2) {
    //                 mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x;
    //                 mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y;
    //     //             std::cout << "px: " << x.to_str() << std::endl;
    //     //          std::cout << "py: " << y.to_str() << std::endl;
    //                 uint32_t max_iter = 50;
    //                 uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter);
    //                 std::cout << "iter: " << iter << std::endl;

    //                 uint8_t color = 255 - (int)(iter*255/max_iter);
    //                 screen1.push_back(color);
    //         SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
    //         SDL_RenderDrawPoint(renderer, (int)i, (int)j);
    //             }
    //         }
    //     }
    // }
    // for (double i = 1.0; i < this->w/2; i+=2) {
    //     for (double j = 1.0; j < this->h/2; j+=2) {
    //         uint8_t color = screen[i*h/2+j];
    //         SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
    //         SDL_RenderDrawPoint(renderer, (int)i, (int)j);
    //     }
    // }
    // for (double i = 0.0; i < this->w; i++) {
    //     for (double j = 0.0; j < this->h; j++) {
    //         mpreal x = (mpfr::abs(from.x - to.x) * (i / this->w)) + from.x;
    //         mpreal y = (mpfr::abs(from.y - to.y) * (j / this->h)) + from.y;
    //         uint32_t max_iter = 50;
    //         uint32_t iter = mandelbrot(Vec2mp(x, y), max_iter);
    //         //std::cout << "iter: " << iter << std::endl;

    //         uint8_t color = 255 - (iter*255/max_iter);
    //         SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
    //         SDL_RenderDrawPoint(renderer, (int)i, (int)j);
    //     }
    // }

    for (uint32_t i = 0; i < m.s.x; i++) {
        for (uint32_t j = 0; j < m.s.y; j++) {
            //uint8_t color = 255 - (m.screen[i][j]*255/m.max_iter);
            uint8_t iter = m.screen[i][j];
            hsv h;
            h.h = 255*(double)iter/(double)m.max_iter;
            h.s = 255;
            if (iter < m. max_iter)
                h.v = 255;
            else
                h.v = 0;
            rgb r;
            r = hsv2rgb(h);
            //std::cout << "rgb" << r.r << r.g << r.b << std::endl;
            SDL_SetRenderDrawColor(this->renderer, r.r, r.g, r.b, 255);
            SDL_RenderDrawPoint(renderer, (int)i, (int)j);
        }
    }
}


void Graphics::drawSelectionRectangle()
{
    if (this->mouseDown) {
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        if (m_start.x < m_end.x) {
            for (int i = m_start.x; i < m_end.x; i++) {
                SDL_RenderDrawPoint(renderer, i, m_start.y);
                SDL_RenderDrawPoint(renderer, i, m_end.y);
            }
        } else {
            for (int i = m_end.x; i < m_start.x; i++) {
                SDL_RenderDrawPoint(renderer, i, m_start.y);
                SDL_RenderDrawPoint(renderer, i, m_end.y);
            }
        }
        if (m_start.y < m_end.y) {
            for (int i = m_start.y; i < m_end.y; i++) {
                SDL_RenderDrawPoint(renderer, m_start.x, i);
                SDL_RenderDrawPoint(renderer, m_end.x, i);
            }
        } else {
            for (int i = m_end.y; i < m_start.y; i++) {
                SDL_RenderDrawPoint(renderer, m_start.x, i);
                SDL_RenderDrawPoint(renderer, m_end.x, i);
            }
        }
    }
}

void Graphics::zoom(Mandelbrotc &m)
{
    if (abs(m_end.x-m_start.x) < 10 || abs(m_end.y-m_start.y) < 10)
        return;

    mpreal s_x = (mpfr::abs(m.f.x - m.t.x) *
                 ((double)this->m_start.x / (double)this->w)) + m.f.x;
    mpreal s_y = (mpfr::abs(m.f.y - m.t.y) *
                 ((double)this->m_start.y / (double)this->h)) + m.f.y;

    mpreal e_x = (mpfr::abs(m.f.x - m.t.x) *
                 ((double)this->m_end.x / (double)this->w)) + m.f.x;
    mpreal e_y = (mpfr::abs(m.f.y - m.t.y) *
                 ((double)this->m_end.y / (double)this->h)) + m.f.y;

    m.f = Vec2mp(s_x, s_y);
    m.t = Vec2mp(e_x, e_y);
}

void Graphics::mainLoop()
{
    mpreal fx = mpreal(-2);
    mpreal fy = mpreal(-1);
    mpreal tx = mpreal(1);
    mpreal ty = mpreal(1);
    this->from = Vec2mp(fx, fy);
    this->to = Vec2mp(tx, ty);

    Vec2i m_start = Vec2i(0, 0);
    Vec2i m_end = Vec2i(0, 0);

    Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), 100);
    m.thread_count = 16;
    m.start_threads(true);

    while (this->running)
    {

        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    running = false;
                    break;
                case SDL_MOUSEWHEEL:
                    if (event.wheel.y > 0) {
                        std::cout << "wheel" << std::endl;
                        this->from.x += 0.2;
                        this->to.x -= 0.2;
                        this->from.y += 0.2;
                        this->to.y -= 0.2;
                    }
                    if (event.wheel.y < 0) {
                        std::cout << "wheel" << std::endl;
                        this->from.x -= 0.2;
                        this->to.x += 0.2;
                        this->from.y -= 0.2;
                        this->to.y += 0.2;
                    }
                    break;
                case SDL_MOUSEBUTTONDOWN:
                    if (this->mouseDown)
                        break;
                    this->mouseDown = true;
                    this->m_start.x = event.button.x;
                    this->m_start.y = event.button.y;
                    break;
                case SDL_MOUSEBUTTONUP:
                    this->mouseDown = false;
                    this->zoom(m);
                    break;
                case SDL_MOUSEMOTION:
                    this->m_end.x = event.motion.x;
                    this->m_end.y = event.motion.y;
                    break;
            }
        }
 
        // std::cout << "mainloop range " << this->from.x << std::endl;
        // std::cout << "mainloop range " << this->from.y << std::endl;
        // std::cout << "mainloop range " << this->to.x << std::endl;
        // std::cout << "mainloop range " << this->to.y << std::endl;

        m.start_threads(false);
        this->plot(m);
        m.stop_threads();

        this->drawSelectionRectangle();
        
        SDL_RenderPresent(renderer);
        SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
        SDL_RenderClear(renderer);
    
        //SDL_Delay(10);
    }
    m.stop_threads();
}