-- 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.

local class = require 'mc.class'
local client = require 'general_hardware.client'
local ctx = require 'mc.context'
local log = require 'mc.logging'

-- 升级进度回调
local UpgradeProcess = class()
function UpgradeProcess:ctor(system_id, firmware_type, start_process, step_progress, parameters)
    self.start_process = start_process
    self.step_progress = step_progress
    self.firmware_type = firmware_type
    self.system_id = system_id
    self.cur_progress = start_process
    self.parameters = parameters
end

function UpgradeProcess:get_comp_percent()
    return self.cur_progress
end

function UpgradeProcess:set_comp_percent(progress)
    if self.cur_progress >= progress then
        return
    end
    self.cur_progress = progress
    client:UpdateServiceUpdateServiceUpdateUpgradeStatus(
        ctx.get_context_or_default(), self.system_id, self.firmware_type, 0, math.modf(progress), 'Running', self.parameters)
end

function UpgradeProcess:process(percentage)
    self:set_comp_percent(self.cur_progress + (self.step_progress - self.start_process) * percentage)
end

return UpgradeProcess