classdef tron < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        TranscodingRemuxingOperationsNucleusTRONLabel  matlab.ui.control.Label
        GeneratecommandsforqualitycontrolhereLabel  matlab.ui.control.Label
        ShareddirectorypathnameTextArea  matlab.ui.control.TextArea
        ShareddirectorypathnameTextAreaLabel  matlab.ui.control.Label
        OutputfilenameTextArea         matlab.ui.control.TextArea
        OutputfilenameTextAreaLabel    matlab.ui.control.Label
        InputfilenameTextArea          matlab.ui.control.TextArea
        InputfilenameTextAreaLabel     matlab.ui.control.Label
        TextArea                       matlab.ui.control.TextArea
        GeneratecommandsButton         matlab.ui.control.Button
        TypedesiredoutputdirectoryintheeditfieldLabel  matlab.ui.control.Label
        OutputdirectoryEditField       matlab.ui.control.EditField
        OutputdirectoryEditFieldLabel  matlab.ui.control.Label
        InputButton                    matlab.ui.control.Button
    end

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

        % Button pushed function: InputButton
        function InputButtonPushed(app, event)
            [file, path] = uigetfile('*.mxf;*.mkv;*.avi', 'Select Video File');
            if isequal(file, 0)
                disp('User selected cancel.');
            else
                inputfile = fullfile(path,file);
                outputfile = (app.OutputdirectoryEditField.Value + "output.mp4");
                ffmpegCommand = sprintf('ffmpeg -i %s -map 0 -c:a copy %s', inputfile, outputfile);

                % Execute the FFmpeg command 
                status = system(ffmpegCommand); 
                % Check the status of the conversion process 
                if status == 0 
                    disp('Conversion completed. Look for "output.mp4" in the output directory you specified.'); 
                else 
                    disp('Conversion failed.');
                end
end 
        end

        % Button pushed function: GeneratecommandsButton
        function GeneratecommandsButtonPushed(app, event)
           str0 = string(app.ShareddirectorypathnameTextArea.Value);
           str1 = string(app.InputfilenameTextArea.Value);
           str2 = string(app.OutputfilenameTextArea.Value);
           app.TextArea.Value = sprintf(append('cd ', str0, '\n', '\n', 'ffplay -f lavfi "movie=', str1, '[org]; movie=', str2, '[enc]; [org][enc]blend=all_mode=difference"'));
        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.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';

            % Create InputButton
            app.InputButton = uibutton(app.UIFigure, 'push');
            app.InputButton.ButtonPushedFcn = createCallbackFcn(app, @InputButtonPushed, true);
            app.InputButton.Position = [225 262 100 23];
            app.InputButton.Text = 'Input';

            % Create OutputdirectoryEditFieldLabel
            app.OutputdirectoryEditFieldLabel = uilabel(app.UIFigure);
            app.OutputdirectoryEditFieldLabel.HorizontalAlignment = 'right';
            app.OutputdirectoryEditFieldLabel.Position = [91 302 93 22];
            app.OutputdirectoryEditFieldLabel.Text = 'Output directory';

            % Create OutputdirectoryEditField
            app.OutputdirectoryEditField = uieditfield(app.UIFigure, 'text');
            app.OutputdirectoryEditField.Position = [199 302 151 22];

            % Create TypedesiredoutputdirectoryintheeditfieldLabel
            app.TypedesiredoutputdirectoryintheeditfieldLabel = uilabel(app.UIFigure);
            app.TypedesiredoutputdirectoryintheeditfieldLabel.Position = [22 338 393 72];
            app.TypedesiredoutputdirectoryintheeditfieldLabel.Text = {'Convert MXF, AVI, or MKV to MP4:'; '1. Type desired output directory for MP4 file in the edit field.'; '2. Click the "Input" button and select the MXF file you want to convert.'; '3. Check the MatLab command window for success/failure notification.'};

            % Create GeneratecommandsButton
            app.GeneratecommandsButton = uibutton(app.UIFigure, 'push');
            app.GeneratecommandsButton.ButtonPushedFcn = createCallbackFcn(app, @GeneratecommandsButtonPushed, true);
            app.GeneratecommandsButton.Position = [211 24 128 23];
            app.GeneratecommandsButton.Text = 'Generate commands';

            % Create TextArea
            app.TextArea = uitextarea(app.UIFigure);
            app.TextArea.Position = [402 24 201 146];

            % Create InputfilenameTextAreaLabel
            app.InputfilenameTextAreaLabel = uilabel(app.UIFigure);
            app.InputfilenameTextAreaLabel.HorizontalAlignment = 'right';
            app.InputfilenameTextAreaLabel.Position = [98 104 84 22];
            app.InputfilenameTextAreaLabel.Text = 'Input file name';

            % Create InputfilenameTextArea
            app.InputfilenameTextArea = uitextarea(app.UIFigure);
            app.InputfilenameTextArea.Position = [197 103 156 25];

            % Create OutputfilenameTextAreaLabel
            app.OutputfilenameTextAreaLabel = uilabel(app.UIFigure);
            app.OutputfilenameTextAreaLabel.HorizontalAlignment = 'right';
            app.OutputfilenameTextAreaLabel.Position = [88 62 94 22];
            app.OutputfilenameTextAreaLabel.Text = 'Output file name';

            % Create OutputfilenameTextArea
            app.OutputfilenameTextArea = uitextarea(app.UIFigure);
            app.OutputfilenameTextArea.Position = [197 61 156 25];

            % Create ShareddirectorypathnameTextAreaLabel
            app.ShareddirectorypathnameTextAreaLabel = uilabel(app.UIFigure);
            app.ShareddirectorypathnameTextAreaLabel.HorizontalAlignment = 'right';
            app.ShareddirectorypathnameTextAreaLabel.Position = [31 146 151 22];
            app.ShareddirectorypathnameTextAreaLabel.Text = 'Shared directory pathname';

            % Create ShareddirectorypathnameTextArea
            app.ShareddirectorypathnameTextArea = uitextarea(app.UIFigure);
            app.ShareddirectorypathnameTextArea.Position = [197 145 156 25];

            % Create GeneratecommandsforqualitycontrolhereLabel
            app.GeneratecommandsforqualitycontrolhereLabel = uilabel(app.UIFigure);
            app.GeneratecommandsforqualitycontrolhereLabel.Position = [22 189 340 33];
            app.GeneratecommandsforqualitycontrolhereLabel.Text = 'Generate commands for quality control here:';

            % Create TranscodingRemuxingOperationsNucleusTRONLabel
            app.TranscodingRemuxingOperationsNucleusTRONLabel = uilabel(app.UIFigure);
            app.TranscodingRemuxingOperationsNucleusTRONLabel.Position = [22 441 580 32];
            app.TranscodingRemuxingOperationsNucleusTRONLabel.Text = 'Transcoding & Remuxing Operations Nucleus (TRON)';

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

            % Create UIFigure and components
            createComponents(app)

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

            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