import 'package:args/command_runner.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirro/getfastmirror.dart';

class GetfastmirroCommand extends Command<int> {
  GetfastmirroCommand({
    required Logger logger,
  }) : _logger = logger {
    argParser.addFlag(
      'only-test',
      abbr: 'o',
      help: "Only test mirrors' speed",
      negatable: false,
    );
  }

  @override
  String get description =>
      'Get fastest mirror and generate config file for oyo';

  @override
  String get name => 'getfastmirror';

  final Logger _logger;

  @override
  Future<int> run() async {
    final output = lightBlue.wrap('''
Get the fastest mirror which will be write into source file
And generate a config file for oyo
''');
    _logger.info(output);
    final onlyTest = argResults?['only-test'] == true;
    final commandResult = await getFastMirror(onlyTest: onlyTest);
    if (commandResult['success'] == false) {
      _logger.err(commandResult['msg'] as String);
      return ExitCode.unavailable.code;
    }
    return ExitCode.success.code;
  }
}
