#include "ui.h"
#include "service.h"
#include "building.h"
#include "house.h"
#include "block.h"
#include <iostream>
#include <string>
#include <vector>

UI::UI(){
    this->service.addBuilding(new House(1900, true));
    this->service.addBuilding(new House(1900, false));
    this->service.addBuilding(new House(2000, false));
    this->service.addBuilding(new Block(2000, 100, 2));
    this->service.addBuilding(new Block(1900, 100, 100));
}

void UI::run(){
    while(true){
        std::cout << "1. Add building\n";
        std::cout << "2. Show all buildings\n";
        std::cout << "3. Show all buildings to be restored built in a given year\n";
        std::cout << "4. Write to file\n";
        std::cout << "0. Exit\n";

        char option;
        std::cout << "Choose an option: ";
        std::cin >> option;

        switch(option){
            case '1':{
                Building* building;
                char type;
                std::cout << "Choose the type of building (h - house, b - block): ";
                std::cin >> type;
                if(type == 'h'){
                    int constructionYear;
                    bool isHistorical;
                    std::cout << "Construction year: ";
                    std::cin >> constructionYear;
                    std::cout << "Is historical (0 - no, 1 - yes): ";
                    std::cin >> isHistorical;
                    building = new House(constructionYear, isHistorical);
                }
                if(type == 'b'){
                    int constructionYear;
                    int totalApartments;
                    int occupiedApartments;
                    std::cout << "Construction year: ";
                    std::cin >> constructionYear;
                    std::cout << "Total apartments: ";
                    std::cin >> totalApartments;
                    std::cout << "Occupied apartments: ";
                    std::cin >> occupiedApartments;
                    building = new Block(constructionYear, totalApartments, occupiedApartments);
                }
                if(type != 'h' && type != 'b'){
                    std::cout << "Invalid type!\n";
                    break;
                }
                this->service.addBuilding(building);
                break;
            }
            case '2':{
                std::vector<Building*> buildings = this->service.getAllBuildings();
                for(auto building : buildings){
                    std::cout << building->toString() << "\n";
                }
                break;
            }
            case '3':{
                int year;
                std::cout << "Year: ";
                std::cin >> year;
                std::vector<Building*> buildings = this->service.getAllToBeRestored();
                for(auto building : buildings){
                    if(building->getConstructionYear() == year){
                        std::cout << building->toString() << "\n";
                    }
                }
                break;
            }
            case '4':{
                std::string fileName_restore;
                std::cout << "File name: (for restoring)";
                std::cin >> fileName_restore;
                std::vector<Building*> buildings = this->service.getAllToBeRestored();
                this->service.writeToFile(fileName_restore, buildings);
                std::string fileName_demolish;
                std::cout << "File name: (for demolishing)";
                std::cin >> fileName_demolish;
                buildings = this->service.getAllToBeDemolished();
                this->service.writeToFile(fileName_demolish, buildings);
                break;
            }
            case '0':{
                exit(0);
            }
            default:{
                std::cout << "Invalid option!\n";
                break;
            }

        }
    }
}
