// ignore_for_file: public_member_api_docs, sort_constructors_first
// 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_file_system.dart';
import 'package:brick_generator/models/boolean_mapping.dart';
import 'package:brick_generator/models/ignore_list.dart';
import 'package:yaml/yaml.dart';

const _pathToBrickifyKey = 'path_to_brickify';
const _ignoreKey = 'ignore';
const _hooksKey = 'hooks';
const _booleanMappingKey = 'boolean_mapping';
const _booleanFileSystemKey = 'boolean_file_system';

class BrickgenConfig {
  final String pathToBrickify;
  final IgnoreList ignore;
  final bool hooks;
  final List<BooleanMapping> booleanMapping;
  final List<BooleanFileSystem> booleanFileSystem;

  BrickgenConfig({
    required this.pathToBrickify,
    required this.ignore,
    required this.hooks,
    required this.booleanMapping,
    required this.booleanFileSystem,
  });

  factory BrickgenConfig.parse(YamlMap? data) {
    if (data == null) {
      throw ArgumentError.notNull('data');
    }

    final booleanMapping = (data[_booleanMappingKey] as YamlMap?)
            ?.entries
            .map(
              (e) =>
                  BooleanMapping.fromYaml(e.key as String, e.value as YamlMap?),
            )
            .toList() ??
        [];

    final booleanFileSystem = (data[_booleanFileSystemKey] as YamlMap?)
            ?.entries
            .map(
              (e) => BooleanFileSystem.fromYaml(
                e.key as String,
                e.value as YamlMap?,
              ),
            )
            .toList() ??
        [];

    final config = BrickgenConfig(
      pathToBrickify: data[_pathToBrickifyKey] as String? ?? '',
      ignore: IgnoreList.fromConfig(data[_ignoreKey] as YamlList?),
      hooks: data[_hooksKey] as bool? ?? false,
      booleanMapping: booleanMapping,
      booleanFileSystem: booleanFileSystem,
    );

    return config;
  }

  @override
  String toString() => 'BrickgenConfig(pathToBrickify: $pathToBrickify, '
      'ignore: $ignore, hooks: $hooks, booleanMapping: $booleanMapping, '
      'booleanFileSystem: $booleanFileSystem)';
}
