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

// TODO Matrix-Funktionen implementieren

Matrix createMatrix(unsigned int rows, unsigned int cols)
{
    Matrix m;
    // Default: leere Matrix bei Fehler/Null-Dimensionen
    m.rows = 0;
    m.cols = 0;
    m.buffer = NULL;

    if (rows == 0 || cols == 0) {
        return m;
    }

    size_t count = (size_t)rows * (size_t)cols;
    m.buffer = (MatrixType *)calloc(count, sizeof(MatrixType));
    if (!m.buffer) {
        // Allocation failed -> return empty matrix
        return m;
    }

    // Nur bei erfolgreicher Allokation Dimensionen setzen
    m.rows = rows;
    m.cols = cols;
    return m;
}

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

void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    if (matrix.buffer == NULL) return;
    if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return;
    size_t idx = (size_t)rowIdx * matrix.cols + colIdx;
    matrix.buffer[idx] = value;
}

MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    if (matrix.buffer == NULL) return (MatrixType)0;
    if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return (MatrixType)0;
    size_t idx = (size_t)rowIdx * matrix.cols + colIdx;
    return matrix.buffer[idx];
}

Matrix add(const Matrix matrix1, const Matrix matrix2)
{
    Matrix result;
    result.rows = result.cols = 0;
    result.buffer = NULL;

    if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) return result;

    result = createMatrix(matrix1.rows, matrix1.cols);
    if (!result.buffer) return result;

    size_t count = (size_t)matrix1.rows * matrix1.cols;
    for (size_t i = 0; i < count; ++i) {
        result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
    }
    return result;
}

Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
    Matrix result;
    result.rows = result.cols = 0;
    result.buffer = NULL;

    if (matrix1.cols != matrix2.rows) return result;
    if (matrix1.rows == 0 || matrix1.cols == 0 || matrix2.cols == 0) return result;

    result = createMatrix(matrix1.rows, matrix2.cols);
    if (!result.buffer) return result;

    for (unsigned int i = 0; i < matrix1.rows; ++i) {
        for (unsigned int j = 0; j < matrix2.cols; ++j) {
            MatrixType sum = (MatrixType)0;
            for (unsigned int k = 0; k < matrix1.cols; ++k) {
                sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
            }
            setMatrixAt(sum, result, i, j);
        }
    }
    return result;
}