local this = class("itemManager")
local heroTpl = L_GameTpl:getHeroTpl()
local commonItemTpl = L_GameTpl:getCommonItemTpl()
local petInfoTpl = L_GameTpl:getPetTpl()
local unitTpl = L_GameTpl:getUnitTpl()
local spiritronTpl = L_GameTpl:getSpiritronTpl()
local accessoryTpl = L_GameTpl:getAccessoryTpl()
local taskItemTpl = L_GameTpl:getTaskItemTpl()
local buildingTpl = L_GameTpl:getHomeBuildingTpl()
local mountTpl = L_GameTpl:getMountTpl()
local currencyTpl = L_GameTpl:getCurrencyTpl()
local playerTitleTpl = L_GameTpl:getPlayerTitleTpl()
local petEggTpl = L_GameTpl:getPetEggTpl()
local petEggAppearanceTpl = L_GameTpl:getPetEggAppearanceTpl()
local rogueEchoinfoTpl = L_GameTpl:getRogueEchoinfoTpl()
local rogueInfoinfoTpl = L_GameTpl:getRogueInfoinfoTpl()
local rogueBossTpl = L_GameTpl:getRogueBossTpl()
local enemyTpl = L_GameTpl:getEnemyTpl()
local starGiftTpl = L_GameTpl:getStargiftTpl()
local goodsTpl = L_GameTpl:getGoodsTpl()
local mountSaddleTpl = L_GameTpl:getMountSaddleTpl()
local rogueGoodsTpl = L_GameTpl:getRogueGoodsTpl()
local collectionToolTpl = L_GameTpl:getCollectionToolTpl()
local playerDressTpl = L_GameTpl:getPlayercardDressTpl()
local Type2Func = {
  [L_Const.resType.hero] = "getHeroItem",
  [L_Const.resType.commonItem] = "getCommonItem",
  [L_Const.resType.pet] = "getPetItem",
  [L_Const.resType.spiritron] = "getSpiritronItem",
  [L_Const.resType.currency] = "getCurrencyItem",
  [L_Const.resType.mount] = "getMountItem",
  [L_Const.resType.homeBuilding] = "getBuildingItem",
  [L_Const.resType.petEgg] = "getGeneralPetEggItem",
  [L_Const.resType.accessory] = "getAccessoryItem",
  [L_Const.resType.playerTitle] = "getPlayerTitleData",
  [L_Const.resType.stargift] = "getStarGiftData",
  [L_Const.resType.rogueEcho] = "getRogueEcho",
  [L_Const.resType.rogueInfo] = "getRogueInfo",
  [L_Const.resType.rogueBoss] = "getRogueBoss",
  [L_Const.resType.enemy] = "getEnemyItem",
  [L_Const.resType.goods] = "getGoodsItem",
  [L_Const.resType.rogueGoods] = "getRogueGoodsItem",
  [L_Const.resType.rogueCurrency] = "getRogueCurrency",
  [L_Const.resType.taskItem] = "getTaskItem",
  [L_Const.resType.collectionTool] = "getCollectionToolItem",
  [L_Const.resType.mountSaddle] = "getMountSaddleItem",
  [L_Const.resType.playerDress] = "getPlayerDressItem"
}

function this:getItemConfig(itemType, itemID, params)
  local data
  params = params or {}
  if Type2Func[itemType] and self[Type2Func[itemType]] then
    data = self[Type2Func[itemType]](self, itemID, params)
  else
    data = {}
  end
  data.itemType = itemType
  data.itemId = itemID
  data.num = self:getItemNum(itemType, itemID)
  return data or {}
end

function this:getHeroItem(itemID)
  local tpl = heroTpl:getTplById(itemID)
  return {
    name = heroTpl:getName(tpl),
    icon_large = heroTpl:getAvatarTexture(tpl, L_Const.resourceIndexOfAvatarTextureInHeroTpl.large),
    icon_mid = heroTpl:getAvatarTexture(tpl, L_Const.resourceIndexOfAvatarTextureInHeroTpl.mid) or "",
    icon_small = heroTpl:getAvatarTexture(tpl, L_Const.resourceIndexOfAvatarTextureInHeroTpl.small) or "",
    quality = heroTpl:getRarity(tpl),
    desc = heroTpl:getDec(tpl)
  }
end

function this:getStarGiftData(itemID)
  local tpl = starGiftTpl:getTplById(itemID)
  return {
    name = starGiftTpl:getName(tpl),
    icon = starGiftTpl:getTexture(tpl, L_Const.avatarTextureIndex.head),
    quality = starGiftTpl:getRarity(tpl),
    desc = starGiftTpl:getDec(tpl)
  }
end

function this:getCommonItem(itemID)
  local tpl = commonItemTpl:getTplById(itemID)
  if not tpl then
    errorf("commonItem\232\161\168\233\135\140\228\184\141\229\173\152\229\156\168\232\175\165itemId = " .. tostring(itemID))
    return nil
  end
  return {
    name = commonItemTpl:getName(tpl),
    icon = commonItemTpl:getIcon(tpl),
    quality = commonItemTpl:getRarity(tpl),
    desc = commonItemTpl:getDesc(tpl)
  }
