<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ConvertUnihanStrokes extends Command
{
    protected $signature = 'convert:unihan-strokes 
                            {--input= : Unihan 原始檔案路徑} 
                            {--output= : 輸出 PHP 陣列檔案路徑}';

    protected $description = '將 Unihan_IRGSources.txt 中的 kTotalStrokes 轉為 PHP 陣列格式';

    public function handle()
    {
        $input = $this->option('input') ?? base_path('resources/data/Unihan_IRGSources.txt');
        $output = $this->option('output') ?? base_path('resources/data/unihan_strokes.php');

        if (!file_exists($input)) {
            $this->error("找不到輸入檔案: $input");
            return 1;
        }

        $handle = fopen($input, 'r');
        if (!$handle) {
            $this->error("無法開啟檔案: $input");
            return 1;
        }

        $strokes = [];

        while (($line = fgets($handle)) !== false) {
            $line = trim($line);
            if ($line === '' || $line[0] === '#') {
                continue;
            }

            if (preg_match('/^U\+([0-9A-F]+)\s+kTotalStrokes\s+(\d+)/i', $line, $matches)) {
                $unicodeHex = 'U+' . strtoupper($matches[1]);
                $strokeCount = intval($matches[2]);
                $strokes[$unicodeHex] = $strokeCount;
            }
        }

        fclose($handle);

        // 寫入 PHP 陣列檔
        //$exported = var_export($strokes, true);
        //$phpCode = "<?php\n\nreturn {$exported};\n";

        //file_put_contents($output, $phpCode);
        // 每 100 筆換一行，自訂格式寫入 PHP 陣列檔
        $lines = ["<?php", "", "return ["];
        $count = 0;
        $line = '    ';

        foreach ($strokes as $key => $value) {
            $line .= var_export($key, true) . ' => ' . var_export($value, true) . ', ';
            $count++;

            if ($count % 100 === 0) {
                $lines[] = rtrim($line);
                $line = '    ';
            }
        }

        if (trim($line) !== '') {
            $lines[] = rtrim($line);
        }

        $lines[] = "];";
        $lines[] = "";

        $phpCode = implode("\n", $lines);
        file_put_contents($output, $phpCode);

        $this->info("✅ 轉換完成，資料已儲存至: $output");

        return 0;
    }
}
