classdef autoblksMbcImportTask < autoblksBlkSetupTask
    % Task to import data to MBC

    % Copyright 2018-2023 The MathWorks, Inc.

    properties
        BaseMbcProject

        MBCTemplateDirectory char

        DataSetObj autoblksMbcSetupDataset

        % UI objects
        ImportUiTable matlab.ui.control.Table;
    end

    % Import data names
    properties (Dependent, SetAccess=public)
        DataName char
    end


    methods
        %% Constructor
        function obj = autoblksMbcImportTask(BaseMbcProject, InputDataSetObj, MBCTemplateDirectory)
            obj.BaseMbcProject = BaseMbcProject;
            obj.DataSetObj = InputDataSetObj;
            obj.TaskName = obj.DataSetObj.ImportName;
            obj.MBCTemplateDirectory = MBCTemplateDirectory;
            if isempty(obj.DataSetObj.ImportDescription)
                obj.Description = getString(message('autoblks_shared:autoblkSharedMisc:ImportDesc'));
            else
                obj.Description = obj.DataSetObj.ImportDescription;
            end
            obj.EnableViewAppButton = true;
            obj.SavePropNames = 'DataName';
        end

        %% TaskFcn
        function TaskFcn(varargin)
            obj=varargin{1};

            obj.BaseMbcProject.GetProject(true); % Create new project in not available
            if nargin>=2 %&& obj.VVCFlag
                DataFileName=varargin{2};
                obj.DataSetObj.ImportData(obj.TaskName,obj.MBCTemplateDirectory,DataFileName);%obj.TaskName,obj.MBCTemplateDirectory);
            else
                obj.DataSetObj.ImportData(obj.TaskName,obj.MBCTemplateDirectory);
            end
            obj.BaseMbcProject.SaveProject;
        end

        %% ViewInApp
        function ViewInApp(obj)
            obj.BaseMbcProject.GetProject;
            obj.DataSetObj.EditDataEditorCloseFcn = @() obj.CloseEditorCallback;
            obj.DataSetObj.EditData;
        end

        %% GetStatusFcn
        function GetStatusFcn(obj)
            obj.BaseMbcProject.GetProject;
            [obj.Completed, SignalInfo] = obj.DataSetObj.isAllRequiredImported;
            if isempty(SignalInfo)
                obj.ErrorMsg = [];
                obj.WarningMsg = [];
                obj.Completed = false;
            elseif ~isempty(SignalInfo) && ~obj.Completed
                % error
                obj.ErrorMsg = getString(message('autoblks_shared:autoerrMbcUI:NotAllReqSigImported'));
                obj.WarningMsg = [];
            elseif any([SignalInfo.isDerivedVar])
                % warning
                obj.WarningMsg = getString(message('autoblks_shared:autoerrMbcUI:NotAllSigImported'));
                obj.ErrorMsg = [];
            else
                obj.ErrorMsg = [];
                obj.WarningMsg = [];
            end

            if ~isempty(obj.ImportUiTable)
                obj.DisplaySignalUITable(obj.ImportUiTable);
            end
        end

        %% DrawCustomPanel
        function DrawCustomPanel(obj)
            obj.ImportUiTable = obj.CreateTbl('Data Summary', obj.CustomPanelObj);
            obj.DisplaySignalUITable(obj.ImportUiTable);
        end

        %% CloseEditorCallback
        function CloseEditorCallback(obj)
            obj.SuccessorTask.UpdateStatus;
        end

        %% set.DataName
        function set.DataName(obj, DataName)
            obj.DataSetObj.Name = DataName;
        end

        %% get.DataName
        function DataName = get.DataName(obj)
            DataName = obj.DataSetObj.Name;
        end

        %% DisplaySignalUITable
        function DisplaySignalUITable(obj, UITableHdl)
            SignalInfo = obj.DataSetObj.AllImportSignalInfo;
            isUifigure = matlab.ui.internal.isUIFigure(obj.CalAppObj.Figure);
            
            % Make bold by using HTML if not uifigure
            if ~isUifigure
                for i = 1:length(SignalInfo)
                    if SignalInfo(i).isRequired
                        SignalInfo(i).Description = sprintf('<HTML><B>%s<B>', SignalInfo(i).Description);
                        SignalInfo(i).Name = sprintf('<HTML><B>%s<B>', SignalInfo(i).Name);
                        SignalInfo(i).Units = sprintf('<HTML><B>%s<B>', SignalInfo(i).Units);
                    end
                end
            end

            if ~obj.DataSetObj.isDataImported
                % Before import
                DispSignalInfo = struct('Description', {SignalInfo.Description},...
                    'Name', {SignalInfo.Name}, 'Units', {SignalInfo.Units});
                UITableHdl.ColumnName = {getString(message('autoblks_shared:autoblkSharedMisc:SigDesc')),...
                    getString(message('autoblks_shared:autoblkSharedMisc:SigName')),...
                    getString(message('autoblks_shared:autoblkSharedMisc:Units'))};
                UITableHdl.ColumnWidth = {275, 150, 75};
            else
                for i = 1:length(SignalInfo)
                    if ~SignalInfo(i).isImported % Error
                        SignalInfo(i).Status = getString(message('autoblks_shared:autoblkSharedMisc:StatError'));
                    elseif SignalInfo(i).isDerivedVar % Warning
                        SignalInfo(i).Status = getString(message('autoblks_shared:autoblkSharedMisc:StatWarn',SignalInfo(i).DerivedVarExp));
                    else % Imported
                        SignalInfo(i).Status = getString(message('autoblks_shared:autoblkSharedMisc:StatImported'));
                    end
                end

                % After data imported
                DispSignalInfo = struct('Status', {SignalInfo.Status}, 'Description', {SignalInfo.Description},...
                    'Name', {SignalInfo.Name}, 'Units', {SignalInfo.Units});
                UITableHdl.ColumnName = {getString(message('autoblks_shared:autoblkSharedMisc:Stat')),...
                    getString(message('autoblks_shared:autoblkSharedMisc:SigDesc')),...
                    getString(message('autoblks_shared:autoblkSharedMisc:SigName')),...
                    getString(message('autoblks_shared:autoblkSharedMisc:Units'))};
                UITableHdl.ColumnWidth = {120, 200, 125, 75};
            end
            
            UITableHdl.ColumnEditable = false(1, length(UITableHdl.ColumnName));
            UITableHdl.Data = table2cell(struct2table(DispSignalInfo));

            % Make required signals bold if web mode
            if isUifigure
                AllIdx = 1:length(SignalInfo);
                BoldIdx = AllIdx([SignalInfo.isRequired]);
                
                if ~isempty(BoldIdx)
                    addStyle(UITableHdl, uistyle("FontWeight", "bold"), "row",BoldIdx)
                end
            end
        end

    end

end