function Text = appendMATLABCodeForOptions(Text,Options,WeightStrings)
% Low level utility function to add MATLAB Code for Options to Text

% Copyright 2015-2023 The MathWorks, Inc.
if nargin < 3
    WeightStrings = [];
end

ReducespecName = 'R';
switch class(Options)
    case 'mor.BalancedTruncationOptions'
        CommandName = getString(message('Control:mrtool:BalancedTruncationTab'));
        DefaultOptions = mor.BalancedTruncationOptions();
    case 'mor.ModalTruncationOptions'
        CommandName = getString(message('Control:mrtool:ModalTruncationTab'));
        DefaultOptions = mor.ModalTruncationOptions();
    case 'mor.SparseBalancedTruncationOptions'
        CommandName = getString(message('Control:mrtool:BalancedTruncationTab'));
        DefaultOptions = mor.SparseBalancedTruncationOptions();
    case 'mor.SparseModalTruncationOptions'
        CommandName = getString(message('Control:mrtool:ModalTruncationTab'));
        DefaultOptions = mor.SparseModalTruncationOptions();
end
if ~isequal(Options,DefaultOptions)
    % put the title
    OptionsTitle = ['% ' getString(message('Control:mrtool:CodegenCreateOptionsMOR',CommandName))];
    Text = controllib.internal.codegen.appendMATLABCode(Text,OptionsTitle);
    % add different ones
    FieldNames = fieldnames(DefaultOptions);
    OptionNamesToSet={};
    OptionValuesToSet={};
    for ct=1:length(FieldNames)
        if ~isequal(Options.(FieldNames{ct}),DefaultOptions.(FieldNames{ct}))
            OptionNamesToSet = vertcat(OptionNamesToSet,FieldNames(ct));
            if strcmpi(FieldNames{ct},"InputWeight")
                Value = WeightStrings(1);
            elseif strcmpi(FieldNames{ct},"OutputWeight")
                Value = WeightStrings(2);
            else
                Value = Options.(FieldNames{ct});
            end
            OptionValuesToSet = vertcat(OptionValuesToSet,Value);
        end
    end
    if ~isempty(OptionNamesToSet)
        for ct=1:size(OptionNamesToSet,1)
            VarName = sprintf('%s.Options.%s',ReducespecName,OptionNamesToSet{ct});
            if strcmpi(OptionNamesToSet{ct},'SepTol')
                switch class(Options)
                    case 'mor.BalancedTruncationOptions'
                        Comment = ['% ',getString(message('Control:mrtool:CodegenOptionsBTSepTol'))];
                    otherwise
                        Comment = ['% ',getString(message('Control:mrtool:CodegenOptionsMTSepTol'))];
                end
            elseif strcmpi(OptionNamesToSet{ct},'Offset')
                switch class(Options)
                    case 'mor.SparseBalancedTruncationOptions'
                        Comment = ['% ',getString(message('Control:mrtool:CodegenOptionsSparseBTOffset'))];
                    otherwise
                        Comment = ['% ',getString(message(['Control:mrtool:CodegenOptions' OptionNamesToSet{ct}]))];
                end
            else
                Comment = ['% ',getString(message(['Control:mrtool:CodegenOptions' OptionNamesToSet{ct}]))];
            end
            Text = controllib.internal.codegen.appendMATLABCode(Text,Comment);
            if any(strcmpi(OptionNamesToSet{ct},["InputWeight" "OutputWeight"]))
                Text = controllib.internal.codegen.appendMATLABCode(Text,sprintf('%s = %s;',VarName,OptionValuesToSet{ct}));
            else
                Text = controllib.internal.codegen.appendMATLABCode(Text,OptionValuesToSet{ct},VarName);
            end
        end
    end
end