import 'dart:async';

import 'package:dart_framework/utils/system_util.dart';
import 'package:oto/oto/commands/build/build_dart.dart';
import 'package:oto/oto/commands/command.dart';
import 'package:oto/oto/data/command_data.dart';

// ignore: constant_identifier_names
enum BuildPlatform { Windows, Macos, Web, Apk, Appbundle, iOS, Linux }

class BuildFlutter extends BuildDart {
  @override
  Future<StringBuffer> appendShell(
      StringBuffer shell, DataCommand command) async {
    var data = DataBuildFlutter.fromJson(getParam(command));
    var platform = data.platform.toLowerCase();
    var flavor = data.flavor == null ? '' : ' --flavor ${data.flavor!}';
    var additional = data.additional == null ? '' : ' ${data.additional!}';
    if (data.macPassword != null) {
      shell.write(' $and security unlock-keychain -p${data.macPassword}');
    }
    shell.write(' $and flutter build $platform$flavor$additional');
    return dataFutrue(shell);
  }
}

void registerBuildFlutterCommands() {
  Command.register(
    CommandType.BuildFlutter,
    () => BuildFlutter(),
    spec: const CommandSpec(
      type: CommandType.BuildFlutter,
      category: 'build',
      dataModel: 'DataBuildFlutter',
      samplePath: 'assets/yaml/sample/11_build_flutter.yaml',
    ),
  );
}
