"""The main code with all the modules loading and running them. Will have to adapt to the new code 927
The final step would be to add the sampling algorithm here...."""


from data_loader import list_data, list_directories, list_files, loadx_variable
from preprocessor import preprocess_data
from arima_model import plot_time_series
from lstm_model import create_dataset, plot_training_history, plot_forecast
from utils import load_config
from utils_plot import Plotter
import time
from datetime import datetime




def main():



    # Loads the configuration file with all settings
    config = load_config('config.yaml')

    # Lists the main directory of the data
    path = config['data_path']
    version = config['version']
    index_pairs = config['index_pairs']
    data = list_data(path)
    files = list_files(path)
    dir = list_directories(path)
    check_variable = config['variables']

    # Create a dictionary to hold the datasets
    datasets = {}

    # Load data for each variable

    datasets = loadx_variable(path,dir,check_variable,version)

    # Get common latitudes and longitudes (assuming all datasets have the same)
    #print("datasets.values()...",datasets.values())
    #print("iter(datasets.values())....",iter(datasets.values()))
    #print("next(iter(datasets.values()))...",next(iter(datasets.values())))
    sample_dataset = next(iter(datasets.values()))
    latitudes = sample_dataset['latitude']
    longitudes = sample_dataset['longitude']

    # Iterate over all variables and index pairs
    for variable, dataset in datasets.items():
        # Initialize the Plotter class
        plotter = Plotter(
            data_array=dataset,
            latitudes=latitudes,
            longitudes=longitudes,
            datasets=datasets,
            output_dir='plots'
        )

        # Call the plot_series method for each variable
        plotter.plot_series(index_pairs)

        # Call the plot_with_markers method for each variable
        time_index = 100

        plotter.plot_with_markers(variable, time_index, index_pairs)


    #scaled_data, scaler = preprocess_data(data)

    # ARIMA Model
    #arima_model = train_arima_model(scaled_data, **config['arima_params'])
    #plot_arima_results(scaled_data, arima_model, 10, config['output_path'])

    # LSTM Model
    #lstm_model = create_lstm_model((scaled_data.shape[1], 1), config['lstm_params']['neurons'])
    #lstm_model = train_lstm_model(lstm_model, scaled_data, scaled_data, config['lstm_params']['epochs'], config['lstm_params']['batch_size'])
    #plot_lstm_results(scaled_data, lstm_model, scaler, 10, config['output_path'])

if __name__ == "__main__":
    start_time_main = time.time()
    print("Running main.............",datetime.now().strftime("%Y-%m-%d_%HH:%MM:%SS"))
    main()
    end_time_main = time.time()
    time_used_main = (end_time_main - start_time_main)
    print("Time used for entire Code.../zzzz", f'{time_used_main:.2f}', "Seconds")
    print("Time used for entire Code..../zzzz", f'{time_used_main/60:.2f}', "Minutes")
    print("Time used for entire Code..../zzzz", f'{time_used_main/3600:.2f}', "Hours")