#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include "starfield.h"

// Properties
double STAR_Speed = 5.0f;
double STAR_MaxRotationValue = 5.0f;
// End Properties

// Vars
int STARFIELD_BoxWidth, STARFIELD_BoxHeight;
SDL_Texture * STAR_Texture;
SDL_Rect * STAR_SourceRects;
// End Vars

void STARFIELD_Initialize(SDL_Renderer * renderer, int width, int height){
    printf("Initializing starfield...\n");
    STARFIELD_BoxWidth = width;
    STARFIELD_BoxHeight = height;
    STAR_Texture = IMG_LoadTexture(renderer, "assets/images/star.png");
    if (!STAR_Texture)
        printf("Star texture cannot be loaded!\n");
    STAR_SourceRects = malloc(3 * sizeof(SDL_Rect));
    int i;
    for (i = 0; i < 3; i++) {
        STAR_SourceRects[i] = (SDL_Rect) {.x = 0, .y = (100 * i), .w = 100, .h = 100 };
    }
    printf("Starfield initialized!\n");
}

void STARFIELD_Deinitialize(){
    printf("De-initializing Starfield class...\n");
    printf("De-initializing Source Rectangles...\n");
    free(STAR_SourceRects);
    printf("De-initializing Starfield textures...\n");
    SDL_DestroyTexture(STAR_Texture);
    printf("Starfield class de-initialized!\n");
}

Starfield STARFIELD_GetDefault(int count){
    Star * strs = malloc(count * sizeof(Star));
    int i;

    for (i = 0; i < count; i++) {
        strs[i] = STAR_GetDefault();
    }
    return (Starfield) {.Stars = strs, .StarCount = count };
}

void STARFIELD_Draw(Starfield * strf, SDL_Renderer * renderer){
    int i;

    for (i = 0; i < (strf->StarCount); i++) {
        STAR_Draw(&((strf->Stars)[i]), renderer);
    }
}

void STARFIELD_Update(Starfield * strf){
    int i;

    for (i = 0; i < (strf->StarCount); i++) {
        STAR_Update(&((strf->Stars)[i]));
    }
}

void STARFIELD_DestroyObject(Starfield * strf){
    printf("De-initializing Starfield...\n");
    printf("De-initializing Star-array...\n");
    free((strf->Stars));
    printf("Starfield de-initialized!\n");
}

Star STAR_GetDefault(){
    return (Star) {
               .x = (rand() % STARFIELD_BoxWidth) - (STARFIELD_BoxWidth / 2),
               .y = (rand() % STARFIELD_BoxHeight) - (STARFIELD_BoxHeight / 2),
               .z = (rand() % STARFIELD_BoxWidth),
               .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 0, .h = 0 },
               .SourceRect = STAR_SourceRects[(rand() % 3)],
               .Rotation = (double)(rand() % 360),
               .RotationValue = (fmod((double)(rand()), STAR_MaxRotationValue * 2.0f) - STAR_MaxRotationValue),
               .OutOfBounds = false
    };
}
void STAR_Update(Star * star){
    if (!(star->OutOfBounds)) {
        (star->Rotation) += (star->RotationValue);
        (star->z) -= STAR_Speed;        // increase speed (rather acceleration since it looks like a 3D room, but the speed is constant)
        if ((star->z) < 1) {            // respawns a star that has disappeared near the edge of the screen
            *star = STAR_GetDefault();
        }
        double sx = (star->x) / (star->z) * STARFIELD_BoxWidth;// current distance using perspective
        if (sx > STARFIELD_BoxWidth  / 2 || sx < -STARFIELD_BoxWidth / 2)
            (star->OutOfBounds) = true;
        double sy = (star->y) / (star->z) * STARFIELD_BoxHeight; // current distance using perspective
        if (sy * 2 > STARFIELD_BoxHeight || sy < -STARFIELD_BoxHeight / 2)
            (star->OutOfBounds) = true;
        double size = (((double)(STARFIELD_BoxWidth - (star->z))) / ((double)(STARFIELD_BoxWidth)) * 2.4f) + 1.0f;  // radius
        size = 3 * size * size;                                                    // potentially grow particles
        (star->TargetRect) = (SDL_Rect) {.x = (int)round(sx) + (STARFIELD_BoxWidth / 2), .y = (int)round(sy) + (STARFIELD_BoxHeight / 2), .w = size, .h = size };
    } else {
        *star = STAR_GetDefault();
    }
}

void STAR_Draw(Star * star, SDL_Renderer * renderer){
    if (!(star->OutOfBounds)) {
        // printf("Drawing star at: %d | %d with Size %d...\n", (star->TargetRect).x,(star->TargetRect).y,(star->TargetRect).w );
        SDL_RenderCopyEx(renderer, STAR_Texture, &(star->SourceRect), &(star->TargetRect), (star->Rotation), NULL, SDL_FLIP_NONE);
    }
}
