classdef setDefaultParamValues < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        setparametervalues              matlab.ui.Figure
        ChangedefaultparametervaluesPanel  matlab.ui.container.Panel
        PlateradiusinPixelsLabel        matlab.ui.control.Label
        PlateradiusinPixelsEditField    matlab.ui.control.NumericEditField
        BoundaryWidthinPixelsEditFieldLabel  matlab.ui.control.Label
        BoundaryWidthinPixelsEditField  matlab.ui.control.NumericEditField
        AcceptButton                    matlab.ui.control.Button
        MaxareainPixelsEditFieldLabel   matlab.ui.control.Label
        MaxareainPixelsEditField        matlab.ui.control.NumericEditField
        RedColonysizemultiplierEditFieldLabel  matlab.ui.control.Label
        RedColonysizemultiplierEditField  matlab.ui.control.NumericEditField
    end

    
    properties (Access = private)
        CallingApp
        plate_radius %radius of a plate in pixels
        boundary_width %Ignore 50 pixel wide region on plate boundary
        colonySizeMax
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app, mainapp, defaults)
            app.CallingApp = mainapp;
        end

        % Value changed function: BoundaryWidthinPixelsEditField
        function BoundaryWidthinPixelsEditFieldValueChanged(app, event)

            app.CallingApp.boundary_width = (app.BoundaryWidthinPixelsEditField.Value);
        end

        % Close request function: setparametervalues
        function setparametervaluesCloseRequest(app, event)
            app.CallingApp.PreferencesButton.Enable = 'on';
            delete(app)
        end

        % Button pushed function: AcceptButton
        function AcceptButtonPushed(app, event)
            app.CallingApp.PreferencesButton.Enable = 'on';
            figure(app.CallingApp.plateImagefigure);
            imshow(app.CallingApp.masked_im);
            
            [logicalimages,  imageproperties, app.CallingApp.colonySizeAvg] = findandDisplay(app.CallingApp, app.CallingApp.L, ...
                app.CallingApp.RG, app.CallingApp.suppress_green,...
                app.CallingApp.colonySizeMax, app.CallingApp.sizeMultiplier, app.CallingApp.plateImagefigure);
            app.CallingApp.redColonies = logicalimages(:,:,1);
            app.CallingApp.allColonies = logicalimages(:,:,2);
            app.CallingApp.redprops = imageproperties{1};
            app.CallingApp.allprops = imageproperties{2};
            
            index = find(ismember(app.CallingApp.FilesListBox.Items, app.CallingApp.FilesListBox.Value));
            app.CallingApp.compiledData(index).fullfilename = app.CallingApp.FilesListBox.Value;
            app.CallingApp.compiledimData(index).redColonies = logicalimages(:,:,1);
            app.CallingApp.compiledimData(index).allColonies = logicalimages(:,:,2);
            app.CallingApp.compiledimData(index).redprops = imageproperties{1};
            app.CallingApp.compiledimData(index).allprops = imageproperties{2};
            app.CallingApp.compiledData(index).numredColonies = height(app.CallingApp.compiledimData(index).redprops);
            app.CallingApp.compiledData(index).numallColonies = height(app.CallingApp.compiledimData(index).allprops);
            app.CallingApp.compiledData(index).suppress_green = app.CallingApp.suppress_green;
            app.CallingApp.RedColoniesTextArea.Value = num2str(app.CallingApp.compiledData(index).numredColonies);
            app.CallingApp.TotalColoniesTextArea.Value = num2str(app.CallingApp.compiledData(index).numallColonies);
            delete(app)
        end

        % Value changed function: MaxareainPixelsEditField
        function MaxareainPixelsEditFieldValueChanged(app, event)
            app.CallingApp.colonySizeMax = app.MaxareainPixelsEditField.Value;
            
        end

        % Value changed function: RedColonysizemultiplierEditField
        function RedColonysizemultiplierEditFieldValueChanged(app, event)
            app.CallingApp.sizeMultiplier = app.RedColonysizemultiplierEditField.Value;
            
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create setparametervalues and hide until all components are created
            app.setparametervalues = uifigure('Visible', 'off');
            app.setparametervalues.Position = [100 100 497 189];
            app.setparametervalues.Name = 'UI Figure';
            app.setparametervalues.CloseRequestFcn = createCallbackFcn(app, @setparametervaluesCloseRequest, true);

            % Create ChangedefaultparametervaluesPanel
            app.ChangedefaultparametervaluesPanel = uipanel(app.setparametervalues);
            app.ChangedefaultparametervaluesPanel.Title = 'Change default parameter values';
            app.ChangedefaultparametervaluesPanel.Position = [1 2 499 188];

            % Create PlateradiusinPixelsLabel
            app.PlateradiusinPixelsLabel = uilabel(app.ChangedefaultparametervaluesPanel);
            app.PlateradiusinPixelsLabel.HorizontalAlignment = 'center';
            app.PlateradiusinPixelsLabel.Position = [20 126 72 28];
            app.PlateradiusinPixelsLabel.Text = {'Plate radius '; 'in Pixels'};

            % Create PlateradiusinPixelsEditField
            app.PlateradiusinPixelsEditField = uieditfield(app.ChangedefaultparametervaluesPanel, 'numeric');
            app.PlateradiusinPixelsEditField.Limits = [500 2000];
            app.PlateradiusinPixelsEditField.RoundFractionalValues = 'on';
            app.PlateradiusinPixelsEditField.HorizontalAlignment = 'center';
            app.PlateradiusinPixelsEditField.Position = [120 132 79 22];
            app.PlateradiusinPixelsEditField.Value = 1500;

            % Create BoundaryWidthinPixelsEditFieldLabel
            app.BoundaryWidthinPixelsEditFieldLabel = uilabel(app.ChangedefaultparametervaluesPanel);
            app.BoundaryWidthinPixelsEditFieldLabel.HorizontalAlignment = 'center';
            app.BoundaryWidthinPixelsEditFieldLabel.Position = [10 78 92 28];
            app.BoundaryWidthinPixelsEditFieldLabel.Text = {'Boundary Width'; 'in Pixels'};

            % Create BoundaryWidthinPixelsEditField
            app.BoundaryWidthinPixelsEditField = uieditfield(app.ChangedefaultparametervaluesPanel, 'numeric');
            app.BoundaryWidthinPixelsEditField.Limits = [0 400];
            app.BoundaryWidthinPixelsEditField.ValueChangedFcn = createCallbackFcn(app, @BoundaryWidthinPixelsEditFieldValueChanged, true);
            app.BoundaryWidthinPixelsEditField.HorizontalAlignment = 'center';
            app.BoundaryWidthinPixelsEditField.Position = [120 84 79 22];
            app.BoundaryWidthinPixelsEditField.Value = 10;

            % Create AcceptButton
            app.AcceptButton = uibutton(app.ChangedefaultparametervaluesPanel, 'push');
            app.AcceptButton.ButtonPushedFcn = createCallbackFcn(app, @AcceptButtonPushed, true);
            app.AcceptButton.Position = [201 15 100 22];
            app.AcceptButton.Text = 'Accept';

            % Create MaxareainPixelsEditFieldLabel
            app.MaxareainPixelsEditFieldLabel = uilabel(app.ChangedefaultparametervaluesPanel);
            app.MaxareainPixelsEditFieldLabel.HorizontalAlignment = 'center';
            app.MaxareainPixelsEditFieldLabel.Position = [278 126 59 28];
            app.MaxareainPixelsEditFieldLabel.Text = {'Max. area'; 'in Pixels'};

            % Create MaxareainPixelsEditField
            app.MaxareainPixelsEditField = uieditfield(app.ChangedefaultparametervaluesPanel, 'numeric');
            app.MaxareainPixelsEditField.Limits = [2000 15000];
            app.MaxareainPixelsEditField.ValueChangedFcn = createCallbackFcn(app, @MaxareainPixelsEditFieldValueChanged, true);
            app.MaxareainPixelsEditField.HorizontalAlignment = 'center';
            app.MaxareainPixelsEditField.Position = [371 132 100 22];
            app.MaxareainPixelsEditField.Value = 10000;

            % Create RedColonysizemultiplierEditFieldLabel
            app.RedColonysizemultiplierEditFieldLabel = uilabel(app.ChangedefaultparametervaluesPanel);
            app.RedColonysizemultiplierEditFieldLabel.HorizontalAlignment = 'center';
            app.RedColonysizemultiplierEditFieldLabel.Position = [262 75 93 28];
            app.RedColonysizemultiplierEditFieldLabel.Text = {'Red Colony size'; 'multiplier'};

            % Create RedColonysizemultiplierEditField
            app.RedColonysizemultiplierEditField = uieditfield(app.ChangedefaultparametervaluesPanel, 'numeric');
            app.RedColonysizemultiplierEditField.Limits = [0 1];
            app.RedColonysizemultiplierEditField.ValueChangedFcn = createCallbackFcn(app, @RedColonysizemultiplierEditFieldValueChanged, true);
            app.RedColonysizemultiplierEditField.HorizontalAlignment = 'center';
            app.RedColonysizemultiplierEditField.Position = [371 81 100 22];
            app.RedColonysizemultiplierEditField.Value = 0.4;

            % Show the figure after all components are created
            app.setparametervalues.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = setDefaultParamValues(varargin)

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.setparametervalues)

            % Execute the startup function
            runStartupFcn(app, @(app)startupFcn(app, varargin{:}))

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.setparametervalues)
        end
    end
end