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

// TODO Matrix-Funktionen implementieren

Matrix createMatrix(unsigned int rows, unsigned int cols) {
    Matrix matrix;
    matrix.rows = 0;
    matrix.cols = 0;
    matrix.buffer = NULL;

    if (rows > 0 && cols > 0) {
        matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
        if (matrix.buffer == NULL) {
            return matrix;
        }
        matrix.rows = rows;
        matrix.cols = cols;
        for (unsigned int i=0;i<rows*cols;i++){
            matrix.buffer[i] = UNDEFINED_MATRIX_VALUE;
        }
    }

    return matrix;
}

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

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

MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    if(rowIdx<(matrix.rows) && colIdx<(matrix.cols)){
        return matrix.buffer[matrix.cols*rowIdx + colIdx];
    }
    else{
        return UNDEFINED_MATRIX_VALUE;
    }
}

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

    if(matrix1.rows==matrix2.rows && matrix1.cols==matrix2.cols){
        output.rows = matrix1.rows;
        output.cols = matrix1.cols;
        output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
        if(output.buffer == NULL){
            return output;
        }
        for (int i=0;i<matrix1.rows*matrix1.cols;i++){
            output.buffer[i]= matrix1.buffer[i]+matrix2.buffer[i];
        }
    }
    else if(matrix1.rows==matrix2.rows && (matrix1.cols>0&&matrix2.cols==1)){
        output.rows = matrix1.rows;
        output.cols = matrix1.cols;
        output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
        if(output.buffer == NULL){
            return output;
        }
        for (int i=0;i<matrix2.rows;i++){
            for (int ii=0;ii<matrix1.cols;ii++){
                output.buffer[(i*matrix1.cols)+ii]= matrix1.buffer[(i*matrix1.cols)+ii]+matrix2.buffer[i];
            }
        }
    }
    else if(matrix1.rows==matrix2.rows && (matrix1.cols==1&&matrix2.cols>0)){
        output.rows = matrix2.rows;
        output.cols = matrix2.cols;
        output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
        if(output.buffer == NULL){
            return output;
        }
        for (int i=0;i<matrix1.rows;i++){
            for (int ii=0;ii<matrix2.cols;ii++){
                output.buffer[(i*matrix2.cols)+ii]= matrix2.buffer[(i*matrix2.cols)+ii]+matrix1.buffer[i];
            }
        }
    }
    return output;
}

Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
    Matrix output = {.rows=0, .cols=0, .buffer=NULL};
    if(matrix1.cols==matrix2.rows){
        output.rows = matrix1.rows;
        output.cols = matrix2.cols;
        output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
        if(output.buffer == NULL){
            return output;
        }
        for (int spalte2=0;spalte2<output.cols;spalte2++){
            for (int zeile1=0;zeile1<matrix1.rows;zeile1++){
                for (int zeile2=0;zeile2<matrix2.rows;zeile2++){
                    output.buffer[zeile1*output.cols+spalte2]= output.buffer[zeile1*output.cols+spalte2] + (matrix1.buffer[zeile1*matrix1.cols+zeile2]*matrix2.buffer[zeile2*matrix2.cols+spalte2]);
                }
            }
        }
    }
    return output;
}