


% Create figure tables

clearvars -except tstart
close all


ModelLabels = {'BC', 'BEBA', 'CB', 'FJ'};
Model_ID_complete = {'Bounded confidence', 'Backfire Effect and Biased Assimilation', 'Classification-based', 'Friedkin-Johnsen'};

Digraph_order_1 = [3 8 5 1];
Digraph_order_2 = [13 38 23 3];

Plot_Letter = {'A', 'B', 'C', 'D', 'E', 'F'};
AdditionalInfoIndicative = {'U', 'U', 'U', 'D', 'D', 'D'};

TablePlotNames = {'APTE', 'UDTE', 'IOTE', 'APSS', 'UDSS', 'IOSS'};


IO_BC_APTE = [1217 287 2495 5112]; % Initial opinions BC model APTE plots
IO_BC_APSS = [1217 287 2495 5112]; % Initial opinions BC model APSS plots 

IO_BEBA_APTE = [1217 287 2022]; % Initial opinions BEBA model APTE plots
IO_BEBA_UDTE = [1217 287 2022]; % Initial opinions BEBA model UDTE plots
IO_BEBA_APSS = [287 2022 2495]; % Initial opinions BEBA model APSS plots
IO_BEBA_UDSS = [1217 287 2022]; % Initial opinions BEBA model UDSS plots

IO_FJ_APTE = [287 1217 2495]; % Initial opinions FJ model APTE plots
IO_FJ_UDTE = [287 1217 2495]; % Initial opinions FJ model UDTE plots
IO_FJ_APSS = [287 1217 2495]; % Initial opinions FJ model APSS plots
IO_FJ_UDSS = [287 1217 2495]; % Initial opinions FJ model UDSS plots

IO_CB_APTE = [1217 287 2022]; % Initial opinions CB model APTE plots
IO_CB_UDTE = [2022 1217 5112]; % Initial opinions CB model UDTE plots 
IO_CB_APSS = [287 2022 2495]; % Initial opinions CB model APSS plots
IO_CB_UDSS = [2022 1217 5112]; % Initial opinions CB model UDSS plots


AP_BC_plots = [55 133 2809 2842 30];
AP_BEBA_plots = [30 1268 133 2809 2842];
AP_CB_plots = [2382 1633 457 2809];
AP_FJ_plots = [55 30 2842 2626];

AP_BC_IOSS_indices = [5 2 3 4];
AP_BC_IOTE_indices = [1 2 3 4];

AP_BEBA_IOSS_indices = [3 1 4];
AP_BEBA_IOTE_indices = [3 1 4];
AP_BEBA_UDSS_indices = [1 3 2 4];
AP_BEBA_UDTE_indices = [2 3 4 5];

AP_CB_IOTE_indices = [1 2 3];
AP_CB_UDTE_indices = [1 2 3 4];
AP_CB_UDSS_indices = [1 2 3 4];
AP_CB_IOSS_indices = [1 2 3];

AP_FJ_IOTE_indices = [1 2 4];
AG_FJ_UDTE_indices = [1 2 3 4];
AP_FJ_UDSS_indices = [1 2 3 4];
AP_FJ_IOSS_indices = [1 2 4];


AP_BC_IOSS = AP_BC_plots(AP_BC_IOSS_indices); % [30 133 2809 2842]; % Agent parameters BC model IOSS plots
AP_BC_IOTE = AP_BC_plots(AP_BC_IOTE_indices); % [55 133 2809 2842]; % Agent paramters BC model IOTE plots 

AP_BEBA_IOSS = AP_BEBA_plots(AP_BEBA_IOSS_indices); % [133 30 2809]; % Agent parameters BEBA model IOSS plots 
AP_BEBA_IOTE = AP_BEBA_plots(AP_BEBA_IOTE_indices); % [133 30 2809]; % Agent parameters BEBA model IOTE plots
AP_BEBA_UDSS = AP_BEBA_plots(AP_BEBA_UDSS_indices); % [30 133 1268 2809]; % Agent parameters BEBA model UDSS plots
AP_BEBA_UDTE = AP_BEBA_plots(AP_BEBA_UDTE_indices); % [1268 133 2809 2842]; % Agent parameters BEBA model UDTE plots 

