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

// TODO Matrix-Funktionen implementieren

Matrix createMatrix(unsigned int rows, unsigned int cols)
{
    Matrix m;
    m.rows = rows;
    m.cols = cols;
    m.buffer = NULL;
 
    if(rows == 0 || cols == 0)
    {
        printf("Error");
        exit(EXIT_FAILURE);
    }
 
    m.buffer = calloc(rows * cols, sizeof(MatrixType));
    if (m.buffer == NULL)
    {
        
        m.rows = 0;
        m.cols = 0;
        m.buffer = NULL;
        return m;
    }
 
    return m;
}

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

void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
   
  matrix.buffer[(rowIdx * matrix.cols) + colIdx] = value;   //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
}

MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
}

static int can_add(Matrix matrix1, Matrix matrix2) {

    int can_add;

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

        can_add = 1;

    }
    else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows){

        can_add = 2;

    }
    else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows){

        can_add = 3;

    }
    else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols){

        can_add = 4;

    }
    else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols){

        can_add = 5;

    }
    else{

        can_add = 0;

    }

    return can_add;
    }

Matrix add(const Matrix matrix1, const Matrix matrix2) //wir addieren nur wenn beide Matrizen gleich groß sind oder eine von beiden eine Zeile oder eine Spalte besitzt
{
    int ok = can_add(matrix1,matrix2);
    
    unsigned int erg_rows = (matrix1.rows == matrix2.rows) ? matrix1.rows : (matrix1.rows == 1 ? matrix2.rows : matrix1.rows);
    unsigned int erg_cols = (matrix1.cols == matrix2.cols) ? matrix1.cols : (matrix1.cols == 1 ? matrix2.cols : matrix1.cols);
    
    Matrix matrix_erg = createMatrix(erg_rows, erg_cols);

    switch(ok)
    {
    case 1: 
    
        for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)

            matrix_erg.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
            
        break;

    case 2:

        for(int i = 0; i < erg_rows; i++){
            for(int j = 0; j < erg_cols; j++)

                matrix_erg.buffer[i * erg_cols + j] = matrix1.buffer[i * erg_cols + j] + matrix2.buffer[i];
        }
        break;

    case 3:
    
        for(int i = 0; i < erg_rows; i++){
            for(int j = 0; j < erg_cols; j++)

                matrix_erg.buffer[i * erg_cols + j] = matrix1.buffer[i] + matrix2.buffer[i * erg_cols + j];
        }
        break;

    case 4:

        for(int i = 0; i < erg_rows; i++){
            for(int j = 0; j < erg_cols; j++)

                matrix_erg.buffer[i * erg_cols + j] = matrix1.buffer[i * erg_cols + j] + matrix2.buffer[j];
        }
        break;

    case 5:
    
        for(int i = 0; i < erg_rows; i++){
            for(int j = 0; j < erg_cols; j++)

                matrix_erg.buffer[i * erg_cols + j] = matrix1.buffer[j] + matrix2.buffer[i * erg_cols + j];
        }
        break;

    default:
        
        matrix_erg.rows = 0;
        matrix_erg.cols = 0;
        matrix_erg.buffer = NULL;
        return matrix_erg;

    
    }

return matrix_erg;

    
    
}
static int can_multiply (Matrix matrix1, Matrix matrix2)
{
    int can_multiply = 0;

    if(matrix1.cols == matrix2.rows)
        can_multiply = 1;
        
    return can_multiply;
}


Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
    int ok = can_multiply(matrix1,matrix2);
    unsigned int erg_rows = matrix1.rows;
    unsigned int erg_cols = matrix2.cols;
    Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
    
    
    if (ok == 1)
    {

        for (int i = 0; i < erg_rows; i++)
        {
            for (int j = 0; j < erg_cols; j++)
            {
                MatrixType sum = 0;
                
                for (int k = 0; k < matrix1.cols; k++)
                {
                    sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
                }
                
                matrix_erg.buffer [i * erg_cols + j] = sum;
            }
 
        }

        return matrix_erg;
    }  
    else 
    {
        
        matrix_erg.rows = 0;
        matrix_erg.cols = 0;
        matrix_erg.buffer = NULL;
        return matrix_erg;
    }
}