clc; clear; close all;

%% ===================== LOAD TABLES =====================
load('TAB_all_hoop_new.mat');         T_hoop   = TAB_all_hoop_new;
load('TAB_all_spiral_new.mat');       T_spiral = TAB_all_spiral_new;
load('TAB_all_tweel_new.mat');        T_tweel  = TAB_all_tweel_new;
load('TAB_all_twin_new.mat');         T_twin   = TAB_all_twin_new;

%% ===================== PARAMETERS TO COMPARE =====================
params = { ...
    'Maximum stress [Pa]', ...
    'Wheel mass [kg]', ...
    'Wheel stiffness [kg/s^2]', ...
    'Durability index'};

arch_names = {'Hoop','Spiral','Tweel','Twin'};

tables = {T_hoop, T_spiral, T_tweel, T_twin};

%% ===================== CHECK COLUMNS =====================
for i = 1:numel(tables)
    for j = 1:numel(params)
        assert(ismember(params{j}, tables{i}.Properties.VariableNames), ...
            'La colonna "%s" manca nella tabella %d.', params{j}, i);
    end
end

%% ===================== COMPUTE MEAN VALUES =====================

vals = zeros(numel(params), numel(tables));

for j = 1:numel(params)
    for i = 1:numel(tables)
        vals(j,i) = mean(tables{i}.(params{j}), 'omitnan');
    end
end

%% ===================== NORMALIZATION W.R.T. MAX =====================
vals_norm = zeros(size(vals));

for j = 1:numel(params)
    max_val = max(vals(j,:));
    vals_norm(j,:) = vals(j,:) / max_val;
end

%% ===================== PLOT 1 =====================
figure('Color','w','Position',[100 100 1250 850]);

colors = [
    0.20 0.45 0.70   
    0.85 0.33 0.10   
    0.47 0.67 0.19   
    0.49 0.18 0.56   
];

for j = 1:numel(params)
    subplot(2,2,j)
    
    b = bar(vals_norm(j,:), 0.65, 'FaceColor', 'flat', 'LineWidth', 2);
    b.CData = colors;
    b.EdgeColor = [0.2 0.2 0.2];
    
    set(gca, ...
        'XTick', 1:numel(arch_names), ...
        'XTickLabel', arch_names, ...
        'FontSize', 20, ...
        'Box', 'off', ...
        'LineWidth', 1);
    
    xtickangle(20)
    ylim([0 1.15])
    grid on
    ax = gca;
    ax.GridAlpha = 0.15;
    ax.YMinorGrid = 'off';
    ax.FontSize = 16;   
    ax.LineWidth = 1;   
    ax.TickLabelInterpreter = "latex";


    cleanLabel = regexprep(params{j}, '\s*\[.*?\]', '');
    
    ylabel(cleanLabel, 'Interpreter', 'latex', 'FontSize', 25, 'FontWeight', 'bold')
    
    
    for i = 1:numel(arch_names)
        text(i, vals_norm(j,i) + 0.03, sprintf('%.2f', vals_norm(j,i)), ...
            'HorizontalAlignment', 'center', ...
            'FontSize', 15, ...
            'Color', [0.15 0.15 0.15],'Interpreter', 'latex');
    end
end

% sgtitle('Confronto architetture ruota - valori normalizzati rispetto al massimo', ...
 %   'FontSize', 15, 'FontWeight', 'bold')

%% ===================== PLOT 2: GROUPED BAR CHART =====================
figure('Color','w','Position',[100 100 1000 500]);
bar(vals_norm')

set(gca, 'XTickLabel', arch_names, 'FontSize', 11)
legend(params, 'Interpreter','none', 'Location','bestoutside')

ylim([0 1.1])
grid on

%% ===================== SHOW RAW VALUES =====================
disp('Valori medi grezzi:');
RawTable = array2table(vals', ...
    'VariableNames', matlab.lang.makeValidName(params), ...
    'RowNames', arch_names);
disp(RawTable)

disp('Valori medi normalizzati rispetto al massimo:');
NormTable = array2table(vals_norm', ...
    'VariableNames', matlab.lang.makeValidName(params), ...
    'RowNames', arch_names);
disp(NormTable)



