function [paramsOut] = globalTrendGUI(hObject, callbackdata, inputData)%#ok<INUSL>
    theTitle ='Global trend parameters';
    defaultStruct = inputData.userData.globalTrend;

    while(true)
        mainFigure = findobj('Type', 'Figure', '-and', 'Name', inputData.name);
        userdata = get(mainFigure, 'UserData');
        if isempty(userdata) || ~isfield(userdata, 'globalTrend')
            paramsOut = struct();
        else
            paramsOut = userdata.globalTrend;
        end
        [defaultStruct, errors] = checkStructureDefaults(paramsOut, defaultStruct);
        
        if ~isempty(errors)
            warning('globalTrendGUI:bad parameters', getMessageString(errors)); %#ok<CTPCT>
        end
        
        % Creates structure for
        fNamesDefault = fieldnames(defaultStruct);
        for k = 1:length(fNamesDefault)
            textColorStruct.(fNamesDefault{k}) = 'k';
        end
        if defaultStruct.doLocal.value
            checkValue = 1;
        else
            checkValue = 0;
        end
        
        %% starts the while loop, sets up the uilist and creates the GUI
        closeOpenWindows(theTitle);
        geometry = {[1,4], 1, [3,1,3,1],[3,1,4]};
        geomvert = [];
        uilist={{'style', 'text', 'string', 'Global trend channels', ...
            'TooltipString', defaultStruct.globalTrendChannels.description}...
            {'style', 'edit', 'string', ...
            num2str(defaultStruct.globalTrendChannels.value), ...
            'tag', 'globalTrendChannels', 'ForegroundColor', ...
            textColorStruct.globalTrendChannels}...
            {'style', 'text', 'string', ''}...
            {'style', 'text', 'string', 'Do local', 'TooltipString', ...
            defaultStruct.doLocal.description}...
            {'style', 'checkbox', 'Value', checkValue, ...
            'tag', 'doLocal', 'ForegroundColor', textColorStruct.doLocal}...
            {'style', 'text', 'string', 'Local cutoff', ...
            'TooltipString', defaultStruct.localCutoff.description}...
            {'style', 'edit', 'string', ...
            num2str(defaultStruct.localCutoff.value), ...
            'tag', 'localCutoff', 'ForegroundColor', textColorStruct.localCutoff}...
            {'style', 'text', 'string', 'Local step size', ...
            'TooltipString', defaultStruct.localStepSize.description}...
            {'style', 'edit', 'string', ...
            num2str(defaultStruct.localStepSize.value), ...
            'tag', 'localStepSize', 'ForegroundColor', ...
            textColorStruct.localStepSize}...
            {'style', 'text', 'string', ''}};
        [~, ~, ~, paramsOut] = inputgui('geometry', geometry, ...
            'geomvert', geomvert, 'uilist', uilist, 'title', theTitle, ...
            'helpcom', 'pophelp(''pop_prepPipeline'')');
        if(isempty(paramsOut))
            break;
        end
        
        [paramsOut, typeErrors, fNamesErrors] = ...
            changeType(paramsOut, defaultStruct);
        %paramsOut.detrendType = typeMenuString(paramsOut.detrendType);
        mainFigure = findobj('Type', 'Figure', '-and', 'Name', inputData.name);
        userdata = get(mainFigure, 'UserData');
        userdata.globalTrend = paramsOut;
        set(mainFigure, 'UserData', userdata);
        if isempty(typeErrors)
            break;
        end
        textColorStruct = highlightErrors(fNamesErrors, ...
            fNamesDefault, textColorStruct);
        
        displayErrors(typeErrors); % Displays the errors and restarts GUI
    end
end