// Copyright (c) 2024 Huawei Technologies Co., Ltd.
// openUBMC is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You can obtain a copy of Mulan PSL v2 at:
//        #  http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

import * as generated from './configure.generated';

export * from './configure.generated';

const {
  LDAP_PAGE_ENABLED,
  ENERGY_PAGE_ENABLED,
  VTPCM_PAGE_ENABLED,
  WEBVNC_PAGE_ENABLED,
  PRODUCT_INFO_ENABLED,
  PROCESSOR_ENABLED,
  MEMORY_ENABLED,
  NETWORK_ADAPTER_ENABLED,
  THRESHOLD_SENSOR_ENABLED,
  DISCRETE_SENSOR_ENABLED,
  FRU_ENABLED,
  ASSEMBLY_ENABLED,
  OTHER_ENABLED,
  MONITOR_ENABLED,
  STORAGE_ENABLED,
  POWER_INFO_ENABLED,
  POWER_CAP_ENABLED,
  POWER_CONVERTER_ENABLED,
  POWER_CONTROL_ENABLED,
  POWER_SUPPLY_MODE_ENABLED,
  POWER_ENERGY_ENABLED,
  POWER_ENERGY_SAVING_ENABLED,
  FAN_ENABLED,
  FAN_CALC_MODEL_MANAGEMENT_ENABLED,
  FAN_SENSOR_ENABLED,
  THERMAL_ENABLED,
  BIOS_ENABLED,
  BIOS_CPU_ENABLED,
  BIOS_SETTINGS_ENABLED,
  BIOS_EXPORT_ENABLED,
  PARTITY_ENABLED,
  POD_ENABLED,
  REPORT_ENABLED,
  TRAP_ENABLED,
  VIDEO_ENABLED,
  SYSTEM_LOG_ENABLED,
  BMC_LOG_ENABLED,
  WORK_RECORD_ENABLED,
  ASSET_ENABLED,
  ASSET_CHANGE_ENABLED,
  TRUSTED_SUPPLY_CHAIN_ENABLED,
  RETIRE_ENABLED,
  OBSERVE_ENABLED,
  KVM_ENABLED,
  VNC_ENABLED,
  LOCAL_USER_ENABLED,
  LDAP_USER_ENABLED,
  TWO_FACTOR_ENABLED,
  ONLINE_USER_ENABLED,
  SECURITY_ENABLED,
  CER_ENABLED,
  TPCM_ENABLED,
  KERBEROS_ENABLED,
  PORT_ENABLED,
  WEB_ENABLED,
  VMM_ENABLED,
  SNMP_ENABLED,
  NETWORK_ENABLED,
  NTP_ENABLED,
  UPGRADE_ENABLED,
  UPGRADE_REPO_ENABLED,
  CONFIG_ENABLED,
  CONFIG_PARTS_RECOVERY_ENABLED,
  LANGUAGE_ENABLED,
  LICENSE_ENABLED,
  BMA_ENABLED,
  IBMA_ENABLED,
  SP_ENABLED,
  USB_ENABLED
} = generated;

export interface FeaturePageFlags {
  ldap: boolean;
  energy: boolean;
  vtpcm: boolean;
  webvnc: boolean;
}

export interface FeatureSummaryFlags {
  productInfo: boolean;
  processor: boolean;
  memory: boolean;
  networkAdapter: boolean;
  thresholdSensor: boolean;
  discreteSensor: boolean;
  fru: boolean;
  assembly: boolean;
  other: boolean;
  monitor: boolean;
  storage: boolean;
}

export interface FeaturePowerFlags {
  info: boolean;
  cap: boolean;
  converter: boolean;
  control: boolean;
  supplyMode: boolean;
  energy: boolean;
  energySaving: boolean;
  fan: boolean;
  fanCalcModelManagement: boolean;
  fanSensor: boolean;
  thermal: boolean;
}

export interface FeatureBiosFlags {
  base: boolean;
  cpu: boolean;
  settings: boolean;
  export: boolean;
}

export interface FeatureSystemFlags {
  partity: boolean;
  pod: boolean;
}

export interface FeatureReportingFlags {
  report: boolean;
  trap: boolean;
  video: boolean;
  systemLog: boolean;
  bmcLog: boolean;
  workRecord: boolean;
  observe: boolean;
}

export interface FeatureAssetFlags {
  asset: boolean;
  assetChange: boolean;
  trustedSupplyChain: boolean;
  retire: boolean;
}

export interface FeatureVirtualConsoleFlags {
  kvm: boolean;
  vnc: boolean;
}

export interface FeatureUserFlags {
  local: boolean;
  ldap: boolean;
  twoFactor: boolean;
  online: boolean;
  security: boolean;
}

export interface FeatureSecurityFlags {
  cer: boolean;
  tpcm: boolean;
  kerberos: boolean;
}

