local ResourceItemDataManager = BaseClass("ResourceItemDataManager")
local Localization = CS.GameEntry.Localization
local ERROR_GET_ITEM_TIME_GAP = 10000
local __init = function(self)
  self.itemList = {}
  self.freezerStorageMax = 0
  self.warehouseStorageMax = 0
  self.freezerProtectMax = 0
  self.warehouseProtectMax = 0
  self.needSellConfirm = true
  self.itemTemplateDic = {}
  self.itemTemplateTypeList = {}
  EventManager:GetInstance():AddListener(EventId.EffectNumChange, self.UpdateMaxValueSignal)
  self.maxErrorMessageFlag = false
  self.lastGetResourceItemTime = 0
end
local __delete = function(self)
  EventManager:GetInstance():RemoveListener(EventId.EffectNumChange, self.UpdateMaxValueSignal)
  self.itemList = {}
  self.freezerStorageMax = 0
  self.warehouseStorageMax = 0
  self.freezerProtectMax = 0
  self.warehouseProtectMax = 0
  self.needSellConfirm = true
  self.itemTemplateDic = {}
  self.itemTemplateTypeList = {}
  self.maxErrorMessageFlag = false
  self.lastGetResourceItemTime = 0
end
local Startup = function()
end
local GetResourceItemTemplate = function(self, id)
  if self.itemTemplateDic[tonumber(id)] == nil then
    local oneTemplate = LocalController:instance():getLine(TableName.Aps_Resource_Item, id)
    if oneTemplate ~= nil then
      local item = ResourceItemTemplate.New()
      item:InitData(oneTemplate)
      if item.id ~= nil then
        self.itemTemplateDic[item.id] = item
      end
    end
  end
  return self.itemTemplateDic[tonumber(id)]
end
local RefreshItemList = function(self, message)
  local list = message.resource_items
  self.soldierDataChanged = false
  if list ~= nil then
    table.walk(list, function(k, v)
      self:RefreshOneItem(v)
    end)
    EventManager:GetInstance():Broadcast(EventId.RefreshResourceItem)
  end
  if self.soldierDataChanged then
    EventManager:GetInstance():Broadcast(EventId.SoldierDataChanged)
    self.soldierDataChanged = false
  end
end
local InitRefreshItemList = function(self, message)
  self.itemList = {}
  RefreshItemList(self, message)
end
local GetSellConfirmFlag = function(self)
  return self.needSellConfirm
end
local SetSellConfirmFlag = function(self, value)
  self.needSellConfirm = value
end
local RefreshOneItem = function(self, data)
  if data ~= nil then
    local uuid = data.uuid
    if uuid ~= nil then
      local itemData = self.itemList[uuid]
      if itemData ~= nil then
        itemData:RefreshData(data)
        if itemData.number <= 0 then
          self.itemList[uuid] = nil
          itemData = nil
        end
      else
        itemData = ResourceItemData.New()
        itemData:InitData(data)
        if itemData.number > 0 then
          self.itemList[uuid] = itemData
        end
      end
      if itemData ~= nil and itemData.template and (itemData.template.type == ResourceItemRealType.Soldier or itemData.template.type == ResourceItemRealType.DragonSoldier or itemData.template.type == ResourceItemRealType.MummySoldier) then
        self.soldierDataChanged = true
      end
      self:TryShowGiftGetPopup(itemData, data.addNum)
    end
  end
end

function ResourceItemDataManager:TryShowGiftGetPopup(itemData, addNum)
  if not (itemData and itemData.template and addNum) or addNum <= 0 then
    return
  end
  if itemData.template.type == ResourceItemRealType.MasterExp then
    UIManager:GetInstance():OpenWindow(UIWindowNames.LWUITCExpGet, {
      anim = true,
      playEffect = false,
      UIMainAnim = UIMainAnimType.LeftRightBottomHide
    }, itemData.itemId, addNum)
  end
end

