classdef aplikacja < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                matlab.ui.Figure
        FileMenu                matlab.ui.container.Menu
        ExitMenu                matlab.ui.container.Menu
        OptionsMenu             matlab.ui.container.Menu
        DarkModeMenu            matlab.ui.container.Menu
        GridLayout              matlab.ui.container.GridLayout
        Switch                  matlab.ui.control.RockerSwitch
        SwitchLabel             matlab.ui.control.Label
        BazadanychListBox       matlab.ui.control.ListBox
        BazadanychListBoxLabel  matlab.ui.control.Label
        WzrostEditField         matlab.ui.control.EditField
        WzrostEditFieldLabel    matlab.ui.control.Label
        WiekEditField           matlab.ui.control.EditField
        WiekEditFieldLabel      matlab.ui.control.Label
        NazwiskoEditField       matlab.ui.control.EditField
        NazwiskoEditFieldLabel  matlab.ui.control.Label
        ImieEditField           matlab.ui.control.EditField
        ImieEditFieldLabel      matlab.ui.control.Label
        TestButton              matlab.ui.control.Button
        SaveButton              matlab.ui.control.Button
        ExitButton              matlab.ui.control.Button
        ClearButton             matlab.ui.control.Button
        PlotButton              matlab.ui.control.Button
        UIAxes                  matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        X=1:6;
        Dane=[1,2,5,-3,4,3]; % Description
        Wiek
        Wzrost
        Imie
        Nazwisko
        DarkMode=false % Description
    end
    
    methods (Access = private)

        
        function chacking_edit_field(app)
            if isempty(app.ImieEditField.Value) || isempty(app.NazwiskoEditField.Value) ...
                    || isempty(app.WiekEditField.Value) ...
                    || isempty(app.WzrostEditField.Value) % || OR (LUB)
                app.SaveButton.Enable="off";
            else
                app.SaveButton.Enable="on";
            end
        end
        
        function index=findIndex(app)
            Baza=app.BazadanychListBox.Items;
            Wybrana=app.BazadanychListBox.Value;
            index=find(strcmp(Baza,Wybrana)==1);
        end
        
        function aktualizacja_listy(app)
            app.BazadanychListBox.Items=app.Imie+" "+app.Nazwisko;
        end
        
        function  switchDarkMode(app)
            if ~app.DarkMode % gdy dark mode wyłączony to przełączamy na dark mode
                app.GridLayout.BackgroundColor=[0.5 0.5 0.5];
                app.UIAxes.Color=[0.8 0.8 0.8];
                app.DarkMode=true;
                app.Switch.Value="On";
            else % gdy dark mode jest włączony to przełączamy na zwykły tryb
                app.GridLayout.BackgroundColor=[0.94,0.94,0.94];
                app.UIAxes.Color=[1 1 1];
                app.DarkMode=false;
                app.Switch.Value="Off";
            end
        end
    end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            T=readtable("Moja_baza.txt");
            T.Properties.VariableNames
            app.Wiek=T.Wiek;
            app.Wzrost=T.Wzrost;   
            app.Imie=T.Imie;
            app.Nazwisko=T.Nazwisko;
            app.BazadanychListBox.Items=T.Imie+" "+T.Nazwisko;
        end

        % Button pushed function: PlotButton
        function PlotButtonPushed(app, event)
            
            Y=app.Wiek;
            plot(app.UIAxes,Y)
            n=length(app.Wiek);
            app.UIAxes.XTick=1:n;
            app.UIAxes.XTickLabel=[app.Imie+" "+app.Nazwisko];
            app.UIAxes.XTickLabelRotation=30; % obrócenie podpisu XTick o 90 stopni
            app.UIAxes.YLabel.String="Wiek [lata]";

        end

        % Button pushed function: ClearButton
        function ClearButtonPushed(app, event)
            cla(app.UIAxes) % czyszczenie wykresów na osiach
        end

        % Button pushed function: ExitButton
        function ExitButtonPushed(app, event)
            
            close(app.UIFigure)
        end

        % Button pushed function: SaveButton
        function SaveButtonPushed(app, event)
            imie=app.ImieEditField.Value;
            nazwisko=app.NazwiskoEditField.Value;
            wiek=str2double(app.WiekEditField.Value);
            wzrost=str2double(app.WzrostEditField.Value);
            
            ind=findIndex(app);
            app.Imie{ind}=imie;
            app.Nazwisko{ind}=nazwisko;
            app.Wiek(ind)=wiek;
            app.Wzrost(ind)=wzrost;

            aktualizacja_listy(app);
            %
            %app.Imie=[app.Imie;imie];
            %app.Nazwisko=[app.Nazwisko;nazwisko];
            %app.Wiek=[app.Wiek;wiek];
            %app.Wzrost=[app.Wzrost;wzrost];

            %app.ImieEditField.Value="";
            %app.NazwiskoEditField.Value="";
            %app.WiekEditField.Value="";
            %app.WzrostEditField.Value="";
            
        end

        % Value changed function: ImieEditField
        function ImieEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Value changed function: NazwiskoEditField
        function NazwiskoEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Value changed function: WiekEditField
        function WiekEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Value changed function: WzrostEditField
        function WzrostEditFieldValueChanged(app, event)
            chacking_edit_field(app);
        end

        % Button pushed function: TestButton
        function TestButtonPushed(app, event)
            clc
            app.Imie
            app.Nazwisko
            app.Wiek
            app.Wzrost
        end

        % Value changed function: BazadanychListBox
        function BazadanychListBoxValueChanged(app, event)
            
            
            value_index= findIndex(app);
            app.ImieEditField.Value=app.Imie{value_index};
            app.NazwiskoEditField.Value=app.Nazwisko{value_index};
            app.WiekEditField.Value=num2str(app.Wiek(value_index));
            app.WzrostEditField.Value=num2str(app.Wzrost(value_index));
        end

        % Menu selected function: ExitMenu
        function ExitMenuSelected(app, event)
            close(app.UIFigure);
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            %% Funkcja realizuje się po zamknięciu okna
            Imie=app.Imie;
            Nazwisko=app.Nazwisko;
            Wiek=app.Wiek;
            Wzrost=app.Wzrost;
            
            T=table(Imie,Nazwisko,Wiek,Wzrost);
            writetable(T,"Moja_baza.txt")
            
            delete(app)
            
        end

        % Menu selected function: DarkModeMenu
        function DarkModeMenuSelected(app, event)
            switchDarkMode(app)
            
           
        end

        % Value changed function: Switch
        function SwitchValueChanged(app, event)
            switchDarkMode(app);
            
        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.Color = [0.502 0.502 0.502];
            app.UIFigure.Position = [100 100 920 578];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create FileMenu
            app.FileMenu = uimenu(app.UIFigure);
            app.FileMenu.Text = 'File';

            % Create ExitMenu
            app.ExitMenu = uimenu(app.FileMenu);
            app.ExitMenu.MenuSelectedFcn = createCallbackFcn(app, @ExitMenuSelected, true);
            app.ExitMenu.Text = 'Exit';

            % Create OptionsMenu
            app.OptionsMenu = uimenu(app.UIFigure);
            app.OptionsMenu.Text = 'Options';

            % Create DarkModeMenu
            app.DarkModeMenu = uimenu(app.OptionsMenu);
            app.DarkModeMenu.MenuSelectedFcn = createCallbackFcn(app, @DarkModeMenuSelected, true);
            app.DarkModeMenu.Text = 'Dark Mode';

            % Create GridLayout
            app.GridLayout = uigridlayout(app.UIFigure);
            app.GridLayout.ColumnWidth = {31, 26, 29, 71, '1x', '1.76x', 100, 100, 124};
            app.GridLayout.RowHeight = {'1.53x', 22, 22, 22, 22, '1x', 23, '2.06x', 22, '3.06x', '9.47x', 23};
            app.GridLayout.RowSpacing = 10.0769230769231;
            app.GridLayout.Padding = [10 10.0769230769231 10 10.0769230769231];

            % Create UIAxes
            app.UIAxes = uiaxes(app.GridLayout);
            app.UIAxes.Layout.Row = [1 11];
            app.UIAxes.Layout.Column = [5 9];

            % Create PlotButton
            app.PlotButton = uibutton(app.GridLayout, 'push');
            app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
            app.PlotButton.Layout.Row = 12;
            app.PlotButton.Layout.Column = 7;
            app.PlotButton.Text = 'Plot';

            % Create ClearButton
            app.ClearButton = uibutton(app.GridLayout, 'push');
            app.ClearButton.ButtonPushedFcn = createCallbackFcn(app, @ClearButtonPushed, true);
            app.ClearButton.Layout.Row = 12;
            app.ClearButton.Layout.Column = 8;
            app.ClearButton.Text = 'Clear';

            % Create ExitButton
            app.ExitButton = uibutton(app.GridLayout, 'push');
            app.ExitButton.ButtonPushedFcn = createCallbackFcn(app, @ExitButtonPushed, true);
            app.ExitButton.Layout.Row = 12;
            app.ExitButton.Layout.Column = 9;
            app.ExitButton.Text = 'Exit';

            % Create SaveButton
            app.SaveButton = uibutton(app.GridLayout, 'push');
            app.SaveButton.ButtonPushedFcn = createCallbackFcn(app, @SaveButtonPushed, true);
            app.SaveButton.Enable = 'off';
            app.SaveButton.Layout.Row = 7;
            app.SaveButton.Layout.Column = [3 4];
            app.SaveButton.Text = 'Save';

            % Create TestButton
            app.TestButton = uibutton(app.GridLayout, 'push');
            app.TestButton.ButtonPushedFcn = createCallbackFcn(app, @TestButtonPushed, true);
            app.TestButton.Layout.Row = 12;
            app.TestButton.Layout.Column = [1 3];
            app.TestButton.Text = 'Test';

            % Create ImieEditFieldLabel
            app.ImieEditFieldLabel = uilabel(app.GridLayout);
            app.ImieEditFieldLabel.HorizontalAlignment = 'right';
            app.ImieEditFieldLabel.Layout.Row = 2;
            app.ImieEditFieldLabel.Layout.Column = 1;
            app.ImieEditFieldLabel.Text = 'Imie';

            % Create ImieEditField
            app.ImieEditField = uieditfield(app.GridLayout, 'text');
            app.ImieEditField.ValueChangedFcn = createCallbackFcn(app, @ImieEditFieldValueChanged, true);
            app.ImieEditField.Layout.Row = 2;
            app.ImieEditField.Layout.Column = [3 4];

            % Create NazwiskoEditFieldLabel
            app.NazwiskoEditFieldLabel = uilabel(app.GridLayout);
            app.NazwiskoEditFieldLabel.HorizontalAlignment = 'right';
            app.NazwiskoEditFieldLabel.Layout.Row = 3;
            app.NazwiskoEditFieldLabel.Layout.Column = [1 2];
            app.NazwiskoEditFieldLabel.Text = 'Nazwisko';

            % Create NazwiskoEditField
            app.NazwiskoEditField = uieditfield(app.GridLayout, 'text');
            app.NazwiskoEditField.ValueChangedFcn = createCallbackFcn(app, @NazwiskoEditFieldValueChanged, true);
            app.NazwiskoEditField.Layout.Row = 3;
            app.NazwiskoEditField.Layout.Column = [3 4];

            % Create WiekEditFieldLabel
            app.WiekEditFieldLabel = uilabel(app.GridLayout);
            app.WiekEditFieldLabel.HorizontalAlignment = 'right';
            app.WiekEditFieldLabel.Layout.Row = 4;
            app.WiekEditFieldLabel.Layout.Column = 1;
            app.WiekEditFieldLabel.Text = 'Wiek';

            % Create WiekEditField
            app.WiekEditField = uieditfield(app.GridLayout, 'text');
            app.WiekEditField.ValueChangedFcn = createCallbackFcn(app, @WiekEditFieldValueChanged, true);
            app.WiekEditField.Layout.Row = 4;
            app.WiekEditField.Layout.Column = [3 4];

            % Create WzrostEditFieldLabel
            app.WzrostEditFieldLabel = uilabel(app.GridLayout);
            app.WzrostEditFieldLabel.HorizontalAlignment = 'right';
            app.WzrostEditFieldLabel.Layout.Row = 5;
            app.WzrostEditFieldLabel.Layout.Column = [1 2];
            app.WzrostEditFieldLabel.Text = 'Wzrost';

            % Create WzrostEditField
            app.WzrostEditField = uieditfield(app.GridLayout, 'text');
            app.WzrostEditField.ValueChangedFcn = createCallbackFcn(app, @WzrostEditFieldValueChanged, true);
            app.WzrostEditField.Layout.Row = 5;
            app.WzrostEditField.Layout.Column = [3 4];

            % Create BazadanychListBoxLabel
            app.BazadanychListBoxLabel = uilabel(app.GridLayout);
            app.BazadanychListBoxLabel.HorizontalAlignment = 'right';
            app.BazadanychListBoxLabel.Layout.Row = 8;
            app.BazadanychListBoxLabel.Layout.Column = [1 2];
            app.BazadanychListBoxLabel.Text = 'Baza danych';

            % Create BazadanychListBox
            app.BazadanychListBox = uilistbox(app.GridLayout);
            app.BazadanychListBox.ValueChangedFcn = createCallbackFcn(app, @BazadanychListBoxValueChanged, true);
            app.BazadanychListBox.Layout.Row = [8 11];
            app.BazadanychListBox.Layout.Column = [3 4];

            % Create SwitchLabel
            app.SwitchLabel = uilabel(app.GridLayout);
            app.SwitchLabel.HorizontalAlignment = 'center';
            app.SwitchLabel.Layout.Row = 12;
            app.SwitchLabel.Layout.Column = 1;
            app.SwitchLabel.Text = 'Switch';

            % Create Switch
            app.Switch = uiswitch(app.GridLayout, 'rocker');
            app.Switch.ValueChangedFcn = createCallbackFcn(app, @SwitchValueChanged, true);
            app.Switch.Layout.Row = 11;
            app.Switch.Layout.Column = 1;

            % 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 = aplikacja

            % 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