AP_CB_IOSS = AP_CB_plots(AP_CB_IOSS_indices); % [2382 1633 457]; % Agent parameters CB model IOSS plots
AP_CB_IOTE = AP_CB_plots(AP_CB_IOTE_indices); % [2382 1633 457]; % Agent parameters CB model IOTE plots
AP_CB_UDSS = AP_CB_plots(AP_CB_UDSS_indices); % [2382 1633 457 2809]; % Agent parameters CB model UDSS plots
AP_CB_UDTE = AP_CB_plots(AP_CB_UDTE_indices); % [2382 1633 457 2809]; % Agent parameters CB model UDTE plots 

AP_FJ_IOSS = AP_FJ_plots(AP_FJ_IOSS_indices); % [55 30 2626]; % Agent parameters FJ model IOSS plots
AP_FJ_IOTE = AP_FJ_plots(AP_FJ_IOTE_indices); % [55 30 2626]; % Agent parameters FJ model IOTE plots 
AP_FJ_UDSS = AP_FJ_plots(AP_FJ_UDSS_indices); % [55 30 2842 2626]; % Agent parameters FJ model UDSS plots
AG_FJ_UDTE = AP_FJ_plots(AG_FJ_UDTE_indices); % [55 30 2842 2626]; % Agent parameters FJ model UDTE plots



Total_Figures_Counter = 0;
max_num_Figures = 506;
AllFigures = cell(max_num_Figures,1);


