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

// TODO Matrix-Funktionen implementieren

Matrix createMatrix(unsigned int rows, unsigned int cols)
{
    Matrix matrix;


    matrix.rows = rows;
    matrix.cols = cols;


    if (rows == 0 || cols == 0) {
        matrix.data = NULL;
        return matrix;
    }


    matrix.data = (MatrixType*) malloc(rows * cols * sizeof(MatrixType));


    if (matrix.data == NULL) {

        matrix.rows = 0;
        matrix.cols = 0;
        return matrix;
    }


    for (unsigned int i = 0; i < rows * cols; i++) {
        matrix.data[i] = 0;
    }

    return matrix;
    
}

void clearMatrix(Matrix *matrix)
{
    if (matrix->data == NULL) {
        return;
    }


    free(matrix->data);


    matrix->data = NULL;
    matrix->rows = 0;
    matrix->cols = 0;
}

void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{

    if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {

        return;
    }


    unsigned int index = rowIdx * matrix.cols + colIdx;


    matrix.data[index] = value;
}

MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{

    if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {

        return 0;
    }


    unsigned int index = rowIdx * matrix.cols + colIdx;


    return matrix.data[index];
}

Matrix add(const Matrix matrix1, const Matrix matrix2)
{
    Matrix result;


    if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols ||
        matrix1.data == NULL || matrix2.data == NULL) {

        result.rows = 0;
        result.cols = 0;
        result.data = NULL;
        return result;
        }


    result = createMatrix(matrix1.rows, matrix1.cols);


    if (result.data == NULL) {
        return result;
    }


    unsigned int totalElements = matrix1.rows * matrix1.cols;

    for (unsigned int i = 0; i < totalElements; i++) {
        result.data[i] = matrix1.data[i] + matrix2.data[i];
    }

    return result;
}

Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
    Matrix result;


    if (matrix1.data == NULL || matrix2.data == NULL ||
        matrix1.rows == 0 || matrix1.cols == 0 ||
        matrix2.rows == 0 || matrix2.cols == 0) {

        result.rows = 0;
        result.cols = 0;
        result.data = NULL;
        return result;
        }


    if (matrix1.cols != matrix2.rows) {

        result.rows = 0;
        result.cols = 0;
        result.data = NULL;
        return result;
    }


    result = createMatrix(matrix1.rows, matrix2.cols);

    if (result.data == NULL) {
        return result;
    }


    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++) {

                unsigned int index1 = i * matrix1.cols + k;


                unsigned int index2 = k * matrix2.cols + j;

                sum += matrix1.data[index1] * matrix2.data[index2];
            }


            unsigned int indexResult = i * result.cols + j;
            result.data[indexResult] = sum;
        }
    }

    return result;
}