#include "UI.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../controller/controller.h"

UI _UI_Constructor(Controller *controller){
    UI ui;
    ui.controller=controller;
    return ui;
}



void _UI_Destructor(UI* ui){
    _Controller_Destructor(ui->controller);
}
    

void _UI_start(UI* ui ){
    while(1){
        printf("\n");
        printf("1. Add a material\n");
        printf("2. Delete a material\n");
        printf("3. Update a material\n");
        printf("4. List all materials\n");
        printf("5. See all available materials past their expiration date\n");
        printf("6. See all materials with a given supplier which are in short supply\n");
        printf("7. Undo\n");
        printf("8. Redo\n");
        printf("9. Exit\n");
        printf("\n");
        printf("Enter command: ");
        char command = getchar();
        char buffer;
        if (command!='\n'&&(buffer=getchar())!='\n'&&buffer!=EOF){
            while(getchar()!='\n'&&buffer!=EOF);
            printf("Invalid Command\n");
            continue;
        }
        switch (command){
            case '1':
            {
                char* name = (char*)malloc(255);
                char* supplier=(char*)malloc(255);
                int quantity;
                int day;
                int month;
                int year;
                char* buffer = (char*)malloc(255);
                printf("Enter name: ");
                fgets(name,255,stdin);
                name[strlen(name)-1]='\0';
                if(strlen(name)==0){
                    printf("Invalid name\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter supplier: ");
                fgets(supplier,255,stdin);
                supplier[strlen(supplier)-1]='\0';
                if(strlen(supplier)==0){
                    printf("Invalid supplier\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter quantity: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                quantity = atoi(buffer);
                if (quantity==0){
                    printf("Invalid quantity\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter day: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                day = atoi(buffer);
                if (day==0 || day>31){
                    printf("Invalid day\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter month: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                month = atoi(buffer);
                if (month==0 || month>12){
                    printf("Invalid month\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter year: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                year = atoi(buffer);
                if (year==0 || year<1900){
                    printf("Invalid year\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                free(buffer);
                int response = _Controller_add(ui->controller,name,supplier,quantity,day,month,year);
                if(response==0){
                    printf("Material added successfully\n");
                }
                else if(response==1){
                    printf("Material not added\n");
                }
                else{
                    printf("Material already exists. Updated quantity\n");
                }
                break;
            }
            case '2':
            {
                char* name = (char*)malloc(255);
                char* supplier=(char*)malloc(255);
                int quantity;
                int day;
                int month;
                int year;
                char* buffer = (char*)malloc(255);
                printf("Enter name: ");
                fgets(name,255,stdin);
                name[strlen(name)-1]='\0';
                if(strlen(name)==0){
                    printf("Invalid name\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter supplier: ");
                fgets(supplier,255,stdin);
                supplier[strlen(supplier)-1]='\0';
                if(strlen(supplier)==0){
                    printf("Invalid supplier\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter day: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                day = atoi(buffer);
                if (day==0 || day>31){
                    printf("Invalid day\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter month: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                month = atoi(buffer);
                if (month==0 || month>12){
                    printf("Invalid month\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter year: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                year = atoi(buffer);
                if (year==0 || year<1900){
                    printf("Invalid year\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                free(buffer);
                int response = _Controller_delete(ui->controller,name,supplier,day,month,year);
                if(response==0){
                    printf("Material deleted successfully\n");
                }
                else{
                    printf("Material not deleted\n");
                }
                break;
            }
            case '3':
            {
                char* name = (char*)malloc(255);
                char* supplier=(char*)malloc(255);
                int quantity;
                int day;
                int month;
                int year;
                char* buffer = (char*)malloc(255);
                printf("Enter name: ");
                fgets(name,255,stdin);
                name[strlen(name)-1]='\0';
                if (strlen(name)==0){
                    printf("Invalid name\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter supplier: ");
                fgets(supplier,255,stdin);
                supplier[strlen(supplier)-1]='\0';
                if (strlen(supplier)==0){
                    printf("Invalid supplier\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter quantity: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                quantity = atoi(buffer);
                if (quantity==0){
                    printf("Invalid quantity\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter day: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                day = atoi(buffer);
                if (day==0 || day>31){
                    printf("Invalid day\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter month: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                month = atoi(buffer);
                if (month==0 || month>12){
                    printf("Invalid month\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                printf("Enter year: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                year = atoi(buffer);
                if (year==0 || year<1900){
                    printf("Invalid year\n");
                    free(buffer);
                    free(name);
                    free(supplier);
                    break;
                }
                free(buffer);
                int response = _Controller_update(ui->controller,name,supplier,quantity,day,month,year);
                if(response==0){
                    printf("Material updated successfully\n");
                }
                else{
                    printf("Material not updated\n");
                }
                break;
            }
            case '4':
            {
                for(int i=0;i<_Controller_getLenght(ui->controller);i++){
                    char* s =_Material_stringify(_Controller_getItem(ui->controller,i));
                    printf("%s",s);
                    free(s);
                }
                break;
            }
            case '5':
            {
                char string[255];
                printf("Enter string to compare: ");
                fgets(string,255,stdin);
                int lenght=0;
                Material** vector_material=_Controller_getExpiredMaterial(ui->controller,string,&lenght);
                for(int i=0;i<lenght;i++){
                    char* s =_Material_stringify(vector_material[i]);
                    printf("%s",s);
                    free(s);
                }
                free(vector_material);
                break;
            }
            case '6':
            {
                int quantity;
                char* buffer = (char*)malloc(255);
                printf("Enter quantity: ");
                fgets(buffer,255,stdin);
                buffer[strlen(buffer)-1]='\0';
                quantity = atoi(buffer);
                if (quantity==0){
                    printf("Invalid quantity\n");
                    free(buffer);
                    break;
                }
                free(buffer);
                Vector* vector = _Controller_getShortSupplyItems(ui->controller,quantity);
                for(int i=0;i<_Vector_getLength(vector);i++){
                    char* s =_Material_stringify((Material*)_Vector_getItem(vector,i));
                    printf("%s",s);
                    free(s);
                }
                _Vector_Destructor(vector);
                break;
            }
            case '7':
            {
                int response = _Controller_undo(ui->controller);
                if(response==0){
                    printf("Undo successful\n");
                }
                else{
                    printf("Undo not successful\n");
                }
                break;
            }
            case '8':
            {
                int response = _Controller_redo(ui->controller);
                if(response==0){
                    printf("Redo successful\n");
                }
                else{
                    printf("Redo not successful\n");
                }
                break;
            }
            case '9':
            {
                _UI_Destructor(ui);
                return;
            }
            default:
            {
                printf("Invalid Command\n");
            }
        }

    }
}