// 代理类
class Proxy {
  final String name;
  final String type;
  final String server;
  final int port;
  final String uuid;
  final int udp;
  final String? flow;
  final String? servername;
  final bool? tls;

  Proxy({
    required this.name,
    required this.type,
    required this.server,
    required this.port,
    required this.uuid,
    required this.udp,
    this.flow,
    this.servername,
    this.tls,
  });

  String toYamlString() {
    var lines = [
      'name: $name',
      'type: $type',
      'server: $server',
      'port: $port',
      'uuid: $uuid',
      'udp: $udp',
    ];

    if (flow != null) lines.add('flow: $flow');
    if (servername != null) lines.add('servername: $servername');
    if (tls != null) lines.add('tls: $tls');

    return lines.map((line) => '  - $line').join('\n');
  }
}

// 代理组类
class ProxyGroup {
  final String name;
  final String type;
  final List<String> proxies;

  ProxyGroup({
    required this.name,
    required this.type,
    required this.proxies,
  });

  String toYamlString() {
    return '  - name: $name\n  type: $type\n  proxies: [${proxies.join(', ')}]';
  }
}

// 主配置类
class YamlConfig {
  final int mixedPort;
  final bool allowLan;
  final String bindAddress;
  final String mode;
  final String logLevel;
  final String externalController;
  final List<Proxy> proxies;
  final List<ProxyGroup> proxyGroups;
  final List<String> rules;

  YamlConfig({
    required this.mixedPort,
    required this.allowLan,
    required this.bindAddress,
    required this.mode,
    required this.logLevel,
    required this.externalController,
    required this.proxies,
    required this.proxyGroups,
    required this.rules,
  });

  String toYamlString() {
    return '''mixed-port: $mixedPort
allow-lan: $allowLan
bind-address: '$bindAddress'
mode: $mode
log-level: $logLevel
external-controller: '$externalController'
proxies:
${proxies.map((proxy) => proxy.toYamlString()).join('\n')}
proxy-groups:
${proxyGroups.map((group) => group.toYamlString()).join('\n')}
rules:
- ${rules.join('\n- ')}''';
  }
}

// void main() {
//   // 为示例，这里仅添加了一些代理和代理组。
//   var exampleProxies = [
//     Proxy(
//       name: '香港22-vless',
//       type: 'vless',
//       server: 'sz.ip2000.top',
//       port: 27399,
//       uuid: '459b4a80-bd61-4ecd-a26b-e9c1809d9e45',
//       udp: 1,
//       flow: 'xtls-rprx-vision',
//       servername: 'www.amazon.com',
//       tls: true,
//     ),
//     // ... 可以根据需要添加更多代理
//   ];
//
//   var exampleProxyGroup = ProxyGroup(
//     name: 'proxy',
//     type: 'select',
//     proxies: ['香港22-vless', '香港原生61D'],
//   );
//
//   var config = YamlConfig(
//     mixedPort: 7890,
//     allowLan: true,
//     bindAddress: '*',
//     mode: 'rule',
//     logLevel: 'info',
//     externalController: '127.0.0.1:9090',
//     proxies: exampleProxies,
//     proxyGroups: [exampleProxyGroup],
//     rules: ['MATCH,用户中心'],
//   );
//
//   // var fileName = 'config_output.yaml';
//   // File(fileName).writeAsStringSync(config.toYamlString());
//   // print('YAML config saved to $fileName');
// }