%% getParampm.m
% date: 18/12/2023
% version: 1.0.5
%%
function [paramValue] = getParampm(paramStruct,paramName)

flag_warning = 1;

%Testing if the input struct is motorProps or settingsMagnetostatics
if isfield(paramStruct,'stator')
    % All possible parameters will be printed
    if strcmp(paramName,'all')
        flag_warning = 0;
        % Open .json file with all motorProps fields
        jsonfileName = 'motorProps.json';
        path = getpmpath('bin/assets/scripts/');
        printParam(path,jsonfileName,paramStruct)
        if(strcmp(paramStruct.stator.scriptName,'from DXF')~=1)
            % Open .json file stator script fields
            path = getpmpath('bin/assets/scripts/Stator/');
            jsonfileName = append(paramStruct.stator.scriptFile,'on');
            printParam(path,jsonfileName,paramStruct)
        end
        if(strcmp(paramStruct.rotor.scriptName, 'from DXF')~=1)
            % Open .json file with rotor script fields
            path = getpmpath('bin/assets/scripts/Rotor/');
            jsonfileName = append(paramStruct.rotor.scriptFile,'on');
            printParam(path,jsonfileName,paramStruct)
        end
    else
        % Searching individual parameters
        % format paramName and delete extra spaces
        paramName = strtrim(paramName);
        paramName = lower(paramName);
        paramName(1) = upper(paramName(1));
        %Adjusting names to access imported struct from json file
        paramNamesplit = split(paramName);
        word = paramNamesplit{1};
        new_paramName = strcat(upper(word(1)),lower(word(2:end)));
        for i=2:length(paramNamesplit)
            word = paramNamesplit{i};
            new_paramName = strcat(new_paramName,upper(word(1)),lower(word(2:end)));
        end
        jsonfileName = 'motorProps.json';
        path = getpmpath('bin/assets/scripts/');
        str = fileread([path jsonfileName]);
        data = jsondecode(str);
        if isfield(data,new_paramName)
            flag_warning = 0;
            key = data.(new_paramName);
            keySplit = split(key,'.');
            numberWords = length(keySplit);
            
            if length(keySplit)>1
                if numberWords == 1
                    paramValue = paramStruct.(key);
                elseif numberWords == 2
                    paramValue = paramStruct.(keySplit{1}).(keySplit{2});
                elseif numberWords == 3
                    paramValue = paramStruct.(keySplit{1}).(keySplit{2}).(keySplit{3});
                end
            else
                paramValue = paramStruct.(key);
            end
        else
            if(strcmp(paramStruct.stator.scriptName,'from DXF')~=1)
                %Stator - motorProps.stator.scriptProps
                path = getpmpath('bin/assets/scripts/Stator/');
                jsonfileName = append(paramStruct.stator.scriptFile,'on');
                str = fileread([path jsonfileName]);
                data = jsondecode(str);
                if isfield(data,new_paramName)
                    flag_warning = 0;
                    key = data.(new_paramName);
                    keySplit = split(key,'.');
                    numberWords = length(keySplit);
                    if numberWords == 1
                        paramValue = paramStruct.(key);
                    elseif numberWords == 2
                        paramValue = paramStruct.(keySplit{1}).(keySplit{2});
                    elseif numberWords == 3
                        paramValue = paramStruct.(keySplit{1}).(keySplit{2}).(keySplit{3});
                    end 
                end
            end
            if(strcmp(paramStruct.rotor.scriptName,'from DXF')~=1)
                %Rotor - motorProps.rotor.scriptProps
                path = getpmpath('bin/assets/scripts/Rotor/');
                jsonfileName = append(paramStruct.rotor.scriptFile,'on');
                str = fileread([path jsonfileName]);
                data = jsondecode(str);
                if isfield(data,new_paramName)
                    flag_warning = 0;
                    key = data.(new_paramName);
                    keySplit = split(key,'.');
                    numberWords = length(keySplit);
                    if numberWords == 1
                        paramValue = paramStruct.(key);
                    elseif numberWords == 2
                        paramValue = paramStruct.(keySplit{1}).(keySplit{2});
                    elseif numberWords == 3
                        paramValue = paramStruct.(keySplit{1}).(keySplit{2}).(keySplit{3});
                    end
                end
            end
        end
        
    end
elseif isfield(paramStruct,'MS_solvertype')
    %settingsMagnetostatic parameters
    if strcmp(paramName,'all')
        flag_warning = 0;
        
        path = getpmpath('bin/assets/scripts/');
        jsonfileName = 'settingsMagnetostatic.json';
        printParam(path,jsonfileName,paramStruct)
        
    else
        %Adjusting the names to access imported struct from json file
        paramNamesplit = split(paramName);
        word = paramNamesplit{1};
        new_paramName = strcat(upper(word(1)),lower(word(2:end)));
        for i=2:length(paramNamesplit)
            word = paramNamesplit{i};
            new_paramName = strcat(new_paramName,upper(word(1)),lower(word(2:end)));
        end
        path = getpmpath('bin/assets/scripts/');
        jsonfileName = 'settingsMagnetostatic.json';
        str = fileread([path jsonfileName]);
        data = jsondecode(str);
        if isfield(data,new_paramName)
            flag_warning = 0;
            key = data.(new_paramName);
            keySplit = split(key,'.');
            numberWords = length(keySplit);
            if numberWords == 1
                paramValue = paramStruct.(key);
            elseif numberWords == 2
                paramValue = paramStruct.(keySplit{1}).(keySplit{2});
            elseif numberWords == 3
                paramValue = paramStruct.(keySplit{1}).(keySplit{2}).(keySplit{3});
            end
        end
    end
else
    flag_warning = 2;
    
end

% Warning messages
warning_message(1) = string(sprintf('The parameter - %s - does not exist\n',paramName));
warning_message(2) = string(sprintf('Incorrect input structure, only motorProps or settingsMagnetostatics are valid.\n'));


if(flag_warning==1 || flag_warning == 2)
    error(warning_message(flag_warning));
end

end

% Function to print all parameters, same logic as individual search
%in a loop
function printParam(path,jsonfileName,paramStruct)

if isfield(paramStruct,'stator')
    n = 0;
else
    n = 5;
end
str = fileread([path jsonfileName]);
data = jsondecode(str);
fn = fieldnames(data);
for i =1:length(fn)-n
    key = data.(string(fn(i)));
    keySplit = split(key,'.');
    numberWords = length(keySplit);
    if length(keySplit)>1
        if numberWords == 1
            paramValue = paramStruct.(key);
        elseif numberWords == 2
            paramValue = paramStruct.(keySplit{1}).(keySplit{2});
        elseif numberWords == 3
            paramValue = paramStruct.(keySplit{1}).(keySplit{2}).(keySplit{3});
        end
    else
        paramValue = paramStruct.(key);
    end
    s= string(fn(i));
    s = regexprep(s, '([A-Z])', ' $1');
    txt = sprintf("%s = ",s);
    fprintf(txt);
    if(isempty(string(paramValue)))
        fprintf('[ ]');
    else
        fprintf(string(paramValue));
    end
    fprintf("\n");
end

end
