/*
 * Copyright (c) 2026 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_control.h"
#include "../../one_power_base.h"

namespace dev {
void IPowerSupply_Control::start()
{
    if (!m_base->is_oob_allowed()) {
        return;
    }

    // Initialize properties by reading from hardware
    auto fan_min_pwm = m_base->get_fan_min_pwm();
    if (fan_min_pwm.has_value()) {
        FanMinPWM = fan_min_pwm.value();
    }

    auto output_power_limit = m_base->get_output_power_limit_watts();
    if (output_power_limit.has_value()) {
        OutputPowerLimitWatts = output_power_limit.value();
    }

    auto sleep_mode = m_base->get_sleep_mode_enum();
    if (sleep_mode.has_value()) {
        // uint8 to string: 0 -> "Normal", 1 -> "DeepSleep"
        SleepMode = (sleep_mode.value() == 1) ? "DeepSleep" : "Normal";
    }

    auto work_mode = m_base->get_work_mode_enum();
    if (work_mode.has_value()) {
        // uint8 to string: 0 -> "Enabled", 1 -> "StandbySpare"
        WorkMode = (work_mode.value() == 1) ? "StandbySpare" : "Enabled";
    }
}

void IPowerSupply_Control::stop()
{}

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

void IPowerSupply_Control::SetFanMinPWM(uint16_t FanMinPWM)
{
    m_base->set_psu_fan_min_pwm(FanMinPWM);
    // 更新属性
    this->FanMinPWM = FanMinPWM;
}

void IPowerSupply_Control::SetWorkMode(uint8_t WorkMode)
{
    if (WorkMode > 1) {
        MC_THROW(mc::invalid_argument_exception, "WorkMode must be 0 or 1");
    }

    m_base->set_power_work_mode(WorkMode);
    // 更新属性:0 -> "Enabled", 1 -> "StandbySpare"
    this->WorkMode = (WorkMode == 1) ? "StandbySpare" : "Enabled";
}

void IPowerSupply_Control::SetSleepMode(std::string SleepMode)
{
    if (SleepMode != "Normal" && SleepMode != "DeepSleep") {
        MC_THROW(mc::invalid_argument_exception, "SleepMode must be Normal or DeepSleep");
    }

    m_base->set_sleep_mode(SleepMode);
    // 更新属性(string类型)
    this->SleepMode = SleepMode;
}

void IPowerSupply_Control::Reset(std::string ResetType)
{
    if (ResetType != "ForceRestart" && ResetType != "ForceOff") {
        MC_THROW(mc::invalid_argument_exception, "ResetType must be ForceRestart or ForceOff");
    }

    m_base->reset(ResetType);
}

void IPowerSupply_Control::SetAlarmLatch(uint8_t Enable)
{
    m_base->set_alarm_latch(Enable);
}

void IPowerSupply_Control::SetPowerLatch()
{
    m_base->set_power_latch();
}

void IPowerSupply_Control::SetOutputPowerLimitWatts(uint32_t Watts)
{
    m_base->set_output_power_limit_watts(Watts);
    // 更新属性
    this->OutputPowerLimitWatts = Watts;
}
} // namespace dev

MC_REFLECT(dev::IPowerSupply_Control)