<?php

use xmvc\MVC;

function sacnLangDir(string $dir, array &$langData)
{
    $isEachSubDir = false;
    $dirSplit = explode('/', $dir);
    $endItem = $dirSplit[count($dirSplit) - 1];
    if ($endItem == '*') {
        unset($dirSplit[count($dirSplit) - 1]);
        $isEachSubDir = true;
        $dir = implode('/', $dirSplit);
    }
    forEachDir($dir, function ($filename, $basename, $type, $level) use (&$langData) {
        if ($type == 'file') {
            parseFile($filename, $langData);
        }
    }, $isEachSubDir);
}

function parseFile(string $filename, array &$langData)
{
    $content = file_get_contents($filename);
    // lg('xxx{:1}xx', ...)
    preg_match_all('/\blg\b\(\s*([\'"])+(.*?)\1\s*([^)]+)?\)/', $content, $m);
    if ($m && isset($m[2])) {
        foreach ($m[2] as $v) {
            $v = trim($v);
            if (!is_numeric($v)) {
                $langData[$v] = $v;
            }
        }
    }
    // <lg 1="xxx" ...>xxx{:xxx}</lg>
    preg_match_all('/<lg(\s*[^>]+)?>(.*?)<\/lg>/', $content, $m);
    if ($m && $m[2]) {
        foreach ($m[2] as $v) {
            $v = trim($v);
            if (!is_numeric($v)) {
                $langData[$v] = $v;
            }
        }
    }
}

$type = I('type');
$isCreate = (bool)I('isCreate', false);
mkdirs(DIR_LOCALE . '/lang');
$xmvcScanFilename = DIR_LOCALE . '/lang/_xmvc_cn.php';

if ($isCreate) {
    if ($type == 'xmvc') {
        $dirs = cfgDirToTrueDir(cfg('mvcLangDirs'));
    } elseif ($type == 'project') {
        $dirs = cfgDirToTrueDir(cfg('projectLangDirs'));
    } else {
        error('不支持的类型');
    }

    $langData = [];
    foreach ($dirs as $dir) {
        sacnLangDir($dir, $langData);
    }
    if ($type == 'xmvc') {
        ksort($langData);
        $langTagList = cfg('lang>list');
        foreach ($langTagList as $tag) {
            $langFilename = DIR_LOCALE . '/lang/_xmvc_' . $tag . '.php';
            if (is_file($langFilename)) {
                $oldLangData = require($langFilename);
                $newLangData = array_intersect_key($oldLangData, $langData);
                $newLangData += array_diff_key($langData, $oldLangData);
            } else {
                $newLangData = $langData;
            }
            ksort($newLangData);
            arrayToFile($langFilename, $newLangData);
        }
    } elseif ($type == 'project') {
        if (is_file($xmvcScanFilename)) {
            $xmvcLangData = require($xmvcScanFilename);
            $langData = array_diff_key($langData, $xmvcLangData);

            $langTagList = cfg('lang>list');
            foreach ($langTagList as $tag) {
                $langFilename = DIR_LOCALE . '/lang/' . $tag . '.php';
                if (is_file($langFilename)) {
                    $oldLangData = require($langFilename);
                    $newLangData = array_intersect_key($oldLangData, $langData);
                    $newLangData += array_diff_key($langData, $oldLangData);
                    if (!IS_DEVELOP && $newLangData != $oldLangData) {
                        copy($langFilename, DIR_BACKUP . '/lang/' . basename($langFilename) . '.' . time() . '.php'); // 备份
                    }
                } else {
                    $newLangData = $langData;
                }
                ksort($newLangData);
                arrayToFile($langFilename, $newLangData);
            }
        } else {
            MVC::warningExit('XMVC 语言包不存在, 须先扫描 XMVC');
        }
    }
    header('Location: ' . url('SELF', null, ['isCreate']));
    exit();
}
// @codingStandardsIgnoreStart
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charSet="utf-8" />
    <title>语言包</title>
</head>

<body>
    <h1><?= strtoupper($type) ?> 语言包</h1>
    <div>
        <a href="<?= HOST ?>/xmvc/core/cmd-lang~type-<?= $type ?>~isCreate-1" onclick="return confirm('确认要扫描生成 <?= strtoupper($type) ?> 语言包吗?')">扫描生成 <?= strtoupper($type) ?> 语言包</a>
    </div>
    <div>

    </div>
</body>

</html>