import 'dart:io';
import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/apt_fast_config_template.dart';

final tmpDir = Directory.systemTemp.path;
final String filePath = '$tmpDir/oyo/apt-fast.conf';
final File file = File(filePath);
final List<String> mirroListInConfigFileYoyo = [];
final List<String> mirroListInConfigFileUbuntu = [];

void generateAptFastConfigFile(
  List<Map<String, String>> sortedUrlListAndTimeYoyo,
  List<Map<String, String>> sortedUrlListAndTimeUbuntu,
) {
  //Yoyo
  for (final element in sortedUrlListAndTimeYoyo) {
    mirroListInConfigFileYoyo.add(element['url'] ?? '');
  }
  final mirroListInConfigFileStringYoyo = mirroListInConfigFileYoyo
      .toString()
      .replaceAll('[', '')
      .replaceAll(']', '');
  //Ubuntu
  for (final element in sortedUrlListAndTimeUbuntu) {
    mirroListInConfigFileUbuntu.add(element['url'] ?? '');
  }
  final mirroListInConfigFileStringUbuntu = mirroListInConfigFileUbuntu
      .toString()
      .replaceAll('[', '')
      .replaceAll(']', '');

  final configContent = aptFastConfigTemplate(
    mirroListInConfigFileStringYoyo,
    mirroListInConfigFileStringUbuntu,
  );
  if (!file.existsSync()) file.createSync(recursive: true);
  file.writeAsStringSync(configContent);
}
