//
// Created by ary on 19.07.2026.
//

#include "examples.h"
#include "structs.h"
#include "vectorOp.h"
#include "raytrace.h"
#include <stdlib.h>
#include "interface.h"
#include "helper.h"

void example_color(int width, int height, int channels, unsigned char **image) {
    for (int i = 0; i < height * width; i++) {
        int step = i *channels;
        int x = i % width;
        int y = i / width;
        (*image)[step] = 0 + (int)((((double)x)/width) * 255);
        (*image)[step+1] = 0 + (int)((((double)y)/width) * 255);
        (*image)[step+2] = 0;
        if (channels == 4) {
            (*image)[step+3] = 255;
        }
    }
}

void exampleSphere( int width, int height, int channels, unsigned char **image) {
    camera cam1;
    cam1.origin.x = 0;
    cam1.origin.y = 0;
    cam1.origin.z = 0;
    cam1.direction.x = 0;
    cam1.direction.y = 0;
    cam1.direction.z = 1;
    cam1.resolutionX = width;
    cam1.resolutionY = height;
    cam1.screenDistance = 1;
    cam1.width = 1.920;
    cam1.height = 1.080;
    vec3 sphereOrigin = (vec3){0,-1.5,5};
    double sphereRadius = 1.0;
    int objectCount = 7;
    object objectList[objectCount];
    sphere obj1 = (sphere){sphereOrigin, sphereRadius,(RGBA){1,0.5,1,1}};
    objectList[0] = (object){0, &obj1, 0};
    sphere obj2 = (sphere){vec3Add(sphereOrigin, (vec3){3,0,0}), sphereRadius,  (RGBA){1,1,0.5,1}};
    objectList[1] = (object){0, &obj2, 0};
    sphere obj3 = (sphere){vec3Add(sphereOrigin, (vec3){-2,1,0}), sphereRadius, (RGBA){1,1,1,1}};
    objectList[2] = (object){0, &obj3, 0};
    sphere obj4 = (sphere){vec3Add(sphereOrigin, (vec3){1,1,3}), sphereRadius, (RGBA){0.5,1,1,1}};
    objectList[3] = (object){0, &obj4, 0};
    sphere light1 = (sphere){(vec3){1,1,4.5}, sphereRadius, (RGBA){1,1,1,1}};
    objectList[4] = (object){0, &light1, 1};
    sphere obj5 = (sphere){(vec3){0, -6, 6.5}, 5, (RGBA){1,0.2,0.6,1}};
    objectList[5] = (object){0, &obj5, 0};
    sphere light2 = (sphere){(vec3){1,-2,4}, sphereRadius, (RGBA){1,1,1,1}};
    objectList[6] = (object){0, &light2, 1};



    displaySzene(objectList, objectCount, cam1, width, height, channels, image);

}

void exampleCornell( int width, int height, int channels, unsigned char **image) {
    camera cam1;
    cam1.origin.x = 0;
    cam1.origin.y = 0;
    cam1.origin.z = 0;
    cam1.direction.x = 0;
    cam1.direction.y = 0;
    cam1.direction.z = 1;
    cam1.resolutionX = width;
    cam1.resolutionY = height;
    cam1.screenDistance = 0.6;
    cam1.width = 1.080;
    cam1.height = 1.080;
    int objectCount = 19;
    object objectList[objectCount];

    cube makroObj1 = createCuboid((vec3){1,-2,2.2}, (vec3){0.75,0,0.75}, (vec3){0,1.2,0}, (vec3){-0.75,0,0.75},(RGBA){1,1,0.5,1});
    objectList[0] = (object){1, &(makroObj1.quad1), 0};
    objectList[1] = (object){1, &(makroObj1.quad2), 0};
    objectList[2] = (object){1, &(makroObj1.quad3), 0};
    objectList[3] = (object){1, &(makroObj1.quad4), 0};
    objectList[4] = (object){1, &(makroObj1.quad5), 0};
    objectList[5] = (object){1, &(makroObj1.quad6), 0};

    quad light1 = (quad){(vec3){-2,1.9,0.5}, (vec3){0,0,4}, (vec3){4,0,0}, (vec3){0,-1,0}, (RGBA){1,1,1,1}};
    objectList[6] = (object){1, &(light1), 1};

    cube makroObj2 = createCuboid((vec3){-2,-2,-0.1}, (vec3){4,0,0}, (vec3){0,4,0}, (vec3){0,0,4},(RGBA){1,1,1,1});
    makroObj2.quad1.normal = vec3Scale(makroObj2.quad1.normal, -1);
    makroObj2.quad2.normal = vec3Scale(makroObj2.quad2.normal, -1);
    makroObj2.quad3.normal = vec3Scale(makroObj2.quad3.normal, -1);
    makroObj2.quad4.normal = vec3Scale(makroObj2.quad4.normal, -1);
    makroObj2.quad5.normal = vec3Scale(makroObj2.quad5.normal, -1);
    makroObj2.quad6.normal = vec3Scale(makroObj2.quad6.normal, -1);

    objectList[7] = (object){1, &(makroObj2.quad1), 0};
    objectList[8] = (object){1, &(makroObj2.quad2), 0};
    objectList[9] = (object){1, &(makroObj2.quad3), 0};
    objectList[10] = (object){1, &(makroObj2.quad4), 0};
    objectList[11] = (object){1, &(makroObj2.quad5), 0};
    objectList[12] = (object){1, &(makroObj2.quad6), 0};

    cube makroObj3 = createCuboid((vec3){-1,-2,2.5}, (vec3){0.75,0,0.45}, (vec3){0,2,0}, (vec3){-0.45,0,0.75},(RGBA){1,0.5,1,1});
    objectList[13] = (object){1, &(makroObj3.quad1), 0};
    objectList[14] = (object){1, &(makroObj3.quad2), 0};
    objectList[15] = (object){1, &(makroObj3.quad3), 0};
    objectList[16] = (object){1, &(makroObj3.quad4), 0};
    objectList[17] = (object){1, &(makroObj3.quad5), 0};
    objectList[18] = (object){1, &(makroObj3.quad6), 0};



    displaySzene(objectList, objectCount, cam1, width, height, channels, image);

}
