import 'dart:async';

import 'package:dart_framework/core/app_data_manager.dart';
import 'package:oto/oto/commands/command.dart';
import 'package:oto/oto/data/command_data.dart';

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

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

    var appData = await AppDataManager.to
        .create(data.scm, workspace, data.relativeAssetPath, data.name);
    await setProperty(data.setVersion, appData.version);
    await setProperty(data.setHash, appData.gitHash.substring(0, 7));
    if(data.setCount != null) await setProperty(data.setCount!, appData.gitCount.toString());
    if(data.setDate != null) await setProperty(data.setDate!, appData.buildDate);
    return complete(0, command);
  }
}

void registerAppDataCreatorCommands() {
  Command.register(
    CommandType.CreateAppData,
    () => CreateAppData(),
    spec: const CommandSpec(
      type: CommandType.CreateAppData,
      category: 'build',
      dataModel: 'DataAppDataCreator',
    ),
  );
}
