classdef Results
%COMPILER.BUILD.RESULTS  A data object containing the results of a compiler.build function
%
%    Use a compiler.build function (such as
%    compiler.build.standaloneApplication) to create a
%    COMPILER.BUILD.RESULTS object.
%
%    READ-ONLY PARAMETERS:
%
%    - BuildType
%        Name of the build function used to generate the results.
%
%    - Files
%        A list of files that should be distributed for the MATLAB Compiler
%        artifact to operate.
%
%    - Options
%        A copy of the build options used to generate the results.
%
%    EXAMPLES:
%
%    % Call compiler.build.standaloneApplication which creates a
%    % COMPILER.BUILD.RESULTS object 'buildResults'.
%    buildResults = compiler.build.standaloneApplication('exampleFcn.m');
%
%    See also compiler.build.standaloneApplication

%   Copyright 2020-2021 The MathWorks, Inc.
    
    % should make error checking for these
    properties(Dependent,SetAccess = private)
        BuildType
    end
    properties(SetAccess = private)
        Files
        IncludedSupportPackages
        Options
    end
    properties(Dependent,Access = private)
        RuntimeDefinition
    end
    
    properties(Access = private)
        BuildTypeEnum
    end
    properties(Hidden,SetAccess = private)
        NumericRuntimeDefinition
        BuildID
        BuildDuration
    end
    
    properties(Constant,Hidden)
        ProductComponentModuleNavigator = matlab.depfun.internal.ProductComponentModuleNavigator
    end
    
    % constructor
    methods(Access = private)
        function obj = Results(varargin)
            p = inputParser;
            allprops = properties(obj);
            allprops = [allprops;'RuntimeDefinition'; 'BuildID'; 'BuildDuration'];
            for n = 1:numel(allprops)
                addParameter(p,allprops{n},obj.(allprops{n}))
            end
            parse(p,varargin{:})
            changedprops = setdiff(allprops,p.UsingDefaults);
            
            for n = 1:numel(changedprops)
                obj.(changedprops{n}) = p.Results.(changedprops{n});
            end
        end
    end
    
    % set
    methods
        function obj = set.BuildType(obj,newType)
            validateattributes(newType, ...
                {'compiler.internal.build.BuildTypes'}, ...
                {'nonempty','scalar'})
            obj.BuildTypeEnum = newType;
        end
        
        function obj = set.Options(obj,optObject)
            validateattributes(optObject, ...
                {'compiler.internal.build.AllOptions'},{'scalar'})
            obj.Options = optObject;
        end
        
        function obj = set.IncludedSupportPackages(obj,newSPFile)
            
            % assume a text file is used to define the support packages
            % this logic should likely move to shared place
            fid = fopen(newSPFile,'r');
            oc = onCleanup(@()fclose(fid));
            spdata = textscan(fid,'%s','Delimiter','\n');
            spdata = spdata{1};
            clear oc
            
            % For now, not going to re-check the validity of these values
            if isempty(spdata)
                obj.IncludedSupportPackages = {};
            else
                obj.IncludedSupportPackages = spdata(:);
            end
        end
        
        function obj = set.RuntimeDefinition(obj,newDef)
            
            % assume a text file is used to define the runtime
            prodnums = compiler.internal.validate.requiredMCRProducts(newDef);
            obj.NumericRuntimeDefinition = prodnums(:);
        end
    end
    
    % get
    methods
        function bt = get.BuildType(obj)
            bt = char(obj.BuildTypeEnum);
        end
        
        function rtdef = get.RuntimeDefinition(obj)
            pcmn = obj.ProductComponentModuleNavigator;
            prodnums = obj.NumericRuntimeDefinition;
            rtdef = cell(numel(prodnums),1);
            for n = 1:numel(prodnums)
                switch prodnums{n}
                    case 35000
                        rtdef{n} = 'MATLAB Graphics';
                    case 35010
                        rtdef{n} = 'MATLAB Numerics';
                    otherwise
                        info = pcmn.productInfo(prodnums{n} - 35100);
                        rtdef{n} = info.extPName;
                end
            end
        end
    end
    
    % hidden constructor
    methods(Static,Hidden)
        function obj = constructor(varargin)
            obj = compiler.build.Results(varargin{:});
        end
    end
    
    % save
%     methods
%         function s = saveobj(obj)
%             s.BuildTypeEnum = obj.BuildTypeEnum;
%             s.Files = obj.Files;
%             s.Options = obj.Options;
%             s.NumericRuntimeDefinition = obj.NumericRuntimeDefinition;
%         end
%     end
    
    % load
end
