#include "triangle.h"

#include <math.h>

#include "display.h"

// TODO: Create implementation for triangle.h functions
int compare_vec2_t(const void* a, const void* b) {
    const vec2_t* arg1 = (const vec2_t*)a;
    const vec2_t* arg2 = (const vec2_t*)b;
    if (arg1->y < arg2->y) return -1;
    if (arg1->y > arg2->y) return 1;
    return 0;
}

#define FUNC (int)ceilf
#define FUNC2 (int)floorf

uint32_t invert_rgb(uint32_t color) {
    uint32_t a = color & 0xFF000000;          // Alpha channel
    uint32_t rgb = color & 0x00FFFFFF;        // RGB channels
    rgb = ~rgb & 0x00FFFFFF;                  // Invert RGB only
    return a | rgb;
}

void fill_flat_bottom_triangle(const triangle_t* triangle, color_t color) {
    float inv_slope_1 = (triangle->points[1].x - triangle->points[0].x) / (triangle->points[1].y - triangle->points[0].y);
    float inv_slope_2 = (triangle->points[2].x - triangle->points[0].x) / (triangle->points[2].y - triangle->points[0].y);;

    float x_start = triangle->points[0].x, x_end = triangle->points[0].x;

    for (int x = FUNC(triangle->points[0].y); x <= FUNC2(triangle->points[2].y); x++) {
        //draw_rect((int)triangle->points[0].x, i, 1, 1, 0xFFFF0000);
        draw_line(FUNC(x_start), x, FUNC2(x_end), x, color);
        x_start += inv_slope_1;
        x_end += inv_slope_2;
    }
}

void fill_flat_top_triangle(const triangle_t* triangle, color_t color) {
    float inv_slope_1 = (triangle->points[2].x - triangle->points[0].x) / (triangle->points[2].y - triangle->points[0].y);
    float inv_slope_2 = (triangle->points[2].x - triangle->points[1].x) / (triangle->points[2].y - triangle->points[1].y);;

    float x_start = triangle->points[2].x, x_end = triangle->points[2].x;

    for (int y = FUNC2(triangle->points[2].y); y >= FUNC(triangle->points[0].y); y--) {
        //draw_rect((int)triangle->points[0].x, i, 1, 1, 0xFFFF0000);
        draw_line(FUNC(x_start), y, FUNC2(x_end), y, color);
        x_start -= inv_slope_1;
        x_end -= inv_slope_2;
    }
}

void draw_filled_triangle(triangle_t triangle) {
    // sort by y
    int temp = 0, min_idx = 0, mid_idx = 1, max_idx = 2;

    if (triangle.points[min_idx].y > triangle.points[mid_idx].y) {
        temp = min_idx; min_idx = mid_idx; mid_idx = temp;
    }
    if (triangle.points[min_idx].y > triangle.points[max_idx].y) {
        temp = min_idx; min_idx = max_idx; max_idx = temp;
    }
    if (triangle.points[mid_idx].y > triangle.points[max_idx].y) {
        temp = mid_idx; mid_idx = max_idx; max_idx = temp;
    }

    color_t color = triangle.color ? triangle.color : 0xFF00FF00; // Default to 0xFF00FF00 if not setr; // Default color if not set

    if (triangle.points[mid_idx].y == triangle.points[max_idx].y) {
        fill_flat_bottom_triangle(&triangle, color);
    } else if (triangle.points[min_idx].y == triangle.points[mid_idx].y) {
        fill_flat_top_triangle(&triangle, color);
    }


    vec2_t midpoint;
    midpoint.y = triangle.points[mid_idx].y;
    midpoint.x = (((triangle.points[max_idx].x - triangle.points[min_idx].x) * (triangle.points[mid_idx].y - triangle.points[min_idx].y)) / (triangle.points[max_idx].y - triangle.points[min_idx].y)) + triangle.points[min_idx].x;

    triangle_t flat_top, flat_bottom;

    flat_bottom.points[0] = triangle.points[min_idx];
    flat_bottom.points[1] = triangle.points[mid_idx];
    flat_bottom.points[2] = midpoint;

    flat_top.points[0] = triangle.points[mid_idx];
    flat_top.points[1] = midpoint;
    flat_top.points[2] = triangle.points[max_idx];

    fill_flat_bottom_triangle(&flat_bottom, color);
    fill_flat_top_triangle(&flat_top, (color));

}

void draw_textured_triangle(triangle_t triangle, uint32_t* texture) {
    // sort by y
    int temp = 0, min_idx = 0, mid_idx = 1, max_idx = 2;

    if (triangle.points[min_idx].y > triangle.points[mid_idx].y) {
        temp = min_idx; min_idx = mid_idx; mid_idx = temp;
    }
    if (triangle.points[min_idx].y > triangle.points[max_idx].y) {
        temp = min_idx; min_idx = max_idx; max_idx = temp;
    }
    if (triangle.points[mid_idx].y > triangle.points[max_idx].y) {
        temp = mid_idx; mid_idx = max_idx; max_idx = temp;
    }

    float inv_slope_1 = 0;
    float inv_slope_2 = 0;

    if (triangle.points[mid_idx].y - triangle.points[min_idx].y != 0) {
        inv_slope_1 = (float)(triangle.points[mid_idx].x - triangle.points[min_idx].x) / fabsf(triangle.points[mid_idx].y - triangle.points[min_idx].y);
    }

    if (triangle.points[max_idx].y - triangle.points[min_idx].y != 0) {
        inv_slope_2 = (float)(triangle.points[max_idx].x - triangle.points[min_idx].x) / fabsf(triangle.points[max_idx].y - triangle.points[min_idx].y);
    }

    for (int y = triangle.points[min_idx].y; y <= triangle.points[mid_idx].y; y++) {
        //float x_start = triangle
    }


}