export interface FeatureServiceFlags {
  port: boolean;
  web: boolean;
  vmm: boolean;
  snmp: boolean;
  network: boolean;
  ntp: boolean;
  upgrade: boolean;
  config: boolean;
  configPartsRecovery: boolean;
  upgradeRepo: boolean;
  language: boolean;
  license: boolean;
}

export interface FeatureManagementFlags {
  bma: boolean;
  ibma: boolean;
  sp: boolean;
  usb: boolean;
}

export interface FeatureConfig {
  page: FeaturePageFlags;
  summary: FeatureSummaryFlags;
  power: FeaturePowerFlags;
  bios: FeatureBiosFlags;
  system: FeatureSystemFlags;
  reporting: FeatureReportingFlags;
  asset: FeatureAssetFlags;
  virtualConsole: FeatureVirtualConsoleFlags;
  user: FeatureUserFlags;
  security: FeatureSecurityFlags;
  service: FeatureServiceFlags;
  management: FeatureManagementFlags;
}

export const featureConfig: FeatureConfig = {
  page: {
    ldap: LDAP_PAGE_ENABLED,
    energy: ENERGY_PAGE_ENABLED,
    vtpcm: VTPCM_PAGE_ENABLED,
    webvnc: WEBVNC_PAGE_ENABLED
  },
  summary: {
    productInfo: PRODUCT_INFO_ENABLED,
    processor: PROCESSOR_ENABLED,
    memory: MEMORY_ENABLED,
    networkAdapter: NETWORK_ADAPTER_ENABLED,
    thresholdSensor: THRESHOLD_SENSOR_ENABLED,
    discreteSensor: DISCRETE_SENSOR_ENABLED,
    fru: FRU_ENABLED,
    assembly: ASSEMBLY_ENABLED,
    other: OTHER_ENABLED,
    monitor: MONITOR_ENABLED,
    storage: STORAGE_ENABLED
  },
  power: {
    info: POWER_INFO_ENABLED,
    cap: POWER_CAP_ENABLED,
    converter: POWER_CONVERTER_ENABLED,
    control: POWER_CONTROL_ENABLED,
    supplyMode: POWER_SUPPLY_MODE_ENABLED,
    energy: POWER_ENERGY_ENABLED,
    energySaving: POWER_ENERGY_SAVING_ENABLED,
    fan: FAN_ENABLED,
    fanCalcModelManagement: FAN_CALC_MODEL_MANAGEMENT_ENABLED,
    fanSensor: FAN_SENSOR_ENABLED,
    thermal: THERMAL_ENABLED
  },
  bios: {
    base: BIOS_ENABLED,
    cpu: BIOS_CPU_ENABLED,
    settings: BIOS_SETTINGS_ENABLED,
    export: BIOS_EXPORT_ENABLED
  },
  system: {
    partity: PARTITY_ENABLED,
    pod: POD_ENABLED
  },
  reporting: {
    report: REPORT_ENABLED,
    trap: TRAP_ENABLED,
    video: VIDEO_ENABLED,
    systemLog: SYSTEM_LOG_ENABLED,
    bmcLog: BMC_LOG_ENABLED,
    workRecord: WORK_RECORD_ENABLED,
    observe: OBSERVE_ENABLED
  },
  asset: {
    asset: ASSET_ENABLED,
    assetChange: ASSET_CHANGE_ENABLED,
    trustedSupplyChain: TRUSTED_SUPPLY_CHAIN_ENABLED,
    retire: RETIRE_ENABLED
  },
  virtualConsole: {
    kvm: KVM_ENABLED,
    vnc: VNC_ENABLED
  },
  user: {
    local: LOCAL_USER_ENABLED,
    ldap: LDAP_USER_ENABLED,
    twoFactor: TWO_FACTOR_ENABLED,
    online: ONLINE_USER_ENABLED,
    security: SECURITY_ENABLED
  },
  security: {
    cer: CER_ENABLED,
    tpcm: TPCM_ENABLED,
    kerberos: KERBEROS_ENABLED
  },
  service: {
    port: PORT_ENABLED,
    web: WEB_ENABLED,
    vmm: VMM_ENABLED,
    snmp: SNMP_ENABLED,
    network: NETWORK_ENABLED,
    ntp: NTP_ENABLED,
    upgrade: UPGRADE_ENABLED,
    config: CONFIG_ENABLED,
    configPartsRecovery: CONFIG_PARTS_RECOVERY_ENABLED,
    upgradeRepo: UPGRADE_REPO_ENABLED,
    language: LANGUAGE_ENABLED,
    license: LICENSE_ENABLED
  },
  management: {
    bma: BMA_ENABLED,
    ibma: IBMA_ENABLED,
    sp: SP_ENABLED,
    usb: USB_ENABLED
  }
};

export function isFeatureEnabled(path: string): boolean {
  const fields = path.split('.');
  let current: any = featureConfig;

  for (const field of fields) {
    if (!Object.prototype.hasOwnProperty.call(current, field)) {
      return false;
    }
    current = current[field];
  }

  return current === true;
}