classdef DesktopAppSharingStrategy < matlab.internal.deployment.AppSharingStrategy
%

%   Copyright 2018-2020 The MathWorks, Inc.

    methods (Access = protected)
        function [projectFullFile, projectKey] = createProjectWithUniqueFileName(obj, filteredAppName, appFullFileName)
            [projectFullFile, projectKey] = matlab.internal.deployment.AppSharingUtils...
                .createProjectWithUniqueFileName(filteredAppName, appFullFileName, obj.service);
        end

        function projectKey = openProject(obj, projectFullFileName)
            projectKey = matlab.internal.deployment.AppSharingUtils.openProject(obj.service, projectFullFileName);
        end

        function populateProjectFields(obj, projectKey, appMetaData, ~)
            if ~isempty(appMetaData.Name)
                filteredAppName = obj.getFilteredAppName(appMetaData.Name);
                obj.service.setApplicationName(projectKey, filteredAppName);
            end

            if ~isempty(appMetaData.Summary)
                obj.service.setSummary(projectKey, appMetaData.Summary);
            end

            if ~isempty(appMetaData.Description)
                obj.service.setDescription(projectKey, appMetaData.Description);
            end

            if ~isempty(appMetaData.Author)
                obj.service.setAuthorName(projectKey, appMetaData.Author);
            end

            if ~isempty(appMetaData.Version)
                obj.service.setVersion(projectKey, appMetaData.Version);
            end

        end

        function projectFullFile = saveAndCloseProject(obj, projectKey)
            projectFullFile = matlab.internal.deployment.AppSharingUtils.saveAndCloseProject(obj.service, projectKey);
        end

        function showProject(obj, projectFullFile, appFullFileName)
            try
                if feature('webui')
                    standalonePreviewWindow = compiler.internal.ui.StandalonePreviewWindow(appFullFileName,projectFullFile);
                    standalonePreviewWindow.launch();
                else
                    matlab.internal.deployment.AppSharingUtils.showProject(obj.service, projectFullFile);
                end
            catch ex
                matlab.internal.deployment.AppSharingUtils.handleCommonCompilerErrors(ex, char(projectFullFile));
            end
        end
    end

    methods (Static, Access = protected)
        function service = getProjectService()
            service = com.mathworks.toolbox.compiler.services.ApplicationCompilerService;
        end

        function chars = getInvalidCharactersForAppName()
            chars = '<>\\\\/?*:|\" ';
        end
    end
end