<?php

require_once dirname(__FILE__) . "/header.php";

/**
 * Updates the given preset
 *
 * @param presetname - The name of the preset. Examples: 'default' 'blog'
 * @param projectname - (optional)The name of the project
 * @param languages - (optional) Commaseparated list of project languages
 * @param templatename - (optional) The name of the tamplte
 */

if (!isset($_REQUEST['presetname'])) {
    \QUI\Setup\Utils\Ajax::output("missing.parameter.presetname");
}
$presetName = $_REQUEST['presetname'];
$projectName = isset($_REQUEST['projectname']) ? $_REQUEST['projectname'] : "";
$languages = isset($_REQUEST['languages']) ? $_REQUEST['languages'] : "";
$templateName = isset($_REQUEST['templatename']) ? $_REQUEST['templatename'] : "";

$presets = \QUI\Setup\Preset::getPresets();
if (!key_exists($presetName, $presets)) {
    \QUI\Setup\Utils\Ajax::output("exception.preset.not.found", 500);
}

$presetData = $presets[$presetName];

if (!empty($projectName)) {
    $signs = [
        'À' => 'A',
        'à' => 'a',
        'Á' => 'A',
        'á' => 'a',
        'Â' => 'A',
        'â' => 'a',
        'Ã' => 'A',
        'ã' => 'a',
        'Å' => 'A',
        'å' => 'a',
        'Æ' => 'AE',
        'æ' => 'ae',
        'Ā' => 'A',
        'ā' => 'a',
        'Ä' => 'AE',
        'ä' => 'ae',
        'Ç' => 'C',
        'ç' => 'c',
        'È' => 'E',
        'è' => 'e',
        'É' => 'E',
        'é' => 'e',
        'Ê' => 'E',
        'ê' => 'e',
        'Ë' => 'E',
        'ë' => 'e',
        'Ē' => 'E',
        'ē' => 'e',
        'Ì' => 'I',
        'ì' => 'i',
        'Í' => 'I',
        'í' => 'i',
        'Î' => 'I',
        'î' => 'i',
        'Ï' => 'I',
        'ï' => 'i',
        'Ī' => 'I',
        'ī' => 'i',
        'Ñ' => 'N',
        'ñ' => 'n',
        'Ò' => 'O',
        'ò' => 'o',
        'Ó' => 'O',
        'ó' => 'o',
        'Ô' => 'O',
        'ô' => 'o',
        'Õ' => 'O',
        'õ' => 'o',
        'Ø' => 'O',
        'ø' => 'o',
        'Ō' => 'O',
        'ō' => 'o',
        'Œ' => 'OE',
        'œ' => 'oe',
        'Ö' => 'OE',
        'ö' => 'oe',
        'Ù' => 'U',
        'ù' => 'u',
        'Ú' => 'U',
        'ú' => 'u',
        'Û' => 'U',
        'û' => 'u',
        'Ũ' => 'U',
        'ũ' => 'u',
        'Ū' => 'U',
        'ū' => 'u',
        'ü' => 'ue',
        'Ü' => 'UE',
        'Ÿ' => 'Y',
        'ÿ' => 'y',
        'ß' => 'ss'
    ];

    foreach ($signs as $from => $to) {
        $projectName = \str_replace($from, $to, $projectName);
    }

    $presetData['project']['name'] = $projectName;
}

function isAssoc(array $arr): bool
{
    if ([] === $arr) {
        return false;
    }

    return array_keys($arr) !== range(0, count($arr) - 1);
}

if (is_array($languages) && isAssoc($languages)) {
    $languages = array_keys($languages);

    $presetData['project']['languages'] = $languages;
    $presetData['project']['default_lang'] = $languages[0];
} elseif (!empty($languages) && is_string($languages)) {
    $presetData['project']['languages'] = explode(",", $languages);
    $presetData['project']['default_lang'] = explode(",", $languages)[0];
}

if (!empty($templateName)) {
    $presetData['template']['name'] = $templateName;
}

try {
    \QUI\Setup\Utils\Validator::validatePresetData($presetData);
} catch (\Exception $Exception) {
    \QUI\Setup\Utils\Ajax::output($Exception->getMessage(), 500);
}

# Save the changes to the file
$json = json_encode($presetData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
$presetFile = dirname(dirname(__FILE__)) . "/templates/presets/" . $presetName . ".json";

if (!is_writable($presetFile)) {
    \QUI\Setup\Utils\Ajax::output("exception.preset.not.writeable", 500);
}

file_put_contents($presetFile, $json);


\QUI\Setup\Utils\Ajax::output(true, 200);
