classdef ValidateC2000Ware < matlab.hwmgr.internal.hwsetup.ValidateLocation
    % ValidateC2000Ware - Screen implementation to enable users to select the
    % CCS Tool chain.

    %   Copyright 2017-2023 The MathWorks, Inc.

    properties
        isValidated = true;
        untestedVersion = false;
        ValidateButton
        StatusTable
        StatusTableTitle
        screenReady = false;
    end

    methods
        function obj = ValidateC2000Ware(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);
            obj.StatusTableTitle = matlab.hwmgr.internal.hwsetup.Label.getInstance(obj.ContentPanel);

            % Title for Status Tables
            obj.StatusTableTitle.Position = [20 200 450 65];
            obj.StatusTableTitle.Color = matlab.hwmgr.internal.hwsetup.util.Color.WHITE;

            %Titles
            obj.StatusTableTitle.Text = message('TIC2000:hwsetup:StatusTableTitle').getString;

            % Add selected processor details here
            obj.StatusTable.Status = {''};
            obj.StatusTable.Steps = {''};

            % hide all these widgets
            obj.StatusTable.Visible = 'off';
            obj.StatusTableTitle.Visible = 'off';

            % Set the Title Text
            obj.Title.Text = message('TIC2000:hwsetup:ValidateC2000WareTitle').getString;

            % Set the Description text
            obj.Description.Text = ...
                message('TIC2000:hwsetup:ValidateC2000WareDescription').getString;

            % Help Text
            obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:SpecifyValidFolder').getString;

            if ~contains(obj.Workflow.C2000WareLocation, obj.Workflow.HardwareInterface.C2000WareVersion) || isempty(obj.Workflow.C2000WareLocation)
                [obj.Workflow.C2000WareLocation, obj.Workflow.C2000WareVersion] = obj.Workflow.HardwareInterface.getC2000WareInfo();
                obj.Workflow.C2000WareLocation = fullfile(obj.Workflow.C2000WareLocation, ['C2000Ware_', obj.Workflow.C2000WareVersion]);
            end

            % set values to editText area of control suite and custom
            % installation area
            c2000warelocation = obj.Workflow.C2000WareLocation;
            % Hardcode some default value
            if isempty(obj.Workflow.C2000WareLocation)
                obj.ValidateEditText.Text = obj.Workflow.HardwareInterface.dummyC2000WarePath;
                % Disable Next, to make validation mandatory
                obj.NextButton.Enable = 'off';
            else
                obj.ValidateEditText.Text = c2000warelocation;
            end

            obj.ValidateEditText.ValueChangedFcn = @obj.FolderValidation;

            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';
            obj.StatusTable.Visible = 'off';
            obj.StatusTable.Status = {''};
            obj.StatusTable.Steps = {''};

            % Validate button properties
            obj.ValidateButton.Text = message('TIC2000:hwsetup:Validate').getString;
            obj.ValidateButton.ButtonPushedFcn = @obj.validatecbk;
            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;

            % 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 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
                if strcmp(obj.Workflow.BaseCode, 'TIC2000_BLOCKSET')
                    out = 'codertarget.tic2000.hwsetup.ValidateCGT';
                else
                    out = 'codertarget.tic2000.hwsetup.ConfirmTPInstallation';
                end
                % Disable next button to prevent multiple press
                obj.NextButton.Enable = 'off';
            end
        end

        function out = getPreviousScreenID(~)
            out = 'codertarget.tic2000.hwsetup.ValidateCCS';
        end

        function custombrowseDirectory(obj, ~, ~)
            dir = uigetdir(obj.ValidateEditText.Text, message('TIC2000:hwsetup:C2WareBrowseHelp').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 validatecbk(obj, ~, ~)
            obj.HelpText.AboutSelection = '';
            obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:SpecifyValidFolder').getString;

            % Validate the path once
            installDir = obj.ValidateEditText.Text;
            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';

            % callback on Next to validate CCS path
            % reset values
            obj.isValidated = true;
            obj.untestedVersion = false;
            obj.StatusTableTitle.Visible = 'off';
            obj.StatusTable.Status = {''};
            obj.StatusTable.Steps = {''};

            if isobject(obj.StatusTable)
                obj.StatusTable.Visible = 'off';
            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;
                obj.untestedVersion = true;
                error(message('TIC2000:hwsetup:DirectoryDoesNotExist', ...
                    installDir, message('TIC2000:hwsetup:ValidateC2000WareFolder').getString));
            elseif ~contains(installDir, obj.Workflow.HardwareInterface.C2000WareVersionTag, 'IgnoreCase', true)
                % Warn user to provide installation parent folder
                obj.untestedVersion = true;
                h = warndlg(getString(message('TIC2000:hwsetup:ValidateControlSUITEVerWarn', ...
                    installDir,  obj.Workflow.HardwareInterface.C2000WareVersionTag))); %#ok<NASGU>
                obj.Workflow.C2000WareVersion = char(regexpi(installDir, '\d+(\_\d+)+', 'match'));
            else
                obj.Workflow.C2000WareVersion = obj.Workflow.HardwareInterface.C2000WareVersion;
            end

            % store new installDir
            obj.Workflow.C2000WareLocation = installDir;

            % Validate if Selected Processors device_support is present in
            % selected C200Ware version
            notfound = {''};
            id = 1;
            resultprocessors = obj.Workflow.HardwareInterface.findC2000WareSupportedProcessors(obj.Workflow);
            ProcessorC2000WareMapObj = obj.Workflow.ProcessorC2000WareMapObj;

            for i=1:numel(resultprocessors)
                fullDir = fullfile(installDir, ProcessorC2000WareMapObj(resultprocessors{i}));
                if isunix
                    fullDir = strrep(fullDir,'\','/');
                end
                if exist(fullDir, 'dir') ~= 7
                    notfound{id} = resultprocessors{i};
                    obj.isValidated =  false;
                    id = id + 1;
                end
            end
            if obj.untestedVersion == true
                if obj.isValidated ==  false
                    obj.StatusTableTitle.Visible = 'on';
                    obj.StatusTableTitle.Text = message('TIC2000:hwsetup:C2000WareTargetNotSupported', obj.Workflow.HardwareInterface.C2000WareVersionTag).getString;
                    % Status table properties
                    obj.StatusTable.ColumnWidth = [20 160];
                    obj.StatusTable.Position= [20 20 390 165];

                    % Show all these widgets
                    obj.StatusTable.Visible = 'on';
                    obj.StatusTable.Steps = notfound;
                    obj.StatusTable.Status = {''};
                    obj.StatusTable.Status(1:numel(obj.StatusTable.Steps)) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Fail};

                    % fit the table based on the size
                    w = obj.StatusTable.getWidth();
                    h = obj.StatusTable.getHeight();
                    init_pos = obj.StatusTable.Position;
                    obj.StatusTable.Position  = [init_pos(1) init_pos(2)+init_pos(4)-h w h];
                else
                    obj.StatusTableTitle.Visible = 'off';
                    obj.StatusTable.Visible = 'on';
                    % Status table properties
                    obj.StatusTable.ColumnWidth = [20 410];
                    obj.StatusTable.Position = [20 210 390 30];
                    obj.StatusTable.Status(1) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Warn};
                    obj.StatusTable.Steps(1) = {message('TIC2000:hwsetup:C2000WareUntestedVersion', obj.Workflow.HardwareInterface.C2000WareVersionTag).getString};
                    obj.NextButton.Enable = 'on';
                end
            else
                % Show only one table
                obj.StatusTable.ColumnWidth = [20 410];
                obj.StatusTable.Position = [20 210 390 30];
                obj.StatusTable.Visible = 'on';
                obj.StatusTable.Steps = {message('TIC2000:hwsetup:ValidationSuccessfull').getString};
                obj.StatusTable.Status(1:numel(obj.StatusTable.Steps)) = {matlab.hwmgr.internal.hwsetup.StatusIcon.Pass};
                obj.NextButton.Enable = 'on';
            end
        end

        function reinit(obj)
            % hide the ignore checkbox if possible or uncheck selection
            % Disable Next, to make validation mandatory
            obj.NextButton.Enable = 'off';
            obj.HelpText.WhatToConsider = message('TIC2000:hwsetup:SpecifyValidFolder').getString;
            obj.HelpText.AboutSelection = '';

            obj.StatusTable.Visible = 'off';
            obj.StatusTableTitle.Visible = 'off';
            if ~contains(obj.Workflow.C2000WareLocation, obj.Workflow.HardwareInterface.C2000WareVersion) || isempty(obj.Workflow.C2000WareLocation)
                [obj.Workflow.C2000WareLocation, obj.Workflow.C2000WareVersion] = obj.Workflow.HardwareInterface.getC2000WareInfo();
                obj.Workflow.C2000WareLocation = fullfile(obj.Workflow.C2000WareLocation, ['C2000Ware_', obj.Workflow.C2000WareVersion]);
                obj.ValidateEditText.Text = obj.Workflow.C2000WareLocation;
            end
        end

    end
end