local GetItemDataByUuid = function(self, uuid)
  return self.itemList[uuid]
end
local GetItemDataByItemId = function(self, itemId)
  local oneData
  for k, v in pairs(self.itemList) do
    if v.itemId == itemId then
      oneData = v
      break
    end
  end
  return oneData
end
local AddItemNum = function(self, itemId, num)
  for _, v in pairs(self.itemList) do
    if v.itemId == itemId then
      v.number = v.number + num
      EventManager:GetInstance():Broadcast(EventId.RefreshResourceItem)
      break
    end
  end
end
local UpdateMaxValueSignal = function(self, data)
  DataCenter.ResourceItemDataManager:UpdateResourceItemMaxValue()
end
local UpdateResourceItemMaxValue = function(self)
  self.freezerStorageMax = LuaEntry.Effect:GetGameEffect(EffectDefine.FREEZER_STORAGE_MAX_LIMIT) + LuaEntry.Effect:GetGameEffect(EffectDefine.FREEZER_STORAGE_ADD) + LuaEntry.Effect:GetGameEffect(EffectDefine.STORAGE_MAX_EXTRA)
  self.freezerProtectMax = 0
  self.warehouseStorageMax = LuaEntry.Effect:GetGameEffect(EffectDefine.WAREHOUSE_STORAGE_MAX_LIMIT)
  self.warehouseProtectMax = LuaEntry.Effect:GetGameEffect(EffectDefine.WAREHOUSE_PROTECT_MAX_LIMIT)
end
local GetFreezerStorageMax = function(self, withoutExtra)
  self.freezerStorageMax = LuaEntry.Effect:GetGameEffect(EffectDefine.FREEZER_STORAGE_MAX_LIMIT) + LuaEntry.Effect:GetGameEffect(EffectDefine.FREEZER_STORAGE_ADD) + LuaEntry.Effect:GetGameEffect(EffectDefine.STORAGE_MAX_EXTRA)
  if not withoutExtra then
    return math.floor(self.freezerStorageMax)
  else
    return math.floor(LuaEntry.Effect:GetGameEffect(EffectDefine.FREEZER_STORAGE_MAX_LIMIT) + LuaEntry.Effect:GetGameEffect(EffectDefine.FREEZER_STORAGE_ADD))
  end
end
local GetResourceItemTotalNumByType = function(self, type)
  local num = 0
  if self.itemList then
    table.walk(self.itemList, function(k, v)
      if v.itemType == type then
        num = num + v.number
      end
    end)
  end
  return num
end
local GetResourceItemListByTypeFromTemplate = function(self, type)
  local itemTemplateTypeList = {}
  LocalController:instance():visitTable(TableName.Aps_Resource_Item, function(id, lineData)
    local item = ResourceItemTemplate.New()
    item:InitData(lineData)
    if item.id ~= nil and item.itemType == type then
      local isUnlock = self:ResourceItemShowInCapacity(item)
      if isUnlock then
        table.insert(itemTemplateTypeList, item.id)
      end
      if self.itemTemplateDic[item.id] == nil then
        self.itemTemplateDic[item.id] = item
      end
    end
  end)
  return itemTemplateTypeList
end
local ResourceItemShowInCapacity = function(self, item)
  return item and item:ShowInCapacity()
end
local ResourceItemCanBuyInOrder = function(self, itemId)
  local template = self:GetResourceItemTemplate(itemId)
  return template ~= nil and template.price_diamond > 0
end
local GetResourceItemBuyPriceTotal = function(self, itemId, needNum)
  local template = self:GetResourceItemTemplate(itemId)
  if template == nil then
    return false, 0, 0
  end
  local hasNum = self:GetCountByItemId(itemId)
  if needNum > hasNum then
    if 0 >= template.price_diamond then
      return false, 0, 0
    end
    return true, math.ceil(template.price_diamond * (needNum - hasNum)), needNum - hasNum
  end
  return true, 0, 0
