<!--
 ~ Copyright 2022 youngmonkeys.org
 ~ 
 ~ Licensed under the ezyplatform, Version 1.0.0 (the "License");
 ~ you may not use this file except in compliance with the License.
 ~ You may obtain a copy of the License at
 ~ 
 ~     https://youngmonkeys.org/licenses/ezyplatform-1.0.0.txt
 ~ 
 ~ Unless required by applicable law or agreed to in writing, software
 ~ distributed under the License is distributed on an "AS IS" BASIS,
 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ~ See the License for the specific language governing permissions and
 ~ limitations under the License.
-->

<!DOCTYPE html>
<html
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org">
<body>
<script th:fragment="scripts" th:inline="javascript">
/*<![CDATA[*/
ezyadmin.messages.apply_module_pname_pversion_failed = /*[[#{apply_module_pname_pversion_failed}]]*/ '';
/*]]>*/
// ========== project buy ==========
ezyadmin.openBuyProjectLink = function(name) {
    var callbackUri = location.pathname + location.search;
    var url = '/market/sso' +
        '?callbackUri=' + encodeURIComponent(callbackUri) +
        '&buyPluginName=' + name;
    window.location = url;
}

// ========== new version apply ========
ezyadmin.onRestartToApplyButtonClick = function(e, moduleName, projectName, version) {
    ezyadmin.showLoadingScreen();
    var actualModuleName = moduleName || ezyadmin.currentModuleName;
    var actualProjectName = projectName || ezyadmin.currentModuleProjectName;
    var actualVersion = version || ezyadmin.currentModuleNewApplyItemVersion;
    $.ajax({
        url: '/api/v1/projects/' + actualModuleName + '/check-dependencies',
        type: 'POST',
        dataType: 'json',
    })
    .done(function(project) {
        ezyadmin.hideLoadingScreen();
		ezyadmin.installEzyPlatform();
    })
    .fail(function(e) {
        ezyadmin.processGetApiErrors(e);
        if (e.status == 403) {
            $('#applyFailedTitle').text(
                ezyadmin.messages
                    .apply_module_pname_pversion_failed
                    .replace('{0}', actualProjectName)
                    .replace('{1}', actualVersion)
            );
            $('#missingDependenciesDivApplyFailed').addClass('d-none');
	        $('#needToUpdateDependenciesDivApplyFailed').addClass('d-none');
	        $('#inactiveDependenciesDivApplyFailed').addClass('d-none');
            ezyadmin.processModuleActionErrors(e, 'ApplyFailed');
            $('#apply-module-version-failed-modal').modal('show');
        }
        ezyadmin.hideLoadingScreen();
    });
}

// ========== project install ==========
ezyadmin.installProject = function(projectName, version) {
    if (ezyadmin.hasNewPlatformVersion) {
		ezyadmin.showUpdateToLatestEzyPlatformModal();
		return;
	}
    ezyadmin.showLoadingScreen();
    $.ajax({
        type: 'POST',
        url: '/api/v1/projects/' + projectName + '/' + version + '/install',
    }).done(function () {
        if (location.pathname == '/modules/add') {
            ezyadmin.redirectToModuleDetailsAfterInstall(projectName);
        } else {
            location.reload();
        }
    }).fail(function (e) {
        var errors = e.responseJSON || {};
        if (e.status == '403' && errors.download == "paidProject") {
            ezyadmin.openBuyProjectLink(projectName);
        } else {
            ezyadmin.processUpdateApiErrors({}, e);
        }
        ezyadmin.hideLoadingScreen();
    });
}

ezyadmin.redirectToModuleDetailsAfterInstall = function(projectName) {
    $.ajax({
        url: '/api/v1/projects/' + projectName + '/module-types',
        type: 'GET',
        dataType: 'json',
    })
    .done(function(moduleTypes) {
        var moduleTypeMap = ezyadmin.listToBooleanMap(moduleTypes);
        var actualModuleType = moduleTypeMap[ezyadmin.currentModuleType]
            ? ezyadmin.currentModuleType
            : moduleTypes[0];
        window.location = '/' + actualModuleType + 's/' + projectName
            + (ezyadmin.lang ? '?lang=' + ezyadmin.lang : '');
    })
    .fail(function(e) {
        ezyadmin.processGetApiErrors(e);
        window.location = '/plugins/' + projectName
            + (ezyadmin.lang ? '?lang=' + ezyadmin.lang : '');
    });
}
</script>
</body>
</html>
