classdef ValidateARMCGT < matlab.hwmgr.internal.hwsetup.ValidateLocation
    % ValidateCGT - Screen implementation to enable users to select the
    % CGT Tool chain.

    %   Copyright 2017-2021 The MathWorks, Inc.

    properties
        ValidateButton
        StatusTable
        screenReady = false;
    end

    methods
        function obj = ValidateARMCGT(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;

            % 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;

            % Status table properties
            obj.StatusTable.Status = {''};
            obj.StatusTable.Steps = {''};
            obj.StatusTable.Visible = 'off';

            if isempty(obj.Workflow.autoInstall3PTool) || ...
                    isempty(obj.Workflow.autoInstall3PTool(2).registeredInstallFolder)
                obj.ValidateEditText.Text = obj.Workflow.HardwareInterface.dummyARMCGTPath;
                % Disable Next, to make validation mandatory
                obj.NextButton.Enable = 'off';
                obj.ValidateButton.Visible = 'on';
                obj.Workflow.ARMCGTVersion = '';
            elseif ~isequal(obj.Workflow.autoInstall3PTool(2).registeredInstallFolder, ...
                    fullfile(obj.Workflow.autoInstall3PTool(2).installParentFolder, ...
                    obj.Workflow.autoInstall3PTool(2).installFolder))
                % Disable Next, to make validation mandatory
                % Considering user given option as priority even though the
                % installation may not be a valid version and there is a
                % valid installation available in default path
                obj.ValidateEditText.Text = obj.Workflow.autoInstall3PTool(2).registeredInstallFolder;
                obj.NextButton.Enable = 'off';
                obj.ValidateButton.Visible = 'on';
            else
                obj.ValidateEditText.Text = obj.Workflow.autoInstall3PTool(2).registeredInstallFolder;
                % Enable next as the path is same as the installed path
                obj.NextButton.Enable = 'on';
                obj.ValidateButton.Visible = 'off';
                obj.Workflow.ARMCGTVersion = obj.Workflow.autoInstall3PTool(2).version;
            end
            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;

            % Help Text
            obj.HelpText.WhatToConsider =  message('TIC2000:hwsetup:ValidateCGTWhatToConsider').getString;
            % 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:CGTBrowseHelp').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 reinit(obj)
            if isempty(obj.Workflow.autoInstall3PTool) || ...
                    isempty(obj.Workflow.autoInstall3PTool(2).registeredInstallFolder)
                obj.ValidateEditText.Text = obj.Workflow.HardwareInterface.dummyARMCGTPath;
                % Disable Next, to make validation mandatory
                obj.NextButton.Enable = 'off';
                obj.ValidateButton.Visible = 'on';
                obj.Workflow.ARMCGTVersion = '';
            elseif ~isequal(obj.Workflow.autoInstall3PTool(2).registeredInstallFolder, ...
                    fullfile(obj.Workflow.autoInstall3PTool(2).installParentFolder, ...
                    obj.Workflow.autoInstall3PTool(2).installFolder))
                % Disable Next, to make validation mandatory
                % Considering user given option as priority even though the
                % installation may not be a valid version and there is a
                % valid installation available in default path
                obj.ValidateEditText.Text = obj.Workflow.autoInstall3PTool(2).registeredInstallFolder;
                obj.NextButton.Enable = 'off';
                obj.ValidateButton.Visible = 'on';
            else
                obj.ValidateEditText.Text = obj.Workflow.autoInstall3PTool(2).registeredInstallFolder;
                % Enable next as the path is same as the installed path
                obj.NextButton.Enable = 'on';
                obj.ValidateButton.Visible = 'off';
                obj.Workflow.ARMCGTVersion = obj.Workflow.autoInstall3PTool(2).version;
            end
            obj.StatusTable.Visible = '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
                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;
                obj.Workflow.autoInstall3PTool(2).registeredInstallFolder = installDir;
                if obj.Workflow.isNeededValidateSUITE
                    out = 'codertarget.tic2000.hwsetup.ValidateControlSUITE';
                elseif any(contains(obj.Workflow.SelectedProcessors, 'TI F28044'))
                    out = 'codertarget.tic2000.hwsetup.ValidateF28044';
                elseif any(contains(obj.Workflow.SelectedProcessors, 'TI F280x'))
                    out = 'codertarget.tic2000.hwsetup.ValidateF280x';
                elseif any(contains(obj.Workflow.SelectedProcessors, 'TI F281x'))
                    out = 'codertarget.tic2000.hwsetup.ValidateF281x';
                else
                    out = 'codertarget.tic2000.hwsetup.ValidateCCS';
                end
                % Disable next button to prevent multiple press
                obj.NextButton.Enable = 'off';
            end
        end

        function out = getPreviousScreenID(obj)
            if obj.Workflow.autoInstall3PTool(1).validationScreenRequired
                out = 'codertarget.tic2000.hwsetup.ValidateCGT';
            else
                out = 'codertarget.tic2000.hwsetup.InstallTpSoftware2';
            end
        end

        function validatePath(obj, ~, ~)
            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';
            obj.ValidateButton.Visible = 'on';
        end

        function validate(obj, ~, ~)
            % callback on Next to validate CCS path
            % Check if the directory exists

            installDir= obj.ValidateEditText.Text;
            [isValidCGT, CGTVersion] = obj.Workflow.HardwareInterface.validateTIARMCGT(installDir);

            %show StatusTable
            obj.StatusTable.ColumnWidth = [20 160];
            obj.StatusTable.Position= [20 220 390 30];
            obj.StatusTable.Visible = 'on';

            if isValidCGT == 0
                obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Fail};
                obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:CompilerPath').getString};
                error(message('TIC2000:setup:DirectoryDoesNotExist', ...
                    installDir, message('TIC2000:hwsetup:ValidateARMCGTFolder').getString));
            else
                % throw a warning, and give option to proceed.
                % Add check for version match.
                % do better validation here
                if isValidCGT == -1
                    % 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 isValidCGT == -2
                    % 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};
                    if isempty(CGTVersion)
                        obj.HelpText.AboutSelection = message('TIC2000:hwsetup:VersionFailed').getString;
                    end
                    obj.NextButton.Enable = 'on';
                else %isValidCGT == 1
                    obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Pass};
                    obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:ValidationSuccessfull').getString};
                    obj.NextButton.Enable = 'on';
                end
            end
            obj.Workflow.ARMCGTVersion = CGTVersion;
        end
    end
end