end
local CheckResourceItemNum = function(self, itemId, needNum)
  local template = self:GetResourceItemTemplate(itemId)
  if template == nil then
    return false
  end
  local data = self:GetItemDataByItemId(itemId)
  local hasNum = 0
  if data ~= nil then
    hasNum = data.number
  end
  if needNum > hasNum then
    return false
  end
  return true
end
local GetAllLackResourceItemParams = function(self, allItems, action, closeAction)
  local all = {}
  local lackItems = {}
  local result = true
  local totalDiamond = 0
  for k, v in pairs(allItems) do
    local tmpResult, tmpPrice, tmpNum = self:GetResourceItemBuyPriceTotal(k, v)
    totalDiamond = totalDiamond + tmpPrice
    if tmpResult == false then
      result = false
      break
    end
    if 0 < tmpNum then
      lackItems[k] = tmpNum
    end
  end
  all.canBuy = result
  all.totalDiamond = totalDiamond
  all.lackItems = lackItems
  all.action = action
  all.closeAction = closeAction
  return all
end
local CheckIsStorageFull = function(self, addCount)
  return false
end
local DoWhenStorageMaxError = function(self)
  if self.maxErrorMessageFlag ~= false then
    return
  end
  self.maxErrorMessageFlag = true
  local now = UITimeManager:GetInstance():GetServerTime()
  self.lastGetResourceItemTime = now
  SFSNetwork.SendMessage(MsgDefines.UserGetAllResourceItem)
end
local DoWhenStorageMaxErrorHandle = function(self, message)
  self.maxErrorMessageFlag = false
  self:RefreshItemList(message)
end
local CanSendQueueFinishBatchMessage = function(self)
  local now = UITimeManager:GetInstance():GetServerTime()
  if now - self.lastGetResourceItemTime > ERROR_GET_ITEM_TIME_GAP then
    self.maxErrorMessageFlag = false
  end
  if self.maxErrorMessageFlag ~= false then
    return false
  end
  return true
end
local IsHaveCanShopResourceItem = function(self, type)
  local template
  for k, v in pairs(self.itemList) do
    if v.itemType == type and v.number > 0 then
      template = self:GetResourceItemTemplate(v.itemId)
      if template ~= nil and template.show == ResourceItemShowType.Show then
        return true
      end
    end
  end
  return false
end
local GetCountByItemId = function(self, id)
  local info = self:GetItemDataByItemId(id)
  if info ~= nil then
    return info.number
  end
  return 0
end
local GetIconPath = function(self, itemId)
  local template = self:GetResourceItemTemplate(itemId)
  if template == nil or template.pic == nil then
    return ""
  end
  return string.format(LoadPath.ItemPath, template.pic)
end
local GetName = function(self, itemId)
  local template = self:GetResourceItemTemplate(itemId)
  if template == nil or template.name == nil then
    return ""
  end
  return Localization:GetString(template.name)
end
local GetDes = function(self, itemId)
  local template = self:GetResourceItemTemplate(itemId)
  if template == nil or template.name == nil then
    return ""
  end
  return Localization:GetString(template.desc)
end
local GetLeftStorageNum = function(self)
  return self:GetFreezerStorageMax() - self:GetResourceItemTotalNumByType(ResourceItemType.Farming)
end
local GetMaxNumItem = function(self)
  local itemId
  local num = 0
  for k, v in pairs(self.itemList) do
    if num < v.number then
      num = v.number
      itemId = v.itemId
    end
  end
  if itemId then
    return itemId
  end
end
local GetAllTemplate = function(self)
  return self.itemTemplateDic
end
local GetResourceItemCountByTempalteType = function(self, type)
  local count = 0
  table.walk(self.itemList, function(k, v)
    if v.template ~= nil and v.template.type == type then
      count = count + v.number
    end
  end)
  return count
end
local GetResourceItemQuality = function(self, id)
  local template = self:GetResourceItemTemplate(id)
  if template == nil then
    return 0
  end
  return template.quality
