function smoother_plots(M_, options_, etahat, linear_etahat)
% smoother_plots(M_, options_, etahat, linear_etahat)
% Plots OccBin smoothed shocks, comparing piecewise-linear vs linear smoother.
%
% INPUTS
% - M_              [structure]  model description
% - options_        [structure]  options
% - etahat          [double]     (r*T) piecewise-linear smoothed shocks (DR order)
% - linear_etahat   [double]     (r*T) linear smoothed shocks (DR order), or [] if unavailable
%
% OUTPUTS
%   none (produces figures and optionally TeX files)

% Copyright © 2022-2026 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.

GraphDirectoryName = CheckPath('graphs', M_.fname);
latexFolder = CheckPath('latex', M_.dname);

has_linear = ~isempty(linear_etahat);

fidTeX = -1;
if options_.TeX && any(strcmp('eps', cellstr(options_.graph_format)))
    fidTeX = fopen([latexFolder filesep M_.fname '_OccBin_smoother_plots.tex'], 'w');
    fprintf(fidTeX, '%% TeX eps-loader file generated by occbin.smoother_plots (Dynare).\n');
    fprintf(fidTeX, ['%% ' datestr(now, 0) '\n']);
    fprintf(fidTeX, ' \n');
end

j1 = 0;
ifig = 0;
for j = 1:M_.exo_nbr
    if max(abs(etahat(j,:))) > 1.e-8
        j1 = j1+1;
        if mod(j1, 9) == 1
            hh_fig = dyn_figure(options_.nodisplay, 'name', 'OccBin smoothed shocks');
            ifig = ifig+1;
            isub = 0;
        end
        isub = isub+1;
        subplot(3, 3, isub)
        if has_linear
            plot(linear_etahat(j,:)', 'linewidth', 2)
            hold on
        end
        plot(etahat(j,:)', 'r--', 'linewidth', 2)
        hold on
        plot([0 options_.nobs], [0 0], 'k--')
        set(gca, 'xlim', [0 options_.nobs])
        if options_.TeX
            title(['$' M_.exo_names_tex{j,:} '$'], 'interpreter', 'latex')
        else
            title(M_.exo_names{j,:}, 'interpreter', 'none')
        end

        if mod(j1, 9) == 0
            add_legend_and_save(options_, GraphDirectoryName, M_.fname, ifig, j1, has_linear, fidTeX);
        end
    end
end

if j1 > 0 && mod(j1, 9) ~= 0
    add_legend_and_save(options_, GraphDirectoryName, M_.fname, ifig, j1, has_linear, fidTeX);
end

if fidTeX ~= -1
    fclose(fidTeX);
end

end

function add_legend_and_save(options_, GraphDirectoryName, model_name, ifig, j1, has_linear, fidTeX)
    if has_linear
        annotation('textbox', [0.1, 0, 0.35, 0.05], 'String', 'Linear', ...
            'Color', 'Blue', 'horizontalalignment', 'center', 'interpreter', 'none');
    end
    annotation('textbox', [0.55, 0, 0.35, 0.05], 'String', 'Piecewise', ...
        'Color', 'Red', 'horizontalalignment', 'center', 'interpreter', 'none');
    dyn_saveas(gcf, [GraphDirectoryName filesep model_name '_smoothedshocks_occbin' int2str(ifig)], ...
        options_.nodisplay, options_.graph_format);
    if fidTeX ~= -1 && options_.TeX && any(strcmp('eps', cellstr(options_.graph_format)))
        fprintf(fidTeX, '\\begin{figure}[H]\n');
        fprintf(fidTeX, '\\centering \n');
        % don't use filesep as it will create issues with LaTeX on Windows
        fprintf(fidTeX, '\\includegraphics[width=%2.2f\\textwidth]{%s_smoothedshocks_occbin%s}\n', ...
            options_.figures.textwidth*min(j1/3, 1), [GraphDirectoryName '/' model_name], int2str(ifig));
        fprintf(fidTeX, '\\caption{OccBin smoothed shocks.}');
        fprintf(fidTeX, '\\label{Fig:smoothedshocks_occbin:%s}\n', int2str(ifig));
        fprintf(fidTeX, '\\end{figure}\n');
        fprintf(fidTeX, ' \n');
    end
end
