using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace SGModule.Common.Helper {
    public static class ModuleVersion {
        public static void Show() {
            var path = Path.Combine(Application.dataPath, "../gupm.toml");

            if (!File.Exists(path)) {
                Log.Warning("Submodules", $"找不到配置文件: {path}");
                return;
            }

            var insideSubmodules = false;
            var submodules = new Dictionary<string, string>();
            var lines = File.ReadAllLines(path);

            foreach (var rawLine in lines) {
                var line = rawLine.Trim();

                // 进入 [Submodules] 区块
                if (!insideSubmodules) {
                    if (line.Equals("[Submodules]", StringComparison.OrdinalIgnoreCase)) {
                        insideSubmodules = true;
                    }

                    continue;
                }

                // 如果遇到下一个区块就退出（可选）
                if (line.StartsWith("[") && line.EndsWith("]")) {
                    break;
                }

                // 忽略空行和注释
                if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) {
                    continue;
                }

                // 使用 = 来切分
                var parts = line.Split(new[] { '=' }, 2);
                if (parts.Length != 2) {
                    continue;
                }

                var key = parts[0].Trim();
                var value = parts[1].Trim().Trim('"');

                submodules[key] = value;
            }

            foreach (var kv in submodules) {
                Log.Info("Submodules", $"{kv.Key} : {kv.Value}");
            }
        }
    }
}
