classdef Integral_Worth_Curve_GUI < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                   matlab.ui.Figure
        CalibrationEditFieldLabel  matlab.ui.control.Label
        CalibrationEditField       matlab.ui.control.EditField
        UIAxes                     matlab.ui.control.UIAxes
        DateDatePickerLabel        matlab.ui.control.Label
        DateDatePicker             matlab.ui.control.DatePicker
        UITable                    matlab.ui.control.Table
        PreviewButton              matlab.ui.control.Button
        IntegralWorthCurveGeneratorLabel  matlab.ui.control.Label
        ExportButton               matlab.ui.control.Button
    end

% stores the previewed data to be exported as a figure    
    properties (Access = private)
        D % Date
        R % Rod
        R_P % Rod Position
        R_W % Rod Worth
        I_R_W % Integral Rod Worth
        X_L %xlab
        end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            % Creating the table
            app.UITable.Data = {1,60.5,15.6; 2,65.2,9.43; 3,69.3,10.51; 4,74.6,12.61; 5,82.1,10.6; 6,90.4,9.33; 7,99.9,'--'}; % Creating the table with placeholder data
            % Set Editable
            ncols = size(app.UITable.Data,2);
            app.UITable.ColumnEditable = true(1,ncols); %all columns editable
            app.UITable.ColumnEditable(1) = false; %column1 non-editable
         
            % Locking the plot's axis
            disableDefaultInteractivity(app.UIAxes) 
            
        end

        % Cell edit callback: UITable
        function UITableCellEdit(app, event)

        end

        % Button pushed function: PreviewButton
        function PreviewButtonPushed(app, event)
       
% Collecting and the inputed data
rod = app.CalibrationEditField.Value;
app.R = rod;
date = app.DateDatePicker.Value;
app.D = date;
measured_rod_position = cell2mat(app.UITable.Data(1:7,2)).';
measured_rod_worth = cell2mat(app.UITable.Data(1:6,3)).';
          
% Mirrors rod positions back to 0% withdrawn
inverse_rod_position = (max(measured_rod_position) - flip(measured_rod_position));

% Combines measured and inversed rod positions into a single string
rod_position = horzcat(inverse_rod_position , measured_rod_position);
app.R_P = rod_position;

% Mirrors the reactivity data so it corresponds to the inveersed rod
% withdraw data
lower_rod_worth = flip(measured_rod_worth);

% Calculates the integral rod worth for the lower portion of the curve 
lower_integral_rod_worth = zeros(1,length(lower_rod_worth));

for n = 1 : length(lower_rod_worth)

   lower_integral_rod_worth(n+1) = lower_integral_rod_worth(n)+lower_rod_worth(n);
end

% Performs a linear fit on the last 2 points of the lower portion of the
% curve that is used to extapolate the behavior in the gap around 50%
% withdrawn
x = inverse_rod_position( length(inverse_rod_position)-1:length(inverse_rod_position) );
y = lower_integral_rod_worth( length(lower_integral_rod_worth)-1:length(lower_integral_rod_worth) );

extrap = polyfit(x ,y, 1);

% Calulates the worth of the gap  about 50%
gap_worth = extrap(1) * min(measured_rod_position) + extrap(2);

% Calculates the integral rod worth for the upper portion of the curve 
upper_integral_rod_worth = zeros(1,length(measured_rod_worth));

for n = 1 : length(lower_rod_worth)

   upper_integral_rod_worth(n+1) = upper_integral_rod_worth(n)+measured_rod_worth(n);

end

% Adds the integral rod worth of the upper portion to the worth of the gap
% about 50% to find the worth in the upper portion of the curve
upper_worth = upper_integral_rod_worth + gap_worth;

% Combines data for worth in upper and lower halves 
integral_rod_worth = horzcat(lower_integral_rod_worth , upper_worth);
app.I_R_W = integral_rod_worth;

rodworth=round(max(integral_rod_worth)/100,3,'significant');
app.R_W = rodworth;

% Plots integral rod worth curve 

