import 'package:hub/features/storage/index.dart';
import 'package:hub/shared/extensions/string_extensions.dart';

import 'module.dart';

enum LicenseKeys {
  messages('FRE-HUB-MESSAGES'),
  liberations('FRE-HUB-LIBERATIONS'),
  reservations('FRE-HUB-RESERVATIONS'),
  access('FRE-HUB-ACCESS'),
  openedVisits('FRE-HUB-OPENED-VISITS'),
  vehicles('FRE-HUB-VEHICLES'),
  residents('FRE-HUB-RESIDENTS'),
  about('FRE-HUB-ABOUT-SYSTEM'),
  pets('FRE-HUB-PETS'),
  orders('FRE-HUB-ORDERS'),
  completeSchedule('FRE-HUB-COMPLETE-SCHEDULE'),
  providerSchedule('FRE-HUB-AGE-PROV-PREST'),
  deliverySchedule('FRE-HUB-AGE-PROV-DELIVERY'),
  provisionalHistory('FRE-HUB-PROVISIONAL-HISTORY'),
  property('FRE-HUB-ABOUT-PROPERTY'),
  fastPass('FRE-HUB-FASTPASS'),
  visitors('FRE-HUB-VISITORS'),
  qrCode('FRE-HUB-QRCODE'),
  people('FRE-HUB-PEOPLE'),
  petsHistory('FRE-HUB-PETS-HISTORY'),
  settings('FRE-HUB-SETTINGS'),
  logout('FRE-HUB-LOGOUT');

  final String value;
  const LicenseKeys(this.value);
}

class License {
  final List<Module> modules;

  const License(
    this.modules,
  );

  static Future<String> _processWithoutModule(LicenseKeys key) async {
    switch (key) {
      case LicenseKeys.reservations:
        return await _precessWpp();
      case LicenseKeys.orders:
        return await _precessWpp();
      case LicenseKeys.fastPass:
        return await _precessWpp();
      case LicenseKeys.providerSchedule:
        return await _processProvisional();
      case LicenseKeys.deliverySchedule:
        return await _processProvisional();
      case LicenseKeys.pets:
        return await _processPets();
      case LicenseKeys.petsHistory:
        return await _processPets();
      default:
        return ModuleStatus.active.key;
    }
  }

  static Future<String> _precessWpp() async {
    final bool whatsapp = await StorageHelper()
        .get(LocalsStorageKey.whatsapp.key)
        .then((v) => v.toBoolean());
    if (whatsapp)
      return ModuleStatus.active.key;
    else
      return ModuleStatus.inactive.key;
  }

  static Future<String> _processProvisional() async {
    final bool provisional = await StorageHelper()
        .get(LocalsStorageKey.provisional.key)
        .then((v) => v.toBoolean());
    if (provisional)
      return ModuleStatus.active.key;
    else
      return ModuleStatus.inactive.key;
  }

  static Future<String> _processPets() async {
    final bool pets = await StorageHelper()
        .get(LocalsStorageKey.pets.key)
        .then((v) => v.toBoolean());
    if (pets)
      return ModuleStatus.active.key;
    else
      return ModuleStatus.inactive.key;
  }

  static getKeyByModule(String s) {}

  /// Returns a [License] object with the modules and their status.
  /// @param isNewVersion: Indica que é sistema novo que possui modularização.
  static Future<License> getLicense(bool isNewVersionWithModule) async {
    return License([
      Module(
        key: LicenseKeys.messages.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.provisionalHistory.value,
        display: ModuleStatus.inactive.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.liberations.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.reservations.value,
        display: await _processWithoutModule(LicenseKeys.reservations),
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.access.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.access.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.openedVisits.value,
        display: isNewVersionWithModule
            ? ModuleStatus.active.key
            : ModuleStatus.inactive.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.vehicles.value,
        display: isNewVersionWithModule
            ? ModuleStatus.active.key
            : ModuleStatus.inactive.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.residents.value,
        display: isNewVersionWithModule
            ? ModuleStatus.active.key
            : ModuleStatus.inactive.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.pets.value,
        display: await _processWithoutModule(LicenseKeys.pets),
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.orders.value,
        display: await _processWithoutModule(LicenseKeys.orders),
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.completeSchedule.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.providerSchedule.value,
        display: await _processWithoutModule(LicenseKeys.providerSchedule),
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.deliverySchedule.value,
        display: await _processWithoutModule(LicenseKeys.deliverySchedule),
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.fastPass.value,
        display: await _processWithoutModule(LicenseKeys.fastPass),
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.qrCode.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.visitors.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.property.value,
        display: isNewVersionWithModule
            ? ModuleStatus.active.key
            : ModuleStatus.inactive.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.people.value,
        display: isNewVersionWithModule
            ? ModuleStatus.inactive.key
            : ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.petsHistory.value,
        display: await _processWithoutModule(LicenseKeys.pets),
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.settings.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.about.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
      Module(
        key: LicenseKeys.logout.value,
        display: ModuleStatus.active.key,
        expirationDate: '',
        startDate: '',
        quantity: 0,
      ),
    ]);
  }
}
