#include <stdlib.h>
#include <string.h>
#include "matrix.h"

// Matrix erstellen
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
    Matrix matrix = {NULL, 0, 0};
    if (rows == 0 || cols == 0) {
        return matrix; // Keine Speicherallokation
    }
    matrix.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
    matrix.rows = rows;
    matrix.cols = cols;
    return matrix;
}

// Speicher freigeben
void clearMatrix(Matrix *matrix)
{
    if (matrix->buffer != NULL) {
        free(matrix->buffer);
        matrix->buffer = NULL;
    }
    matrix->rows = 0;
    matrix->cols = 0;
}

// Wert setzen
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    if (rowIdx < matrix.rows && colIdx < matrix.cols && matrix.buffer != NULL) {
        matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
    }
}

// Wert auslesen
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    if (rowIdx < matrix.rows && colIdx < matrix.cols && matrix.buffer != NULL) {
        return matrix.buffer[rowIdx * matrix.cols + colIdx];
    }
    return UNDEFINED_MATRIX_VALUE; // Fehlerbehandlung
}

// Matrizen addieren (mit Broadcasting)
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
    if (matrix1.buffer == NULL || matrix2.buffer == NULL) {
        return (Matrix){NULL, 0, 0};
    }

    // Prüfen auf Broadcasting-Kompatibilität
    if (!((matrix1.rows == matrix2.rows) || (matrix1.rows == 1) || (matrix2.rows == 1))) {
        return (Matrix){NULL, 0, 0};
    }
    if (!((matrix1.cols == matrix2.cols) || (matrix1.cols == 1) || (matrix2.cols == 1))) {
        return (Matrix){NULL, 0, 0};
    }

    unsigned int resultRows = (matrix1.rows > matrix2.rows) ? matrix1.rows : matrix2.rows;
    unsigned int resultCols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;

    Matrix result = createMatrix(resultRows, resultCols);

    for (unsigned int i = 0; i < resultRows; i++) {
        for (unsigned int j = 0; j < resultCols; j++) {
            MatrixType val1 = matrix1.buffer[(matrix1.rows == 1 ? 0 : i) * matrix1.cols +
                                             (matrix1.cols == 1 ? 0 : j)];
            MatrixType val2 = matrix2.buffer[(matrix2.rows == 1 ? 0 : i) * matrix2.cols +
                                             (matrix2.cols == 1 ? 0 : j)];
            result.buffer[i * resultCols + j] = val1 + val2;
        }
    }

    return result;
}

// Matrizen multiplizieren
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
    if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL) {
        return (Matrix){NULL, 0, 0};
    }

    Matrix result = createMatrix(matrix1.rows, matrix2.cols);
    for (unsigned int i = 0; i < matrix1.rows; i++) {
        for (unsigned int j = 0; j < matrix2.cols; j++) {
            MatrixType sum = 0;
            for (unsigned int k = 0; k < matrix1.cols; k++) {
                sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
            }
            result.buffer[i * result.cols + j] = sum;
        }
    }
    return result;
}