plot(app.UIAxes, rod_position, integral_rod_worth,'LineWidth',2,'Marker','o','Color','b','MarkerFaceColor','b')

        end

        % Button pushed function: ExportButton
        function ExportButtonPushed(app, event)
      % Creates the finalized plot including notes of:
      % Rod calibrated, total rod worth, calibration date
      % and line for signature
            figure;
            plot(app.R_P, app.I_R_W,'LineWidth',2,'Marker','o','Color','b','MarkerFaceColor','b')
            title (app.R, 'FontSize', 24);
            lab={'Rod Withdraw %','',['Total Rod Worth: $' num2str(app.R_W)  '      ' datestr(app.D) '     Approved:___________________'] } ;
            xlabel (lab, 'FontSize', 18)
            ylabel ('Reactivity in Cents', 'FontSize', 18) 
            grid on
            grid minor
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.AutoResizeChildren = 'off';
            app.UIFigure.Position = [250 200 1000 850];
            app.UIFigure.Name = 'UI Figure';
            app.UIFigure.Resize = 'off';

            % Create CalibrationEditFieldLabel
            app.CalibrationEditFieldLabel = uilabel(app.UIFigure);
            app.CalibrationEditFieldLabel.HorizontalAlignment = 'right';
            app.CalibrationEditFieldLabel.FontSize = 18;
            app.CalibrationEditFieldLabel.Position = [38 711 92 22];
            app.CalibrationEditFieldLabel.Text = 'Calibration';

            % Create CalibrationEditField
            app.CalibrationEditField = uieditfield(app.UIFigure, 'text');
            app.CalibrationEditField.FontSize = 18;
            app.CalibrationEditField.Position = [145 707 100 26];

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Rod Worth Curve')
            xlabel(app.UIAxes, 'Rod Withdrawl %')
            ylabel(app.UIAxes, 'Reactivity in Cents')
            app.UIAxes.PlotBoxAspectRatio = [2.08932038834951 1 1];
            app.UIAxes.FontSize = 18;
            app.UIAxes.XGrid = 'on';
            app.UIAxes.YGrid = 'on';
            app.UIAxes.Position = [93 54 816 515];

            % Create DateDatePickerLabel
            app.DateDatePickerLabel = uilabel(app.UIFigure);
            app.DateDatePickerLabel.HorizontalAlignment = 'right';
            app.DateDatePickerLabel.FontSize = 18;
            app.DateDatePickerLabel.Position = [38 777 44 22];
            app.DateDatePickerLabel.Text = 'Date';

            % Create DateDatePicker
            app.DateDatePicker = uidatepicker(app.UIFigure);
            app.DateDatePicker.FontSize = 18;
            app.DateDatePicker.Position = [97 776 150 23];

            % Create UITable
            app.UITable = uitable(app.UIFigure);
            app.UITable.ColumnName = {'Measurment'; 'Measured Rod Positoin'; 'Measured Rod Worth'};
            app.UITable.ColumnWidth = {78, 140, 150};
            app.UITable.RowName = {};
            app.UITable.CellEditCallback = createCallbackFcn(app, @UITableCellEdit, true);
            app.UITable.FontSize = 18;
            app.UITable.Position = [348 585 370 214];

            % Create PreviewButton
            app.PreviewButton = uibutton(app.UIFigure, 'push');
            app.PreviewButton.ButtonPushedFcn = createCallbackFcn(app, @PreviewButtonPushed, true);
            app.PreviewButton.FontSize = 16;
            app.PreviewButton.Position = [809 732 100 26];
            app.PreviewButton.Text = 'Preview';

            % Create IntegralWorthCurveGeneratorLabel
            app.IntegralWorthCurveGeneratorLabel = uilabel(app.UIFigure);
            app.IntegralWorthCurveGeneratorLabel.FontSize = 22;
            app.IntegralWorthCurveGeneratorLabel.FontWeight = 'bold';
            app.IntegralWorthCurveGeneratorLabel.Position = [379 812 339 27];
            app.IntegralWorthCurveGeneratorLabel.Text = 'Integral Worth Curve Generator';

            % Create ExportButton
            app.ExportButton = uibutton(app.UIFigure, 'push');
            app.ExportButton.ButtonPushedFcn = createCallbackFcn(app, @ExportButtonPushed, true);
            app.ExportButton.FontSize = 16;
            app.ExportButton.Position = [809 660 100 26];
            app.ExportButton.Text = 'Export';

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

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = Integral_Worth_Curve_GUI

            % Create UIFigure and components
            createComponents(app)

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

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

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

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