function fits = fitBackscatter(data, single_toggle)
%Fits models to data
%if single_toggle = 0, then don't fit the single layer models. For speed
%else fit all the models

% Check if the second parameter is provided
if nargin < 2
    single_toggle = 1; % Default value if not provided
end



completeFig(3) = figure;
% completeFig(3) = gcf;
title("Two Layer Model")

hold on

datafields = fieldnames(data);

if single_toggle == 1 %%fit the single layer models?

completeFig(1) = figure;
title("Single Layer Models With Altimetry")
hold on
completeFig(2) = figure;
title("Single Layer Models Without Altimetry")

%% EXPONENTIAL BSCATTER
%Fit the Exponential backscatter model. Loops over data in order to do one
%fit with all data and one fit with no altimitry data.
for ii = 1:numel(datafields)
    x = data.(datafields{ii}).inc;
    y = data.(datafields{ii}).sigma0;
    
    ft = fittype('exponential_bscatter(x,diel,s,A)' );
    %COEFF ORDER: A, DIEL, S
    f = fit (x, y, ft,...
        'StartPoint',[0.1, 3,deg2rad(10)],...
        'Upper',[1, 5,deg2rad(30)],...
        'Lower',[0.01, 0.25, 0]);
    fits.exp{ii} = f;
    
    plotFitdB(@(x)exponential_bscatter(x,f.diel,f.s,f.A),...
        x, y, completeFig(ii));
    
    % switch ii
    %     case 1
    %         title({'Exponential Backscatter Model', 'With Altimetry'});
    %     case 2
    %         title({'Exponential Backscatter Model', 'Without Altimetry'});
    % 
    % end
end

%% GAUSSIAN BSCATTER
%Fit the Gaussian backscatter model. Loops over data in order to do one
%fit with all data and one fit with no altimitry data.
datafields = fieldnames(data);
for ii = 1:numel(datafields)
    x = data.(datafields{ii}).inc;
    y = data.(datafields{ii}).sigma0;
    
    ft = fittype('gaussian_bscatter(x,diel,s,A)' );
    %COEFF ORDER: A, DIEL, S
    f = fit (x, y, ft,...
        'StartPoint',[0.1, 3,deg2rad(10)],...
        'Upper',[1, 10,deg2rad(30)],...
        'Lower',[0.01, 0.25, 0]);
    fits.gauss{ii} = f;
    
    plotFitdB(@(x)gaussian_bscatter(x,f.diel,f.s,f.A),...
        x, y, completeFig(ii));
    % switch ii
    %     case 1
    %         title({'Gaussian Backscatter Model', 'With Altimetry'});
    %     case 2
    %         title({'Gaussian Backscatter Model', 'Without Altimetry'});
    % 
    % end
end

%% HAGFORS BSCATTER
%Fit the Hagfors backscatter model. Loops over data in order to do one
%fit with all data and one fit with no altimitry data.
datafields = fieldnames(data);
for ii = 1:numel(datafields)
    x = data.(datafields{ii}).inc;
    y = data.(datafields{ii}).sigma0;
    
    ft = fittype('hagfors(x,diel,s,A)' );
    %COEFF ORDER: A, DIEL, S
    f = fit (x, y, ft,...
        'StartPoint',[0.1, 3,deg2rad(10)],...
        'Upper',[1, 10,deg2rad(30)],...
        'Lower',[0.01, 0.25, 0]);
    fits.hagfors{ii} = f;
    
    plotFitdB(@(x)hagfors(x,f.diel,f.s,f.A),...
        x, y, completeFig(ii));
    % switch ii
    %     case 1
    %         title({'Hagfors Backscatter Model', 'With Altimetry'});
    %     case 2
    %         title({'Hagfors Backscatter Model', 'Without Altimetry'});
    % 
    % end
end

% %% Muhleman radar model
% %Fit the Hagfors backscatter model. Loops over data in order to do one
% %fit with all data and one fit with no altimitry data.
% datafields = fieldnames(data);
% for ii = 1:numel(datafields)
%     x = data.(datafields{ii}).inc;
%     y = data.(datafields{ii}).sigma0;
% 
%     ft = fittype('muhleman(x,K1,K2)' );
%     %COEFF ORDER: A, DIEL, S
%     f = fit (x, y, ft,...
%         'StartPoint',[0.01, 0.1],...
%         'Upper',[2, 2],...
%         'Lower',[0.001, 0.001]);
%     fits.muhleman{ii} = f;
% 
%     plotFitdB(@(x)muhleman(x,f.K1,f.K2),...
%         x, y, completeFig(ii));
%     % switch ii
%     %     case 1
%     %         title({'Hagfors Backscatter Model', 'With Altimetry'});
%     %     case 2
%     %         title({'Hagfors Backscatter Model', 'Without Altimetry'});
%     % 
%     % end
% end

end
% %% TWO-LAYER BSCATTER
% %Fit the Two-Layer backscatter model. Loops over data in order to do one
% %fit with all data and one fit with no altimetry data.
% ii = 1;
% x = data.(datafields{ii}).inc;
% y = data.(datafields{ii}).sigma0;
% 
% ft = fittype('two_layer(x,eps1,eps2,alpha,s1,s2,h,A)', 'independent', 'x', 'dependent', 'y');
% %COEFF ORDER: A, Alpha, EPS1, EPS2, S1, S2, H
% opts = fitoptions(ft);
% opts.StartPoint = [0.25, 0.01, 2.5, 3.1, 0.1, 0.1, 1];
% opts.Lower = [0.01, 0.0001, 0.1, 0.1, 0.01, 0.01, 0.01];
% opts.Upper = [1, 0.5, 5, 5, 0.4, 0.4, 5];
% 
% [f, gof] = fit(x, y, ft, opts);
% fits.twoLayer{ii} = f;
% 
% plotFitdB(@(x)two_layer(x,f.eps1,f.eps2,f.alpha,f.s1,f.s2,f.h,f.A),...
%     x, y, completeFig(3));
% 
% % title({'Two-Layer Backscatter Model', 'With Altimetry'});

%% SIMPLE-TWO-LAYER BSCATTER
%Fit the Two-Layer backscatter model. Loops over data in order to do one
%fit with all data and one fit with no altimetry data.
datafields = fieldnames(data);
for ii = 1:numel(datafields)
% ii = 1;
x = data.(datafields{ii}).inc;
y = data.(datafields{ii}).sigma0;

ft = fittype('simple_two_layer(x, eps1, eps2, s1, s2, A)', 'independent', 'x', 'dependent', 'y');
%COEFF ORDER: A, EPS1, EPS2, S1, S2
opts = fitoptions(ft);
opts.StartPoint = [0.1, 1.3, 2, 0.02, 0.1];
opts.Lower = [0.01, 1, 1, 0.001, 0.001];
opts.Upper = [1, 5, 5, 0.2, 0.4];

[f, gof] = fit(x, y, ft, opts);
fits.simpleTwoLayer{ii} = f;

plotFitdB(@(x)simple_two_layer(x,f.eps1,f.eps2,f.s1,f.s2,f.A),...
    x, y, completeFig(3));

title({'Two-Layer Backscatter Model', 'With Altimetry'});
legend("two layer")
end


end

