classdef WebAppSharingStrategy < matlab.internal.deployment.AppSharingStrategy
%

%   Copyright 2018-2020 The MathWorks, Inc.

    properties (Access = private)
        wasNewProjectCreated = false
    end

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

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

        function populateProjectFields(obj, projectKey, appMetaData, imgFullFile)
            if ~isempty(appMetaData.Name)
                obj.service.setDisplayName(projectKey, appMetaData.Name);
            end

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

            if ~isempty(imgFullFile)
                obj.service.setImage(projectKey, imgFullFile);
            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')
                    webAppWindow = compiler.internal.ui.WebAppPreviewWindow(appFullFileName,projectFullFile);
                    webAppWindow.launch();
                else
                    matlab.internal.compiler.ui.deployProject(string(projectFullFile), [], false);
                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_mdwas.services.WebAppCompilerService;
        end

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