/*
 * Copyright (c) 2025 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.
 */

#include "i_dual_inputs.h"
#include "../../one_power_base.h"

namespace dev {

void IPowerSupply_DualInputs::start()
{
    if (!m_base->is_oob_allowed()) {
        return;
    }

    // Initialize properties by reading from hardware
    auto relay_count = m_base->get_relay_switched_count();
    if (relay_count.has_value()) {
        RelaySwitchedCount = relay_count.value();
    }

    auto retransfer_delay = m_base->get_retransfer_delay_seconds();
    if (retransfer_delay.has_value()) {
        RetransferDelaySeconds = retransfer_delay.value();
    }

    auto main_voltage_type = m_base->get_main_circuit_input_voltage_type();
    if (main_voltage_type.has_value()) {
        MainCircuitInputVoltageType = main_voltage_type.value();
    }

    auto backup_voltage_type = m_base->get_backup_circuit_input_voltage_type();
    if (backup_voltage_type.has_value()) {
        BackupCircuitInputVoltageType = backup_voltage_type.value();
    }

    auto main_vin_status = m_base->get_main_circuit_vin_status();
    if (main_vin_status.has_value()) {
        MainCircuitVINStatus = main_vin_status.value();
    }

    auto backup_vin_status = m_base->get_backup_circuit_vin_status();
    if (backup_vin_status.has_value()) {
        BackupCircuitVINStatus = backup_vin_status.value();
    }
    // Note: PowerSupplyChannel is not readable from hardware, only set via SetPowerSupplyCircuit
}

void IPowerSupply_DualInputs::stop()
{}

void IPowerSupply_DualInputs::init(OnePowerBase* base_ptr)
{
    m_base = base_ptr;
}

void IPowerSupply_DualInputs::SetPowerSupplyCircuit(uint8_t PowerSupplyCircuit)
{
    if (PowerSupplyCircuit > 1) {
        MC_THROW(mc::invalid_argument_exception, "PowerSupplyCircuit must be 0 or 1");
    }
    m_base->set_power_supply_circuit(PowerSupplyCircuit);
    this->PowerSupplyChannel = PowerSupplyCircuit;
}

void IPowerSupply_DualInputs::SetRetransferDelaySeconds(uint32_t RetransferDelaySeconds)
{
    m_base->set_retransfer_delay_seconds(RetransferDelaySeconds);
    this->RetransferDelaySeconds = RetransferDelaySeconds;
}

} // namespace dev

MC_REFLECT(dev::IPowerSupply_DualInputs)