end

function this:getPetItem(petConfigId, isSpecial)
  isSpecial = type(isSpecial) ~= "boolean" and false
  local tpl = petInfoTpl:getTplById(petConfigId)
  return {
    name = petInfoTpl:getName(tpl, isSpecial),
    icon = petInfoTpl:getPetPixelIcon(tpl, isSpecial),
    quality = 4,
    desc = petInfoTpl:getName(tpl, isSpecial)
  }
end

function this:getBuildingItem(itemID)
  local tpl = buildingTpl:getTplById(itemID)
  return {
    name = buildingTpl:getName(tpl),
    icon = buildingTpl:getBuildIcon(tpl),
    desc = buildingTpl:getDesc(tpl),
    quality = 0
  }
end

function this:getSpiritronItem(itemID)
  local tpl = spiritronTpl:getTplById(itemID)
  return {
    name = spiritronTpl:getName(tpl),
    icon = spiritronTpl:getAvatarTexture(tpl, 2),
    quality = spiritronTpl:getRarity(tpl),
    desc = spiritronTpl:getDesc(tpl),
    reishiExp = spiritronTpl:getReishiExp(tpl)
  }
end

function this:getCurrencyItem(itemID)
  local tpl = currencyTpl:getTplById(itemID)
  return {
    name = currencyTpl:getName(tpl),
    icon = currencyTpl:getIcon(tpl),
    smallIcon = currencyTpl:getIconSmall(tpl),
    quality = currencyTpl:getQuality(tpl),
    desc = currencyTpl:getDesc(tpl)
  }
end

function this:getMountItem(itemID)
  local tpl = mountTpl:getTplById(itemID)
  return {
    name = mountTpl:getNameByTpl(tpl),
    icon = mountTpl:getIconByTpl(tpl),
    quality = mountTpl:getQuatityByTpl(tpl),
    desc = mountTpl:getDescByTpl(tpl)
  }
end

function this:getGeneralPetEggItem(itemID)
  local tpl = petEggTpl:getTplById(itemID)
  return {
    name = petEggTpl:getName(tpl),
    icon = petEggTpl:getEggImgIcon(tpl),
    quality = 1,
    desc = petEggTpl:getDesc(tpl)
  }
end

function this:getPetEggItem(itemID, guid)
  local tpl = petEggTpl:getTplById(itemID)
  local petEggData = L_PetStore:getPetEggItem(guid)
  return {
    name = petEggData.type,
    icon = petEggTpl:getEggImgIcon(tpl),
    quality = 1,
    desc = petEggTpl:getDesc(tpl)
  }
end

function this:getAccessoryItem(itemID)
  local tpl = accessoryTpl:getTplById(itemID)
  if tpl == nil then
    errorf("\230\178\161\230\156\137\230\137\190\229\136\176\233\133\141\231\189\174\230\149\176\230\141\174, Accessory id = " .. tostring(itemID))
    return {}
  end
  return {
    name = accessoryTpl:getName(tpl),
    icon = accessoryTpl:getIcon(tpl),
    quality = accessoryTpl:getRarity(tpl),
    desc = accessoryTpl:getDesc(tpl)
  }
end

function this:getPlayerTitleData(itemID)
  local tpl = playerTitleTpl:getTplById(itemID)
  return {
    name = playerTitleTpl:getName(tpl),
    icon = playerTitleTpl:getIcon(tpl),
    desc = playerTitleTpl:getDesc(tpl),
    quality = playerTitleTpl:getRarity(tpl)
  }
end

function this:getRogueEcho(itemId)
  local tpl = rogueEchoinfoTpl:getTplById(itemId)
  return {
    name = rogueEchoinfoTpl:getName(tpl),
    icon = rogueEchoinfoTpl:getIcon(tpl),
    desc = rogueEchoinfoTpl:getFunctionDes(tpl),
    quality = rogueEchoinfoTpl:getRarity(tpl)
  }
end

function this:getRogueInfo(itemId)
  local tpl = rogueInfoinfoTpl:getTplById(itemId)
  return {
    name = rogueInfoinfoTpl:getName(tpl),
    icon = rogueInfoinfoTpl:getIcon(tpl),
    desc = rogueInfoinfoTpl:getFunctionDes(tpl),
    quality = rogueInfoinfoTpl:getRarity(tpl)
  }
end

function this:getRogueBoss(itemId)
  local tpl = rogueBossTpl:getTplById(itemId)
  return {
    name = rogueBossTpl:getBossName(tpl),
    icon = nil,
    desc = "",
    quality = 1
  }
end

function this:getEnemyItem(itemId)
  local tpl = enemyTpl:getTplById(itemId)
  return {
    name = enemyTpl:getName(tpl),
    icon = enemyTpl:getAvatarTexture(tpl),
    desc = enemyTpl:getDesc(tpl),
    quality = nil
  }
