// ignore_for_file: constant_identifier_names

import 'dart:async';
import 'dart:io';
import 'package:dart_framework/utils/system_util.dart';
import 'package:oto/oto/application.dart';
import 'package:oto/oto/commands/command.dart';
import 'package:oto/oto/data/command_data.dart';

enum MSBuildType { Clean, Build, Rebuild }

enum MSBuildConfig { Debug, Release }

class BuildMSBuild extends Command {
  Function? get error => null;

  @override
  Future execute(DataCommand command) async {
    var data = DataBuildMSBuild.fromJson(getParam(command));

    var param =
        '${data.projectFile} /p:Configuration=${data.config.name} /p:Platform=x64 /t:${data.type.name}';

    var shell = StringBuffer(getCDPath(workspace));
    shell.write(' $and msbuild $param');

    var process = await runtime.start(shell,
        decoder: SystemEncoding().decoder,
        printStderr: false,
        logHandler: Application.logWithType);
    return await completeProcess(process, command);
  }
}

void registerBuildMsbuildCommands() {
  Command.register(
    CommandType.BuildMSBuild,
    () => BuildMSBuild(),
    spec: const CommandSpec(
      type: CommandType.BuildMSBuild,
      category: 'build',
      dataModel: 'DataBuildMSBuild',
      samplePath: 'assets/yaml/sample/10_build_tools.yaml',
    ),
  );
}
