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


local frudata_intf = require 'frudata_intf'
local common = require 'common'
local manage = require 'frudata_object.manage'

local frudata_object = {}

frudata_object.__index = frudata_object

function frudata_object:new(fruinfo_manage, obj)
    return setmetatable({
        fruinfo_manage = fruinfo_manage,
        obj = obj
    }, frudata_object)
end

function frudata_object:init()
    local fru_id = self.obj.FruId
    local fru_file, system_data, fru_offset, uid

    fru_file, system_data, fru_offset, uid = manage:frudev_read_from_eeprom(self.obj)
    if uid ~= '' then
        manage:update_fru_uid(fru_id, uid)
    end

    -- EepromV2类型电子标签eeprom_format为0，其他类型默认使用TianChi规范类型为1
    local eeprom_format = 1
    local is_sup_dft = 1
    local is_sys_area = fru_id == 0 and 1 or 0

    self.fruinfo_manage[fru_id] = {
        IsSupportDft = is_sup_dft,
        EepFormat = eeprom_format,
        FruOffset = fru_offset,
        SystemOffset = common.SYSTEM_OFFSET
    }
    if self.update_flag then
        frudata_intf.clear_fru_info(fru_id)
    end
    frudata_intf.frudata_init(fru_id, fru_file, system_data, is_sup_dft, eeprom_format, is_sys_area)
end

function frudata_object:write_system_area_to_eep(fru_id)
    manage:write_system_area_to_eep(fru_id, self.obj, self.fruinfo_manage)
end

function frudata_object:write_fru_area(fru_id, _)
    -- 天池数据进行重新读取，区别于MCU
    local data = frudata_intf.get_fru_area(fru_id)
    manage:write_fru_area_to_eep(fru_id, data, self.obj, self.fruinfo_manage)
end

function frudata_object:update_sys_data_to_dbus(fru_id)
    manage:update_sys_data_to_dbus(fru_id, self.obj)
end

function frudata_object:update_fru_data_to_dbus(fru_id)
    manage:update_fru_data_to_dbus(fru_id, self.obj)
end

return frudata_object