for id_model = 1:4
    Model_ID = ModelLabels{id_model};
    
    for id_plot = 1:6

    switch id_plot
        case 1
            x_label = 'ap';
            y_label = 'di';
            indicative = '1';
            switch id_model
                case 1
                    x_labels = AP_BC_IOTE; % [55 133 2809 2842]; % Agent parameters BEBA model IOTE plots
                    y_labels = 1;
                case 2
                    x_labels = AP_BEBA_IOTE; % [133 30 2809]; % Agent paramters BC model IOTE plots 
                    y_labels = Digraph_order_1;
                case 3
                    x_labels = AP_CB_IOTE; % [2382 1633 457]; % Agent parameters CB model IOTE plots
                    y_labels = Digraph_order_2;
                case 4
                    x_labels = AP_FJ_IOTE; % [55 30 2626]; % Agent parameters FJ model IOTE plots 
                    y_labels = Digraph_order_2;
            end
            plot_type = 3; % IOTE

        case 2
            x_label = 'io';
            y_label = 'di';
            indicative = '2';
            switch id_model
                case 1
                    x_labels = IO_BC_APTE; % [1217 287 2495 5112]; % Initial opinions BC model APTE plots
                    y_labels = 1;
                case 2
                    x_labels = IO_BEBA_APTE; % [1217 287 2022]; % Initial opinions BEBA model APTE plots
                    y_labels = Digraph_order_1;
                case 3
                    x_labels = IO_CB_APTE; % [1217 287 2022]; % Initial opinions CB model APTE plots
                    y_labels = Digraph_order_2;
                case 4
                    x_labels = IO_FJ_APTE; % [287 1217 2495]; % Initial opinions FJ model APTE plots
                    y_labels = Digraph_order_2;
            end
            plot_type = 1; % APTE

        case 3
            x_label = 'io';
            y_label = 'ap';
            indicative = '3';
            switch id_model
                case 1
                    x_labels = [1217 2022 5112];
                    y_labels = [55 2809 133];
                case 2
                    x_labels = IO_BEBA_UDTE; % [1217 287 2022]; % Initial opinions BEBA model UDTE plots
                    y_labels = AP_BEBA_UDTE; % [1268 133 2809 2842]; % Agent parameters BEBA model UDTE plots 
                case 3
                    x_labels = IO_CB_UDTE; % [2022 1217 5112]; % Initial opinions CB model UDTE plots 
                    y_labels = AP_CB_UDTE; % [2382 1633 457 2809]; % Agent parameters CB model UDTE plots 
                case 4
                    x_labels = IO_FJ_UDTE; % [287 1217 2495]; % Initial opinions FJ model UDTE plots
                    y_labels = AG_FJ_UDTE; % [55 30 2842 2626]; % Agent parameters FJ model UDTE plots
            end
            plot_type = 2; % UDTE

        case 4
            x_label = 'io';
            y_label = 'di';
            indicative = '4';
            switch id_model
                case 1
                    x_labels = IO_BC_APSS; % [1217 287 2495 5112]; % Initial opinions BC model APSS plots 
                    y_labels = 1;
                case 2
                    x_labels = IO_BEBA_APSS; % [287 2022 2495]; % Initial opinions BEBA model APSS plots
                    y_labels = Digraph_order_1;
                case 3
                    x_labels = IO_CB_APSS; % [287 2022 2495]; % Initial opinions CB model APSS plots
                    y_labels = Digraph_order_2;
                case 4
                    x_labels = IO_FJ_APSS; % [287 1217 2495]; % Initial opinions FJ model APSS plots
                    y_labels = Digraph_order_2;
            end
            plot_type = 4; % APSS

        case 5
            x_label = 'io';
            y_label = 'ap';
            indicative = '5';
            switch id_model
                case 1
                    x_labels = [1217 2022 5112];
                    y_labels = [55 2809 133];
                case 2
                    x_labels = IO_BEBA_UDSS; % [1217 287 2022]; % Initial opinions BEBA model UDSS plots
                    y_labels = AP_BEBA_UDSS; % [30 133 1268 2809]; % Agent parameters BEBA model UDSS plots
                case 3
                    x_labels = IO_CB_UDSS; % [2022 1217 5112]; % Initial opinions CB model UDSS plots
                    y_labels = AP_CB_UDSS; % [2382 1633 457 2809]; % Agent parameters CB model UDSS plots
                case 4
                    x_labels = IO_FJ_UDSS; % [287 1217 2495]; % Initial opinions FJ model UDSS plots
                    y_labels = AP_FJ_UDSS; % [55 30 2842 2626]; % Agent parameters FJ model UDSS plots
            end
            plot_type = 5; % UDSS

        case 6
            x_label = 'ap';
            y_label = 'di';
            indicative = '6';
            switch id_model
                case 1
                    x_labels = AP_BC_IOSS; % [30 133 2809 2842]; % Agent parameters BC model IOSS plots
                    y_labels = 1;
                case 2
                    x_labels = AP_BEBA_IOSS; % [133 30 2809]; % Agent parameters BEBA model IOSS plots 
                    y_labels = Digraph_order_1;
                case 3
                    x_labels = AP_CB_IOSS; % [2382 1633 457]; % Agent parameters CB model IOSS plots
                    y_labels = Digraph_order_2;
                case 4
                    x_labels = AP_FJ_IOSS; % [55 30 2626]; % Agent parameters FJ model IOSS plots
                    y_labels = Digraph_order_2;
            end
            plot_type = 6; % IOSS

    end

    cell_width = 1;

    if size(y_labels,2) > 1
        % This happens for every model EXCEPT the BC model. So, for every
        % model except the BC model, the axes are reversed

        temp_var = y_labels;
        y_labels = x_labels;
        x_labels = temp_var;

        temp_var = y_label;
        y_label = x_label;
        x_label = temp_var;

        reverse = 1;
    else

        reverse = 0;

    end

    if (plot_type == 1)||(plot_type == 2)||(plot_type == 3)
        % APTE or UDTE or IOTE
        LinesOrPoints = 'curves';
    elseif (plot_type == 4)||(plot_type == 5)||(plot_type == 6)
        % APSS or UDSS or IOSS
        LinesOrPoints = 'points';
    end

    if id_model == 3
        AgentParametersIDs_small = [2809, 1633, 457, 30, 1206, 2382, 2531];
        AgentParametersIDs_medium = [508, 3057, 318, 1248, 1877, 436, 82, 229, 899, 3450, 388, 344, 2397, 2907, 1575];
    elseif id_model == 2
        AgentParametersIDs_small = [1268 2842 2626 2809 30 133];
        AgentParametersIDs_medium = [2842, 1666, 490, 2626, 1450, 274, 2809, 1633, 457, 133, 1309, 2485, 2553, 1377, 201];
    else
        AgentParametersIDs_small = [55 2842 2626 2809 30 133];
        AgentParametersIDs_medium = [2842, 1666, 490, 2626, 1450, 274, 2809, 1633, 457, 133, 1309, 2485, 2553, 1377, 201];
    end

    load('InitialOpinions_100_agents.mat', 'AllInitialOpinions')
    InitialOpinionIDs_small = [2495, 1217, 2022, 5112, 287]; 
    InitialOpinionIDs_medium = [5081, 3057, 318, 1248, 1877, 4364, 82, 229, 899, 3450, 3889, 3443, 2397, 2907, 1575];
    InitialOpinionIDs_large = 1:size(AllInitialOpinions,1); 

    switch id_model
        case 1 
            ModelLabel = 'BC';
            ModelLabel_par = 'BC';
            model_parameter_explanation = 'confidence radius';
        case 2
            ModelLabel = 'BEBA';
            ModelLabel_par = 'FJ';
            model_parameter_explanation = 'entrenchment';
        case 3
            ModelLabel = 'CB';
            ModelLabel_par = 'CB';
            model_parameter_explanation = 'traits';
        case 4
            ModelLabel = 'FJ';
            ModelLabel_par = 'FJ';
            model_parameter_explanation = 'susceptibility';
    end

    load([ ModelLabel_par '_AgentParameters_100_agents.mat'], 'AllAgentParameters')
    AgentParametersIDs_large = 1:size(AllAgentParameters,1);

    load([ ModelLabel '_InitialNetworks_100_agents.mat'], 'InteractionNetworks')
    if id_model == 1
        DigraphIDs = 1;
    else
        DigraphIDs = 1:size(InteractionNetworks,2);
    end

    if plot_type == 1
        % APTE ~ the number of agent parameters
        NumberofLinesorPoints = size(AgentParametersIDs_medium,2);
        PlotTypeExplanation = ['For each one of these plots the initial opinion distributions and underlying digraphs are constant ' ... 
                               ' and the agent parameters are different for each line ' '.'];%  ' (the line colour represents the mean ' model_parameter_explanation ', ' ...
                               % ' see Table \ref{table:TableofTables}). '];
       whatchanges = ' agent parameters ';
       if id_model == 1
           whatdoesnotchange = ' initial opinion distributions';
       else
           whatdoesnotchange = ' initial opinion distributions and underlying digraphs';
       end
       
    elseif plot_type == 2
        % UDTE ~ the number of underlying digraphs
        NumberofLinesorPoints = size(DigraphIDs,2);
        PlotTypeExplanation = ['For each one of these plots the initial opinion distributions and agent parametres are constant ' ... 
                               ' and the underlying digraphs are different for each line  . '];
        whatchanges = ' underlying digraphs ';
        whatdoesnotchange = ' initial opinion distributions and agent parameters';
    elseif plot_type == 3
        % IOTE ~ the number of initial opinions
        NumberofLinesorPoints = size(InitialOpinionIDs_medium,2);
        PlotTypeExplanation = ['For each one of these plots the agent parameters and underlying digraphs are constant ' ... 
                               ' and the initial opinion distributions  are different for each line . '];
        whatchanges = ' initial opinion distributions ';
        if id_model == 1
           whatdoesnotchange = ' agent parameters';
       else
           whatdoesnotchange = ' agent parameters and underlying digraphs';
        end

    elseif plot_type == 4
        % APSS ~ the number of agent parameters
        NumberofLinesorPoints = size(AgentParametersIDs_large,2);
        PlotTypeExplanation = ['For each one of these plots the initial opinion distributions and underlying digraphs are constant ' ... 
                               ' and the agent parameters are different for each point '  ' .  '];%   '  (the point colour represents the mean ' model_parameter_explanation ', ' ...
                               % ' see Table \ref{table:TableofTables}). '];
       whatchanges = ' agent parameters ';
       whatdoesnotchange = ' initial opinion distributions and underlying digraphs';
    elseif plot_type == 5
        % UDSS ~ the number of underlying digraphs
        NumberofLinesorPoints = size(DigraphIDs,2);
        PlotTypeExplanation = ['For each one of these plots the initial opinion distributions and agent parametres are constant ' ... 
                               ' and the underlying digraphs are different for each point  . '];
        whatchanges = ' underlying digraphs ';
        whatdoesnotchange = ' initial opinion distributions and agent parameters';
    elseif plot_type == 6
        % IOSS ~ the number of initial opinions
        NumberofLinesorPoints = size(InitialOpinionIDs_large,2); 
        PlotTypeExplanation = ['For each one of these plots the agent parameters and underlying digraphs are constant ' ... 
                               ' and the initial opinion distributions  are different for each point . '];
        whatchanges = ' initial opinion distributions ';
        whatdoesnotchange = ' agent parameters and underlying digraphs';
    end

    NumberPlots = size(x_labels,2)*size(y_labels,2);
    
    % Create the files that will go on the text
    AdditionalLabel = ['   A full-page version of this table can be seen in Table \ref{tab:results_' Model_ID '_Plot_' num2str(id_plot) '_onepage}.'];

    CompleteCaption = [ '\textbf{'  TablePlotNames{plot_type} '} plots for the ' Model_ID_complete{id_model} ' model. ' ...
                        ' Each of the ' num2str(NumberPlots) ' \textbf{'  TablePlotNames{plot_type} '} plots includes '  num2str(NumberofLinesorPoints) '  ' LinesOrPoints    '   ' ...
                        ' corresponding to different choices of ' whatchanges ' and constant ' whatdoesnotchange '. '  ... PlotTypeExplanation  ... ' with different choices of ' whatchanges ' and constant ' whatdoesnotchange '. '  ... PlotTypeExplanation  ...
                        ' \Caption' Model_ID 'Plot' Plot_Letter{plot_type}  ' \AdditionalPlotInformation' AdditionalInfoIndicative{plot_type} ' ' AdditionalLabel ];


    fileID = fopen([ 'new_' Model_ID '_PlotTable_' num2str(id_plot) '.tex'], 'w');
    fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');
    fprintf(fileID,'%s','\begin{table}[h!]');fprintf(fileID,'\n');
    AgreementPlotLaTeXCreation
    fprintf(fileID,'%s',['	\label{tab:results_' Model_ID '_Plot_' num2str(id_plot) '}	']);fprintf(fileID,'\n');
    fprintf(fileID,'%s','\end{table}');fprintf(fileID,'\n');
    fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');
    fclose(fileID);

    cell_width = 1.7;

    % Create the files that will go on the Appendix
    AdditionalLabel = ''; 


    CompleteCaption = [ '\textbf{'  TablePlotNames{plot_type} '} plots for the ' Model_ID_complete{id_model} ' model. ' ...
                        ' Each of the ' num2str(NumberPlots) ' \textbf{'  TablePlotNames{plot_type} '} plots includes '  num2str(NumberofLinesorPoints) '  ' LinesOrPoints    '   ' ...
                        ' with different choices of ' whatchanges ' and constant ' whatdoesnotchange '. '  ... PlotTypeExplanation  ...
                        ' \Caption' Model_ID 'Plot' Plot_Letter{plot_type}  ' \AdditionalPlotInformation' AdditionalInfoIndicative{plot_type} ' ' AdditionalLabel ];

    fileID = fopen([ 'Appendix_new_' Model_ID '_PlotTable_' num2str(id_plot) '.tex'], 'w');
    fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');
    fprintf(fileID,'%s','\begin{sidewaystable}[htpb]');fprintf(fileID,'\n');
    AgreementPlotLaTeXCreation
    fprintf(fileID,'%s',['	\label{tab:results_' Model_ID '_Plot_' num2str(id_plot) '_onepage}	']);fprintf(fileID,'\n');
    fprintf(fileID,'%s','\end{sidewaystable}');fprintf(fileID,'\n');
    fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');
    fclose(fileID);

    end
end


save('RequiredFigures.mat', 'AllFigures')