end

function this:getGoodsItem(itemId)
  local tpl = goodsTpl:getTplById(itemId)
  return {
    name = goodsTpl:getGoodsName(tpl),
    icon = goodsTpl:getIcon(tpl),
    desc = "",
    quality = goodsTpl:getRarity(tpl)
  }
end

function this:getRogueGoodsItem(itemId)
  local tpl = rogueGoodsTpl:getTplById(itemId)
  return {
    name = rogueGoodsTpl:getGoodsName(tpl),
    icon = rogueGoodsTpl:getIcon(tpl),
    desc = "",
    quality = rogueGoodsTpl:getRarity(tpl)
  }
end

function this:getRogueCurrency(itemId)
  if itemId == L_RogueConst.currencyType.virtualGold then
    return {
      name = L_WordsTpl:getValue("ui_rogueManager_37"),
      icon = "Rogue/tex_rougeingame_icon_xujin",
      desc = "",
      quality = nil
    }
  end
end

function this:getTaskItem(itemID)
  local tpl = taskItemTpl:getTplById(itemID)
  return {
    name = taskItemTpl:getName(tpl),
    icon = taskItemTpl:getIcon(tpl),
    quality = taskItemTpl:getRarity(tpl),
    desc = taskItemTpl:getDesc(tpl)
  }
end

function this:getMountSaddleItem(itemID)
  local tpl = mountSaddleTpl:getTplById(itemID)
  return {
    name = mountSaddleTpl:getName(tpl),
    icon = mountSaddleTpl:getIcon(tpl),
    quality = mountSaddleTpl:getRarity(tpl),
    desc = mountSaddleTpl:getDesc(tpl)
  }
end

function this:getCollectionToolItem(itemId)
  local tpl = collectionToolTpl:getTplById(itemId)
  return {
    name = collectionToolTpl:getName(tpl),
    icon = collectionToolTpl:getIcon(tpl),
    quality = collectionToolTpl:getRarity(tpl),
    desc = collectionToolTpl:getDesc(tpl)
  }
end

function this:getPlayerDressItem(itemId)
  local tpl = playerDressTpl:getTplById(itemId)
  return {
    name = playerDressTpl:getName(tpl),
    icon = playerDressTpl:getIcon(tpl),
    quality = playerDressTpl:getRarity(tpl),
    desc = playerDressTpl:getDesc(tpl)
  }
end

function this:getItemNum(type, id)
  if type == L_Const.resType.commonItem then
    return L_BagStore:getItemNum(id)
  elseif type == L_Const.resType.currency then
    return L_PlayerStore:getCurrencyNum(id)
  elseif type == L_Const.resType.accessory then
    return L_AccessoryManager:getAccessoryNum(id)
  elseif type == L_Const.resType.petEgg then
    return L_PetStore:getPetEggNum(id)
  elseif type == L_Const.resType.pet then
    return L_PetStore:getPetNum(id)
  elseif type == L_Const.resType.hero then
    return L_HeroStore:getHasHero(id) and 1 or 0
  elseif type == L_Const.resType.spiritron then
    return L_SpiritronStore:getSpiritronNum(id)
  elseif type == L_Const.resType.collectionTool then
    return L_CollectionToolStore:getNumById(id)
  elseif type == L_Const.resType.mountSaddle then
    return AzurWorld.mountMgr:GetIdMountSaddleUnlock(id) and 1 or 0
  end
  return L_BagStore:getItemNum(id)
end

function this:showInfoTip(itemType, itemId, guid)
  if itemType == L_Const.resType.hero and guid == nil then
    local entityId = itemId
    local list = {}
    table.insert(list, entityId)
    L_UI:open("pageHero", {
      heroIdList = list,
      pageType = L_Const.heroPageType.PreviewMode
    })
    return
  end
  if itemType == L_Const.resType.spiritron and guid == nil then
    local entityId = itemId
    local list = {}
    table.insert(list, entityId)
    L_UI:open("pageHero", {
      spiritronId = itemId,
      heroIdList = list,
      pageType = L_Const.heroPageType.SpritronPreviewMode
    })
    return
  end
  if itemType == L_Const.resType.pet then
    L_UI:open("pagePetPreview", {configId = itemId})
    return
  end
  if itemType == L_Const.resType.accessory then
    L_UI:open("pageAccessoryInfoTip", {guid = guid, itemId = itemId})
    return
  end
  L_UI:open("pageInfoTip", {
    itemType = itemType,
    itemId = itemId,
    guid = guid
  })
end

function this:showInfoTipSelectNum(itemType, itemId, guid, curNum, maxNum, selectNumCB)
  if itemType == L_Const.resType.accessory then
    L_UI:open("pageAccessoryInfoTip", {guid = guid, itemId = itemId})
    return
  end
  L_UI:open("pageInfoTip", {
    itemType = itemType,
    itemId = itemId,
    guid = guid,
    curNum = curNum,
    maxNum = maxNum,
    selectNumCB = selectNumCB
  })
end

return this
