classdef ValidateF281x < matlab.hwmgr.internal.hwsetup.ValidateLocation
    % ValidateF281x - Screen implementation to enable users to select the
    % F281x driver files.
    
    %   Copyright 2022 The MathWorks, Inc.
    
    properties
        ValidateButton
        StatusTable
        screenReady = false;
    end
    
    properties(Access = private)
        BusySpinner
    end
    
    methods
        function obj = ValidateF281x(varargin)
            % call to the base class constructor
            obj@matlab.hwmgr.internal.hwsetup.ValidateLocation(varargin{:});
            obj.ValidateButton = matlab.hwmgr.internal.hwsetup.Button.getInstance(obj.ContentPanel);
            obj.StatusTable = matlab.hwmgr.internal.hwsetup.StatusTable.getInstance(obj.ContentPanel);
            % Status table properties
            obj.StatusTable.Status = {''};
            obj.StatusTable.Steps = {''};
            obj.StatusTable.Visible = 'off';
            
            % Set the Title Text
            obj.Title.Text = message('TIC2000:hwsetup:ValidateF281xTitle').getString;
            
            % Set the Description text
            obj.Description.Text = message('TIC2000:hwsetup:ValidateF281xDescription').getString;
            
            
            % Update Installed locations of CCS and CGT
            % Add the path from the stored workflow variable
            
            if isempty(obj.Workflow.F281xLocation)
                [obj.Workflow.F281xLocation, obj.Workflow.F281xVersion] = obj.Workflow.HardwareInterface.getF281xDetails();
            end
            
            % Hardcode some default value
            if isempty(obj.Workflow.F281xLocation)
                obj.ValidateEditText.Text = obj.Workflow.HardwareInterface.dummyF281xPath;
            else
                obj.ValidateEditText.Text = obj.Workflow.F281xLocation;
            end
            
            
            obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:SpecifyValidFolder').getString;
            
            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';
            
            % Validate button properties
            obj.ValidateButton.Text = message('TIC2000:hwsetup:Validate').getString;
            obj.ValidateButton.ButtonPushedFcn = @obj.validateF281x;
            obj.ValidateButton.Position = [20 270 143 22];
            obj.ValidateButton.Color = matlab.hwmgr.internal.hwsetup.util.Color.MWBLUE;
            obj.ValidateButton.FontColor = matlab.hwmgr.internal.hwsetup.util.Color.WHITE;
            obj.ValidateEditText.ValueChangedFcn = @obj.FolderValidation;
            
            % browse button callback
            obj.BrowseButton.ButtonPushedFcn = @obj.custombrowseDirectory;
            obj.BrowseButton.Color = matlab.hwmgr.internal.hwsetup.util.Color.MWBLUE;
            obj.BrowseButton.FontColor = matlab.hwmgr.internal.hwsetup.util.Color.WHITE;
            % Do not place any code below this line in this function
            obj.screenReady = true;
        end
        
        function custombrowseDirectory(obj, ~, ~)
            dir = uigetdir(obj.ValidateEditText.Text, message('TIC2000:hwsetup:F281xBrowseHelp').getString);
            obj.Workflow.Window.bringToFront();
            if dir % If the user cancel's the file browser, uigetdir returns 0.
                % When a new location is selected, then set that location value
                % back to show it in edit text area. (ValidateEditText.Text).
                obj.ValidateEditText.Text = dir;
            end
        end
        
        function FolderValidation(obj, ~, ~)
            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';
        end
        
        function out = getNextScreenID(obj)
            if ~isobject(obj) || isempty(obj.screenReady) || ~(obj.screenReady)
                % Placeholder if user clicks next quickly before the screen is
                % populated with options stay in the same screen
                out = [];
            else
                out = 'codertarget.tic2000.hwsetup.ValidateCCS';
                if isempty(obj.Workflow.CCSInstallLocation)
                    % Check registry to pick installed CCSInstallLocation during setup
                    obj.disableScreen();
                    obj.BusySpinner = matlab.hwmgr.internal.hwsetup.BusyOverlay.getInstance(obj.ContentPanel);
                    obj.BusySpinner.Text = message('TIC2000:hwsetup:AutoFind3P').getString;

                    [obj.Workflow.CCSInstallLocation, obj.Workflow.CCSInstallVersion] = obj.Workflow.HardwareInterface.getCCSInfo();
                    obj.enableScreen;
                end
                % Disable next button to prevent multiple press
                obj.NextButton.Enable = 'off';
            end
        end
        
        function reinit(obj)
            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';
            obj.StatusTable.Visible = 'off';
            if ~isempty(obj.BusySpinner) && strcmpi(obj.BusySpinner.Visible, 'on')
                obj.BusySpinner.Visible = 'off';
            end
        end
        
        function out = getPreviousScreenID(obj)
            if any(contains(obj.Workflow.SelectedProcessors, 'TI F280x'))
                out = 'codertarget.tic2000.hwsetup.ValidateF280x';
            elseif any(contains(obj.Workflow.SelectedProcessors, 'TI F28044'))
                out = 'codertarget.tic2000.hwsetup.ValidateF28044';
            elseif obj.Workflow.isNeededValidateSUITE
                out = 'codertarget.tic2000.hwsetup.ValidateControlSUITE';
            else
                if (obj.Workflow.required3PToolCount == 2) && ...
                        obj.Workflow.autoInstall3PTool(2).validationScreenRequired
                    out = 'codertarget.tic2000.hwsetup.ValidateARMCGT';
                elseif obj.Workflow.autoInstall3PTool(1).validationScreenRequired
                    out = 'codertarget.tic2000.hwsetup.ValidateCGT';
                else
                    out = 'codertarget.tic2000.hwsetup.InstallTpSoftware2';
                end
            end
        end
        
        
        function validateF281x(obj, ~, ~)
            % validate path first
            installDir = obj.ValidateEditText.Text;
            obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:SpecifyValidFolder').getString;
            installDirSplit = obj.Workflow.HardwareInterface.splitDirName(installDir);
            % Check for the folder of pattern v<NUMBER> to ensure valid
            % parent directory
            if isempty(installDirSplit{end})
                notValidParentFolder = isempty(regexp(installDirSplit{end-1},'[v]\d', 'once'));
            else
                notValidParentFolder = isempty(regexp(installDirSplit{end},'[v]\d', 'once'));
            end
            if exist(installDir, 'dir') ~= 7
                obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Fail};
                obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:DirectoryNA').getString};
                obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:HelpTextPathFailure').getString;
                error(message('TIC2000:setup:DirectoryDoesNotExist', ...
                    installDir, message('TIC2000:hwsetup:F281xError').getString));
            elseif ~contains(installDir,'dsp281x', 'IgnoreCase', true) || notValidParentFolder
                % Warn user to provide dsp2804x installation parent folder
                obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Fail};
                obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:DirectoryNA').getString};
                obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:HelpTextPathFailure').getString;
                error(message('TIC2000:hwsetup:F281xParentDirectoryNA', ...
                    installDir,  message('TIC2000:hwsetup:InstallTpF281x').getString));
            end
            
            % store new installDir
            obj.Workflow.F281xLocation = installDir;
            
            %show StatusTable
            obj.StatusTable.ColumnWidth = [20 160];
            obj.StatusTable.Position = [20 220 390 30];
            obj.StatusTable.Visible = 'on';
            
            % throw a warning, and give option to proceed.
            % Add check for version match.
            reqDir = fullfile('DSP281x', 'v120');
            
            if ~contains(installDir, reqDir, 'IgnoreCase', true)
                obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:HelpTextProcessorVersionMismatch', 'v120').getString;
                warndlg(message('TIC2000:hwsetup:ValidateF281xWarn', installDir, 'v120').getString);
                obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Warn};
                obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:UntestedVersion').getString};
            else
                obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Pass};
                obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:ValidationSuccessfull').getString};
                % Show all these widgets
            end
            obj.NextButton.Enable = 'on';
            
        end
        
    end
end