// 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/core/string_extension.dart';
import 'package:brick_generator/models/brick_variable.dart';
import 'package:brick_generator/models/variable_string_syntax.dart';
import 'package:brick_generator/models/variable_type.dart';
import 'package:yaml/yaml.dart';

const _compilableKey = 'compilable';
const _formatsKey = 'formats';
const _typeKey = 'type';
const _descriptionKey = 'description';
const _defaultKey = 'default';
const _promptKey = 'prompt';

class BrickVariableString extends BrickVariable<String> {
  BrickVariableString({
    required this.compilable,
    required this.formats,
    required this.syntaxes,
    required super.type,
    required super.name,
    required super.description,
    required super.defaultValue,
    required super.prompt,
  });

  factory BrickVariableString.fromYaml(String key, YamlMap? source) {
    if (source == null) {
      throw ArgumentError.notNull('source');
    }

    final type = VariableType.fromString(source[_typeKey] as String?);

    final formats = (source[_formatsKey] as YamlList?)
            ?.map((element) => element as String)
            .map(VariableStringSyntax.fromString) ??
        VariableStringSyntax.values;

    final syntaxes = (source[_compilableKey] as String?)?.syntaxes ?? {};

    return BrickVariableString(
      compilable: source[_compilableKey] as String? ?? '',
      type: type,
      syntaxes: syntaxes,
      name: key,
      description: source[_descriptionKey] as String? ?? '',
      defaultValue: source[_defaultKey] as String? ?? '',
      prompt: source[_promptKey] as String? ?? '',
      formats: formats.toList(),
    );
  }

  String compilable;
  Map<String, String>? syntaxes;
  List<VariableStringSyntax> formats;

  @override
  String toString() => 'BrickVariableString(name: $name, '
      'description: $description, compilable: $compilable, '
      'defaultValue: $defaultValue, prompt: $prompt,)';
}
