import 'package:wyatt_clean_code/core/enums/flavor.dart';

class FlavorSettings {
  static FlavorSettings? _instance;

  final Flavor flavor;

  // Per flavor settings
  String apiKey = '';

  /// Banner are not display in release mode, whatever this value
  bool displayBanner = true;

  FlavorSettings._(this.flavor);

  factory FlavorSettings.development() {
    _instance ??= FlavorSettings._(Flavor.development);
    if (_instance!.flavor != Flavor.development) {
      throw Exception('Flavor already initialized in: ${_instance!.flavor}');
    }
    return _instance!;
  }

  factory FlavorSettings.staging() {
    _instance ??= FlavorSettings._(Flavor.staging);
    if (_instance!.flavor != Flavor.staging) {
      throw Exception('Flavor already initialized in: ${_instance!.flavor}');
    }
    return _instance!;
  }

  factory FlavorSettings.production() {
    _instance ??= FlavorSettings._(Flavor.production);
    if (_instance!.flavor != Flavor.production) {
      throw Exception('Flavor already initialized in: ${_instance!.flavor}');
    }
    return _instance!;
  }

  /// Returns initialized [FlavorSettings], may throw if not initialized.
  static FlavorSettings get() {
    if (_instance == null) {
      throw Exception('Flavor not initialized!');
    }
    return _instance!;
  }

  /// To call after `WidgetsFlutterBinding.ensureInitialized()`
  ///
  /// Here you can config all the settings attributes.
  static void init() {
    switch (get().flavor) {
      case Flavor.development:
        _instance!.apiKey = 'example-dev';
        break;
      case Flavor.staging:
        _instance!.apiKey = 'example-stg';
        break;
      case Flavor.production:
        _instance!.apiKey = 'example-prod';
        break;
    }
  }
}
