//  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 may 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 FIT FOR A PARTICULAR PURPOSE.
//  See the Mulan PSL v2 for more details.
import $http from '@/utils/http-service';
import { IPodData, ISuperPodLabelEnum } from './pod.datatype';

// 获取Net数据
export function getPodData(): Promise<any> {
  return new Promise((resolve, reject) => {
    $http
      .get('/UI/Rest/System/SuperPodOptions')
      .then((res: any) => {
        resolve(res.data);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

export function factoryPodData(data: any): IPodData {
    const { ChassisID: chassisID, ChassisIDSupported: chassisIDSupported } = data;
    const {
        SuperPodId: nodeNumber,
        SuperPodSize: nodeScale,
        TopologyType: nodeTopology,
        InterconType: nodeMode,
        PresentDPUCount: nodeDPU,
        ServerIndex: nodeOS,
        NPUBootOption: bootMode,
        UBManagedBy: manageMode,
        SuperPodIdSupported: superPodIdSupported,
        SuperPodSizeSupported: superPodSizeSupported,
        TopologyTypeSupported: topologyTypeSupported,
        InterconTypeSupported: interconTypeSupported,
        PresentDPUCountSupported: presentDPUCountSupported,
        ServerIndexSupported: serverIndexSupported,
        NPUBootOptionSupported: npuBootOptionSupported,
        UBManagedBySupported: ubManagedBySupported,
        PresentDPUCountAllowedNumbers: presentDPUCountAllowedNumbers,
        TopologyTypeAllowedNumbers: topologyTypeAllowedNumbers,
        InterconTypeAllowedNumbers: interconTypeAllowedNumbers,
        NPUBootOptionAllowedNumbers: npuBootOptionAllowedNumbers,
        UBManagedByAllowedNumbers: ubManagedByAllowedNumbers,
    } = data.SuperPodLabel || {};

    const result: IPodData = {
        nodeNumber,
        nodeScale,
        nodeFrame: chassisID,
        nodeTopology,
        nodeMode,
        nodeDPU,
        nodeOS,
        bootMode,
        manageMode,
        superPodIdSupported,
        superPodSizeSupported,
        topologyTypeSupported,
        interconTypeSupported,
        presentDPUCountSupported,
        chassisIDSupported,
        serverIndexSupported,
        npuBootOptionSupported,
        ubManagedBySupported,
        topologyTypeAllowedNumbers,
        interconTypeAllowedNumbers,
        npuBootOptionAllowedNumbers,
        ubManagedByAllowedNumbers,
        presentDPUCountAllowedNumbers,
    };
    return result;
}

export function getShowedComps(comps: any[]): any[] {
    return comps.filter(comp => comp.show !== false);
}

export function saveParams(forms: any[]): any {
    const params: any = {};
    for (const form of forms) {
        // 不需要转number的字段
        if (form.associated === ISuperPodLabelEnum.CHASSISID) {
            params[form.associated] = form.value;
            continue;
        }

        if (typeof form.value === 'object') {
            params[form.associated] = form.value;
            continue;
        }
        if (!Object.prototype.hasOwnProperty.call(params, 'SuperPodLabel')) {
            params.SuperPodLabel = {};
        }
        params.SuperPodLabel[form.associated] = transformToNumber(form.value);
    }
    return updateConfig(params);
}

function updateConfig(params: any): Promise<any> {
  return new Promise((resolve, reject) => {
    $http
      .patch('/UI/Rest/System/SuperPodOptions', params)
      .then((res: any) => {
        resolve(res.data);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

export function resetValue(): Promise<any> {
  return new Promise((resolve, reject) => {
    $http
      .post('/UI/Rest/System/SuperPodOptions/ResetToDefaults', {})
      .then((res: any) => {
        resolve(res.data);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

function transformToNumber(str: string): number | string {
    const transformed = parseInt(str, 10);
    if (isNaN(transformed)) {
        return str;
    }
    return transformed;
}