// 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/boolean_name_mapping.dart';
import 'package:yaml/yaml.dart';

const _onTrueKey = 'on_true';
const _onFalseKey = 'on_false';

class BooleanNameListMapping extends BooleanNameMapping {
  BooleanNameListMapping({
    required this.fileSystemEntityName,
    required this.onTrueNames,
    required this.onFalseNames,
  });

  factory BooleanNameListMapping.fromYaml(YamlMap? source) {
    if (source == null) {
      throw ArgumentError.notNull('source');
    }

    final fsEntityName = source.keys.first as String;
    final onTrueNames =
        ((source.values.first as YamlMap?)?[_onTrueKey] as YamlList?)
            ?.map((element) => element as String)
            .toList();
    final onFalseNames =
        ((source.values.first as YamlMap?)?[_onFalseKey] as YamlList?)
            ?.map((element) => element as String)
            .toList();

    return BooleanNameListMapping(
      fileSystemEntityName: fsEntityName,
      onTrueNames: onTrueNames ?? [],
      onFalseNames: onFalseNames ?? [],
    );
  }

  @override
  final String fileSystemEntityName;
  final List<String> onTrueNames;
  final List<String> onFalseNames;

  BooleanNameListMapping copyWith({
    String? fileSystemEntityName,
    List<String>? onTrueNames,
    List<String>? onFalseNames,
  }) =>
      BooleanNameListMapping(
        fileSystemEntityName: fileSystemEntityName ?? this.fileSystemEntityName,
        onTrueNames: onTrueNames ?? this.onTrueNames,
        onFalseNames: onFalseNames ?? this.onFalseNames,
      );

  @override
  String get onFalseName => onFalseNames.first;

  @override
  String get onTrueName => onTrueNames.first;

  @override
  String toString() =>
      'BooleanNameListMapping(fileSystemEntityName: $fileSystemEntityName, '
      'onTrueNames: $onTrueNames, onFalseNames: $onFalseNames)';
}
