function [ATTIVA,FIG,FIG_ENG] = report_ThL(rThL,m,NodoThermLink,toolrif,...
    unitrif,datarif,Font_caption,Font_tools,siteID,DT,DT_ENG,FIG,FIG_ENG,...
    activeEN,br,status,ATTIVA,conn,FileName)

fileID = fopen(FileName,'a');
fmt = '%s \r';
text = 'report_ThL function started';
fprintf(fileID,fmt,text);

import mlreportgen.dom.*
import mlreportgen.report.*

% Make sure DOM is compilable
makeDOMCompilable()

NODATAThL(m,1) = 0;
check = 0;
while check == 0
    if status(m,1) == 1
        if ATTIVA == 1
            ATTIVA = 0;
            testo = Paragraph('Catena non ancora installata o inattiva.');
            add(DT,testo);
            if activeEN == 1
                testo = Paragraph('Array not installed or inactive.');
                add(DT_ENG,testo);
            end
        end
        check = 1; 
    elseif status(m,1) == 3
        if ATTIVA == 1
            ATTIVA = 0;
            testo = Paragraph(['Monitoraggio completato. Invitiamo a visitare la '...
                'piattaforma web per la visione dei dati storici.']);
            add(DT,testo);
            if activeEN == 1
                testo = Paragraph(['Monitoring completed. Please log into the web-based platform '...
                    'to access the complete dataset.']);
                add(DT_ENG,testo);
            end
        end
        check = 1;
    else
        titolo = 1;
        ThE = Section();
        if activeEN == 1
            ThE_EN = Section();
        end
        for n = 1:rThL(m,1)
            NodeNum = num2str(NodoThermLink(n,m));
            comando = ['select T_node, NodeDepth, EventDate, EventTime from ElabDataView where EventDate >= ''' ...
                datarif ''' and  ToolNameID = ''' char(toolrif) ''' and UnitName = ''' char(unitrif) ...
                ''' and NodeNum = ''' NodeNum ''' '];
            curs = exec(conn,comando);
            curs = fetch(curs);
            Dati = curs.Data;
            % Modifico il formato di data e ora
            [rD,rC] = size(Dati);
            if rC ~= 1
                T = [cell2mat(Dati(:,3)) repmat(' ', [rD,1]) cell2mat(Dati(:,4))];
                Date = datenum(T); % Data
                if n == 1 % Il primo lo scrivo e creo la variabile di controllo
                else
                    [rS,~] = size(TempHR);
                    if rD ~= rS
                        NODATAThL(m,1) = 2;
                        check = 0;
                        break
                    else
                        check = 1;
                    end
                end
                TempHR(:,n) = cell2mat(Dati(:,1)); % temperatura
                MaxTempHR(1,n) = max(TempHR(:,n));
                MinTempHR(1,n) = min(TempHR(:,n));
                Prof(:,n) = -(cell2mat(Dati(1,2))); % Profondità
            else
                NODATAThL = 1;
                check = 1;
            end
        end
        if NODATAThL == 0
            testo = Paragraph(['I grafici seguenti mostrano i valori di temperatura lungo la verticale '...
                'ed il loro andamento nel tempo, misurati dal termometro ad alta risoluzione '...
                'durante il periodo temporale di riferimento. ']);
            testo.HAlign = 'justify';
            
            if activeEN == 1
                testo_ENG = Paragraph(['The following graphs show the temperature values recorded at different depths '...
                    'and their evolution over time, recorded by the high-resolution thermometer '...
                    'during the reference time period. ']);
                testo_ENG.HAlign = 'justify';
            end
            
            % grafico temperatura - profondità
            figure(1)
            plot(TempHR(end,:),Prof,'-o');
            hold on
            plot(MaxTempHR(1,:),Prof,'-o');
            Tmin_nodi = plot(MinTempHR(1,:),Prof,'-o');
            legend('Ultima temperatura','Temperatura massima','Temperatura minima','Location','southeast');
            
            title('Temperatura');
            xlabel(['Temperatura [' char(176) 'C]']);
            ylabel(['Profondit' char(224) ' [m]']);
            h = gca;
            h.XAxis.MinorTick = 'on';
            grid on
            grid minor
            
            TempName = char(strcat('Report',siteID,'-', char(toolrif),'Therm.png'));
            saveas(Tmin_nodi,TempName);
            ChartTh = Image(TempName);
            ChartTh.Style = {Height('9.5cm'),HAlign('center')};
            ChartTh_cap = Paragraph(['Fig. ' num2str(FIG) ' - Ultimo dato, '...
                'temperatura massima e minima misurate '...
                'durante il periodo temporale di riferimento']);
            FIG = FIG+1;
            ChartTh_cap.Style = {HAlign('center'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
            if titolo == 1
                sezTherm = Heading3('Therm Link');
                sezTherm.Style = {OuterMargin('0in','0in','0.15in','0in'),FontSize(Font_tools),Bold(1),Italic(1)};
                ThE.Title = sezTherm;
                add(DT,sezTherm);
                if activeEN == 1
                else
                    titolo = 0;
                end
            end
            add(DT,testo);
            add(DT,ChartTh);
            add(DT,ChartTh_cap);
            
            % Grafico Temperatura vs Tempo
            cont = 1;
            num = 2;
            tab = 1;
            while cont <= rThL(m,1)
                figure(num)
                if rThL(m,1)-cont > 5
                    str = cell(5,1);
                else
                    str = cell(rThL(m,1)-cont+1,1);
                end
                hold on
                grid on
                grid minor
                xlabel('Data [gg/mm/aaaa]');
                ylabel(['Temperatura [' char(176) 'C]']);
                Mx = [];
                Mn = [];
                for a = 1:5
                    if cont > rThL(m,1)
                        break
                    end
                    Tt = plot(Date,TempHR(:,cont));
                    Mx = [Mx; ceil(max(TempHR(:,cont)))];
                    Mn = [Mn; ceil(min(TempHR(:,cont)))];
                    xlim([Date(1)-1 Date(end)+1]);
                    h = gca;
                    h.XAxis.MinorTick = 'on';
                    datetick('x','dd/mm/yyyy','keepticks','keeplimits');
                    xtickangle(20);
                    str(a,1) = cellstr(['' num2str(Prof(cont)) ' m da P.C.']);
                    cont = cont+1;
                end
                ylim([min(Mn)-1 max(Mx)+1]);
                lgd = legend(str,'Location','north');
                num = num+1;
                TempName = char(strcat('Report',siteID,'-', char(toolrif),'Therm ',num2str(num),'.png'));
                saveas(Tt,TempName);
                if tab == 2
                    TempName1 = char(strcat('Report',siteID,'-', char(toolrif),'Therm ',num2str(num-1),'.png'));
                    Chart1 = Image(TempName1);
                    Chart1.Style = {Height('6.8cm'),HAlign('center')};
                    Chart2 = Image(TempName);
                    Chart2.Style = {Height('6.8cm'),HAlign('center')};
                    lot = Table({Chart1, Chart2});
                    lot.Style = {ResizeToFitContents(false), Width('100%')};
                    add(DT,lot);
                    tab = 1;
                elseif rThL <= 5 % Una sola immagine
                    Chart1 = Image(TempName);
                    Chart1.Style = {Height('7.5cm'),HAlign('center')};
                    add(DT,Chart1);
                elseif cont > rThL
                    Chart2 = Image(TempName);
                    Chart2.Style = {Height('6.8cm'),HAlign('center')};
                    add(DT,Chart2);
                else
                    tab = tab+1;
                end
                close(figure(num));
            end
            
            ChartTh2_cap = Paragraph(['Fig. ' num2str(FIG) ' - Andamento nel tempo della temperatura registrata '...
                'dai singoli sensori']);
            add(DT,ChartTh2_cap);
            FIG = FIG+1;
            ChartTh2_cap.Style = {HAlign('center'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
            
            add(DT,br);
            close(figure(1));
            close(figure(2));
            
            if activeEN == 1
                % grafico temperatura - profondità
                figure(1)
                plot(TempHR(end,:),Prof,'-o');
                hold on
                plot(MaxTempHR(1,:),Prof,'-o');
                Tmin_nodi = plot(MinTempHR(1,:),Prof,'-o');
                legend('Last value','T max','T min','Location','southeast');
                
                title('Temperature');
                xlabel(['Temperature [' char(176) 'C]']);
                ylabel('Depth [m]');
                h = gca;
                h.XAxis.MinorTick = 'on';
                grid on
                grid minor
                
                TempName = char(strcat('Report',siteID,'-', char(toolrif),'Therm_ENG.png'));
                saveas(Tmin_nodi,TempName);
                ChartTh = Image(TempName);
                ChartTh.Style = {Height('9.5cm'),HAlign('center')};
                ChartTh_cap = Paragraph(['Fig. ' num2str(FIG_ENG) ' - Last value, '...
                    'maximum and minimum temperature recorded '...
                    'during the reference time period']);
                FIG_ENG = FIG_ENG+1;
                ChartTh_cap.Style = {HAlign('center'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
                if titolo == 1
                    sezTherm = Heading3('Therm Link');
                    sezTherm.Style = {OuterMargin('0in','0in','0.15in','0in'),FontSize(Font_tools),Bold(1),Italic(1)};
                    ThE.Title = sezTherm;
                    add(DT_ENG,sezTherm);
                    titolo = 0;
                end
                add(DT_ENG,testo_ENG);
                add(DT_ENG,ChartTh);
                add(DT_ENG,ChartTh_cap);
                
                % Grafico Temperatura vs Tempo
                cont = 1;
                num = 2;
                tab = 1;
                while cont <= rThL(m,1)
                    figure(num)
                    if rThL(m,1)-cont > 5
                        str = cell(5,1);
                    else
                        str = cell(rThL(m,1)-cont+1,1);
                    end
                    hold on
                    grid on
                    grid minor
                    xlabel('Date [dd/mm/yyyy]');
                    ylabel(['Temperature [' char(176) 'C]']);
                    Mx = [];
                    Mn = [];
                    for a = 1:5
                        if cont > rThL(m,1)
                            break
                        end
                        Tt = plot(Date,TempHR(:,cont));
                        Mx = [Mx; ceil(max(TempHR(:,cont)))];
                        Mn = [Mn; ceil(min(TempHR(:,cont)))];
                        xlim([Date(1)-1 Date(end)+1]);
                        h = gca;
                        h.XAxis.MinorTick = 'on';
                        datetick('x','dd/mm/yyyy','keepticks','keeplimits');
                        xtickangle(20);
                        str(a,1) = cellstr(['' num2str(Prof(cont)) ' m b.g.l.']);
                        cont = cont+1;
                    end
                    ylim([min(Mn)-1 max(Mx)+1]);
                    lgd = legend(str,'Location','north');
                    num = num+1;
                    TempName = char(strcat('Report',siteID,'-', char(toolrif),'Therm ',num2str(num),'.png'));
                    saveas(Tt,TempName);
                    if tab == 2
                        TempName1 = char(strcat('Report',siteID,'-', char(toolrif),'Therm ',num2str(num-1),'.png'));
                        Chart1 = Image(TempName1);
                        Chart1.Style = {Height('6.8cm'),HAlign('center')};
                        Chart2 = Image(TempName);
                        Chart2.Style = {Height('6.8cm'),HAlign('center')};
                        lot = Table({Chart1, Chart2});
                        lot.Style = {ResizeToFitContents(false), Width('100%')};
                        add(DT,lot);
                        tab = 1;
                    elseif rThL <= 5 % Una sola immagine
                        Chart1 = Image(TempName);
                        Chart1.Style = {Height('7.5cm'),HAlign('center')};
                        add(DT,Chart1);
                    elseif cont > rThL
                        Chart2 = Image(TempName);
                        Chart2.Style = {Height('6.8cm'),HAlign('center')};
                        add(DT,Chart2);
                    else
                        tab = tab+1;
                    end
                    close(figure(num));
                end
                
                ChartTh2_cap = Paragraph(['Fig. ' num2str(FIG_ENG) ' - Temporal evolution of the temperature recorded '...
                    'by the sensors']);
                add(DT_ENG,ChartTh2_cap);
                FIG_ENG = FIG_ENG+1;
                ChartTh2_cap.Style = {HAlign('center'),FontSize(Font_caption),Italic(1),Color('midnightblue')};
                
                add(DT_ENG,br);
                close(figure(1));
                close(figure(2));
            end
            
            clear TempHR;
            clear MaxTempHR;
            clear MinTempHR;
            clear Prof;
            
        elseif NODATA(m,1) == 1
            if ATTIVA == 1
                testo = Paragraph('Non sono presenti dati relativi al periodo temporale considerato.');
                ATTIVA = 0;
                add(DT,testo);
                if activeEN == 1
                    testo = Paragraph('No data available for the reference time period.');
                    add(DT_ENG,testo);
                    if m == 1
                        add(DT_ENG,br);
                    end
                end
                check = 1;
            end
        end
    end
end

close all

text = 'report_ThL function executed correctly';
fprintf(fileID,fmt,text);
fclose(fileID);

end