// 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 BooleanNameSingleMapping extends BooleanNameMapping {
  BooleanNameSingleMapping({
    required this.fileSystemEntityName,
    required this.onTrueName,
    required this.onFalseName,
  });

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

    final fsEntityName = source.keys.first as String;
    final onTrueName =
        (source.values.first as YamlMap?)?[_onTrueKey] as String?;
    final onFalseName =
        (source.values.first as YamlMap?)?[_onFalseKey] as String?;

    return BooleanNameSingleMapping(
      fileSystemEntityName: fsEntityName,
      onTrueName: onTrueName ?? fsEntityName,
      onFalseName: onFalseName ?? fsEntityName,
    );
  }

  @override
  final String fileSystemEntityName;
  @override
  final String onTrueName;
  @override
  final String onFalseName;

  BooleanNameSingleMapping copyWith({
    String? fileSystemEntityName,
    String? onTrueName,
    String? onFalseName,
  }) =>
      BooleanNameSingleMapping(
        fileSystemEntityName: fileSystemEntityName ?? this.fileSystemEntityName,
        onTrueName: onTrueName ?? this.onTrueName,
        onFalseName: onFalseName ?? this.onFalseName,
      );

  @override
  String toString() =>
      'BooleanNameSingleMapping(fileSystemEntityName: $fileSystemEntityName, '
      'onTrueName: $onTrueName, onFalseName: $onFalseName)';
}