end
local GetHeroExpCount = function(self)
  return self:GetCountByItemId(ResourceItemId.HeroExp)
end

function ResourceItemDataManager:RemoveItemByItemId(itemId)
  local itemData = self:GetItemDataByItemId(itemId)
  if itemData ~= nil then
    local uuid = itemData.uuid
    self.itemList[uuid] = nil
  end
end

function ResourceItemDataManager:GetItemUuidsByItemId(itemId)
  local list = {}
  for _, v in pairs(self.itemList) do
    if v.itemId == itemId then
      table.insert(list, v.uuid)
    end
  end
  return list
end

function ResourceItemDataManager:RemoveItemByUuid(uuid)
  self.itemList[uuid] = nil
end

ResourceItemDataManager.__init = __init
ResourceItemDataManager.__delete = __delete
ResourceItemDataManager.Startup = Startup
ResourceItemDataManager.RefreshItemList = RefreshItemList
ResourceItemDataManager.InitRefreshItemList = InitRefreshItemList
ResourceItemDataManager.GetItemDataByUuid = GetItemDataByUuid
ResourceItemDataManager.GetItemDataByItemId = GetItemDataByItemId
ResourceItemDataManager.AddItemNum = AddItemNum
ResourceItemDataManager.UpdateResourceItemMaxValue = UpdateResourceItemMaxValue
ResourceItemDataManager.UpdateMaxValueSignal = UpdateMaxValueSignal
ResourceItemDataManager.GetResourceItemTemplate = GetResourceItemTemplate
ResourceItemDataManager.GetResourceItemListByTypeFromTemplate = GetResourceItemListByTypeFromTemplate
ResourceItemDataManager.RefreshOneItem = RefreshOneItem
ResourceItemDataManager.GetResourceItemTotalNumByType = GetResourceItemTotalNumByType
ResourceItemDataManager.GetSellConfirmFlag = GetSellConfirmFlag
ResourceItemDataManager.SetSellConfirmFlag = SetSellConfirmFlag
ResourceItemDataManager.GetFreezerStorageMax = GetFreezerStorageMax
ResourceItemDataManager.ResourceItemShowInCapacity = ResourceItemShowInCapacity
ResourceItemDataManager.ResourceItemCanBuyInOrder = ResourceItemCanBuyInOrder
ResourceItemDataManager.GetResourceItemBuyPriceTotal = GetResourceItemBuyPriceTotal
ResourceItemDataManager.CheckResourceItemNum = CheckResourceItemNum
ResourceItemDataManager.GetAllLackResourceItemParams = GetAllLackResourceItemParams
ResourceItemDataManager.CheckIsStorageFull = CheckIsStorageFull
ResourceItemDataManager.DoWhenStorageMaxError = DoWhenStorageMaxError
ResourceItemDataManager.DoWhenStorageMaxErrorHandle = DoWhenStorageMaxErrorHandle
ResourceItemDataManager.CanSendQueueFinishBatchMessage = CanSendQueueFinishBatchMessage
ResourceItemDataManager.IsHaveCanShopResourceItem = IsHaveCanShopResourceItem
ResourceItemDataManager.GetCountByItemId = GetCountByItemId
ResourceItemDataManager.GetIconPath = GetIconPath
ResourceItemDataManager.GetName = GetName
ResourceItemDataManager.GetDes = GetDes
ResourceItemDataManager.GetLeftStorageNum = GetLeftStorageNum
ResourceItemDataManager.GetMaxNumItem = GetMaxNumItem
ResourceItemDataManager.GetAllTemplate = GetAllTemplate
ResourceItemDataManager.GetResourceItemCountByTempalteType = GetResourceItemCountByTempalteType
ResourceItemDataManager.GetResourceItemQuality = GetResourceItemQuality
ResourceItemDataManager.GetHeroExpCount = GetHeroExpCount
return ResourceItemDataManager
