//
// Created by bruno on 17.2.2025.
//

#include "switches.h"
#include <stdlib.h>
#include <stdio.h>

void generate_switch_rects(Switches *switches) {
    int switch_width = switches->rect->w / 4;
    int switch_height = switches->rect->h / 4;

    for (int row = 0; row < 4; row++) {
        for (int col = 0; col < 4; col++) {
            int idx = row * 4 + col;

            // Base rect inside switches->rect
            switches->rects[idx] = (SDL_Rect) {
                    col * switch_width + 2,
                    row * switch_height + 2,
                    switch_width - 2,
                    switch_height - 2
            };

            // Rect relative to outRect
            switches->outRects[idx] = (SDL_Rect) {
                    switches->outRect->x + col * switch_width + 2,
                    switches->outRect->y + row * switch_height + 2,
                    switch_width - 2,
                    switch_height - 2
            };
        }
    }
}

void render_switch_matrix(SDL_Renderer *renderer, Switches *switches) {
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);

    for (int i = 0; i < 16; i++) {
        if (switches->value & (1 << i)) {
            SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
        } else {
            // Render black for "off" switches
            SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
        }
        SDL_RenderFillRect(renderer, &switches->rects[i]);
    }
}

void toggle_switch(SDL_Rect rect, Switches *switches) {
    for (int i = 0; i < 16; i++) {
        if (SDL_HasIntersection(&rect, &switches->outRects[i])) {
            switches->value ^= (1 << i);
        }
    }
}

void update_switches_texture(SDL_Renderer *renderer, Switches *display) {
    SDL_SetRenderTarget(renderer, display->texture);

    render_switch_matrix(renderer, display); // Render the 4x4 switch matrix

    SDL_SetRenderTarget(renderer, NULL);
}


void init_switches(Switches *switches, SDL_Renderer *renderer, int x, int y, int width, int height) {
    switches->rect = malloc(sizeof(SDL_Rect));
    switches->outRect = malloc(sizeof(SDL_Rect));
    switches->value = 0;

    switches->rect->x = 0;
    switches->rect->y = 0;
    switches->rect->w = width;
    switches->rect->h = height;

    switches->outRect->x = x;
    switches->outRect->y = y;
    switches->outRect->w = width;
    switches->outRect->h = height;

    switches->texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height);
    if (!switches->texture) {
        fprintf(stderr, "Failed to create texture: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    switches->value = (1 << 0) | (1 << 1);
    generate_switch_rects(switches);
    update_switches_texture(renderer, switches);
}

void render_switches_segment(Switches *display, SDL_Renderer *renderer) {
    if (display->oldValue != display->value) {
        display->oldValue = display->value;
        update_switches_texture(renderer, display);
    }
    if (display->texture) {
        SDL_RenderCopy(renderer, display->texture, NULL, display->outRect);
    }
}

void destroy_switches_segment(Switches *switches) {
    SDL_DestroyTexture(switches->texture);
    free(switches->rect);
    free(switches->outRect);
}