//
// Created by m on 12/3/21.
//
#include <cstdio>
#include "sdlrenderer.hpp"

void SdlRenderer::initialize(RendererParams params) {
    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL vide oinit failed: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    auto img_flags = IMG_INIT_PNG;
    if(!(IMG_Init(img_flags) & img_flags)) {
        printf("Failed to init image loading: %s\n", IMG_GetError());
        exit(EXIT_FAILURE);
    }
    if(TTF_Init() < 0) {
        printf("Failed to initialize font loading: %s\n", TTF_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_Window* sdl_window = SDL_CreateWindow(params.title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, params.width, params.height, SDL_WINDOW_SHOWN);
    if(sdl_window == nullptr) {
        printf("error making window: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    else {
        this->window = std::unique_ptr<SDL_Window, SDL_WindowDeleter>(sdl_window);
        SDL_Renderer* sdl_renderer = SDL_CreateRenderer(this->window.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        this->renderer = std::shared_ptr<SDL_Renderer>(sdl_renderer, SDL_DestroyRenderer);

        this->texture_manager = std::make_unique<SdlTextureManager>(this->renderer);
        this->font_manager = std::make_unique<SdlFontManager>();
        this->renderer_params = params;

        SDL_RenderClear(this->renderer.get());
    }
}

void SdlRenderer::flush() {
    SDL_RenderPresent(this->renderer.get());
    SDL_SetRenderDrawColor(this->renderer.get(), COLOR_BLACK.r, COLOR_BLACK.g, COLOR_BLACK.b, COLOR_BLACK.a);
    SDL_RenderClear(this->renderer.get());
}

void SdlRenderer::draw_point(int x, int y, Color color) {
    SDL_SetRenderDrawColor(this->renderer.get(), color.r, color.g, color.b, color.a);
    SDL_RenderDrawPoint(this->renderer.get(), x, y);
}

void SdlRenderer::draw_text(std::optional<TextRenderDetails> details, std::string text, int x, int y) {
    TextRenderDetails unpacked_details = details.value_or(this->renderer_params.default_font);
    std::shared_ptr<TTF_Font> font = this->font_manager->fetch_resource(SdlFontArgs(unpacked_details.size, unpacked_details.font_path));
    std::shared_ptr<SDL_Texture> texture = this->texture_manager->load_text_as_texture(text, font, unpacked_details.color);
    render_texture(x, y, texture, nullptr);
}

SdlRenderer::~SdlRenderer() {
    // exit subsystems
    IMG_Quit();
    TTF_Quit();
    SDL_Quit();
}

void SdlRenderer::render_texture(int x, int y, const std::shared_ptr<SDL_Texture>& texture, SDL_Rect *src) {
    int w, h;
    if(src == nullptr) {
        SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h);
    }
    else {
        w = src->w;
        h = src->h;
    }
    SDL_Rect rect = {x,y,w,h};
    SDL_RenderCopy(this->renderer.get(), texture.get(), src, &rect);
}

void SdlRenderer::draw_sprite(Sprite sprite, int x, int y) {
    SpriteSheet *sprite_sheet = sprite.sprite_sheet;
    TextureManagerFetchArgs args = TextureManagerFetchArgs {sprite_sheet->path, sprite_sheet->color_key};
    std::shared_ptr<SDL_Texture> texture = this->texture_manager->fetch_resource(args);
    int w, h;
    SDL_QueryTexture(texture.get(), nullptr, nullptr, &w, &h);
    int l = (sprite_sheet->sprite_width * sprite.index) / w;
    int src_y = l*sprite_sheet->sprite_height;
    int src_x = (sprite_sheet->sprite_width * sprite.index) % w;
    auto source = SDL_Rect{src_x, src_y, sprite_sheet->sprite_width, sprite_sheet->sprite_height};
    render_texture(x, y, texture, &source);
}
