
classdef ValidateARMCGTSoC < matlab.hwmgr.internal.hwsetup.ValidateLocation
    % ValidateCCS - Screen implementation to enable users to select the
    % CCS Tool chain.
    
    %   Copyright 2017-2021 The MathWorks, Inc.
    
    properties
        ValidateButton
        StatusTable
    end
    
    methods
        function obj = ValidateARMCGTSoC(varargin)
            % call to the base class constructor ValidateLocation template
            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);
            
            % Set the Title Text
            obj.Title.Text = message('TIC2000:hwsetup:ValidateARMCGTTitle').getString;
            obj.Description.Text = message('TIC2000:hwsetup:ValidateARMCGTDescription').getString;
            
            cgtpath = matlab.internal.get3pInstallLocation('ticoncertoarmcodegentools.instrset');
            if isempty(cgtpath)
                var = fullfile(obj.Workflow.CCSInstallLocation,'tools','compiler',strcat('ti-cgt-arm_', char(message('TIC2000:hwsetup:ARMCGTVersion').string), '.LTS'));
                if exist(var, 'dir') == 7
                    obj.ValidateEditText.Text = var;
                else
                    obj.ValidateEditText.Text = obj.Workflow.HardwareInterface.dummyARMCGTPath;
                end
            else
                cgtpath = fullfile(cgtpath, strcat('ti-cgt-arm_', char(message('TIC2000:hwsetup:ARMCGTVersion').string), '.LTS'));
                obj.ValidateEditText.Text = cgtpath;
            end
            obj.Workflow.ARMCGTVersion = char(message('TIC2000:hwsetup:ARMCGTVersion').string);
            
            obj.ValidateEditText.ValueChangedFcn = @obj.validatePath;
            
            % browse button callback
            obj.BrowseButton.Color = matlab.hwmgr.internal.hwsetup.util.Color.MWBLUE;
            obj.BrowseButton.FontColor = matlab.hwmgr.internal.hwsetup.util.Color.WHITE;
            obj.BrowseButton.ButtonPushedFcn = @obj.custombrowseDirectory;
            
            % Status table properties
            obj.StatusTable.Status = {''};
            obj.StatusTable.Steps = {''};
            obj.StatusTable.Visible = 'off';
            
            % ValidateButton properties
            obj.ValidateButton.Text = message('TIC2000:hwsetup:Validate').getString;
            obj.ValidateButton.ButtonPushedFcn = @obj.validate;
            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.ValidateButton.Visible = 'off';
            
            % Help Text
            obj.HelpText.WhatToConsider =  message('TIC2000:hwsetup:ValidateCGTWhatToConsider').getString;
        end
        
        function custombrowseDirectory(obj, ~, ~)
            dir = uigetdir(obj.ValidateEditText.Text, message('TIC2000:hwsetup:CGTBrowseHelp').getString);
            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 reinit(~)
        end
        
        function out = getNextScreenID(obj)
            installDir= obj.ValidateEditText.Text;
            if exist(installDir, 'dir') ~= 7
                error(message('TIC2000:setup:DirectoryDoesNotExist', ...
                    installDir, message('TIC2000:hwsetup:ValidateARMCGTFolder').getString));
            end
            obj.Workflow.ARMCGTLocation = installDir;
            if (obj.Workflow.isConcerto)
                out = 'codertarget.concerto.hwsetup.ConfirmTPInstallation';
            else
                out = 'codertarget.tic2000.hwsetup.ConfirmTPInstallation';
            end
        end
        
        function out = getPreviousScreenID(~)
            out = 'codertarget.tic2000.hwsetup.ValidateCGT';
        end
        
        function validatePath(obj, ~, ~)
            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';
            obj.ValidateButton.Visible = 'on';
        end
        
        function validate(obj, ~, ~)
            installDir= obj.ValidateEditText.Text;
            
            if exist(installDir, 'dir') ~= 7
                error(message('TIC2000:setup:DirectoryDoesNotExist', ...
                    installDir, message('TIC2000:hwsetup:ValidateARMCGTFolder').getString));
            end
            
            % callback on Next to validate CCS path
            % Check if the directory exists
            installDir= obj.ValidateEditText.Text;
            
            %show StatusTable
            obj.StatusTable.ColumnWidth = [20 160];
            obj.StatusTable.Position= [20 220 230 30];
            obj.StatusTable.Visible = 'on';
            
            % throw a warning, and give option to proceed.
            % Add check for version match.
            % do better validation here
            readMeFileDoesNotExists = ~(exist(fullfile(installDir, 'README.txt'), 'file') == 2 || ...
                exist(fullfile(installDir, 'README.html'), 'file') == 2);
            if readMeFileDoesNotExists
                % Warn user to provide cgt installation parent folder
                obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Fail};
                obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:CompilerPath').getString};
                error(getString(message('TIC2000:hwsetup:CGTParentDirectoryNA', ...
                    installDir, message('TIC2000:hwsetup:ValidateARMCGTFolder').getString)));
            elseif ~contains(installDir, strcat('ti-cgt-arm_', char(message('TIC2000:hwsetup:ARMCGTVersion').string)), 'IgnoreCase', true)
                % Warn if the selected version is not tested.
                h = warndlg(message('TIC2000:hwsetup:ValidateCGTWarn', installDir, char(message('TIC2000:hwsetup:ARMCGTVersion').string)).getString); %#ok<NASGU>
                obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Warn};
                obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:UntestedVersion').getString};
                % Read from parent folder name
                ver = regexpi(installDir, '\d+(\.\d+)+', 'match');% match between v and - and get version number
                obj.Workflow.ARMCGTVersion = ver{1};
            else
                obj.Workflow.ARMCGTVersion = char(message('TIC2000:hwsetup:ARMCGTVersion').string);
                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