// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:brick_generator/models/brick_variable_boolean.dart';
import 'package:brick_generator/models/brick_variable_string.dart';
import 'package:brick_generator/models/variable_type.dart';
import 'package:yaml/yaml.dart';

const _typeKey = 'type';

abstract class BrickVariable<T> {
  BrickVariable({
    required this.type,
    required this.name,
    required this.description,
    required this.defaultValue,
    required this.prompt,
  });

  String name;
  VariableType type;
  String description;
  T defaultValue;
  String prompt;

  static BrickVariable<dynamic> fromYaml(String key, YamlMap? source) {
    final type = VariableType.fromString(source?[_typeKey] as String?);
    switch (type) {
      case VariableType.string:
        return BrickVariableString.fromYaml(key, source);
      case VariableType.boolean:
        return BrickVariableBoolean.fromYaml(key, source);
      case VariableType.none:
        throw ArgumentError('Invalid variable type', 'type');
    }
  }

  // TODO(wyatt): use `yaml_writer` to dumps YAML properly.
  String toMason() => '''
  $name:
    type: $type
    description: $description
    default: $defaultValue
    prompt: $prompt
''';
}
