local this = class("itemManager")

function this:parseItem(itemType, itemID, guid)
  local data = {}
  if itemType == L_Const.resType.commonItem then
    data = self:parseCommonItem(itemID)
  elseif itemType == L_Const.resType.hero then
    data = self:parseHeroItem(itemID)
  elseif itemType == L_Const.resType.spiritron then
    data = self:parseSpiritronItem(itemID)
  elseif itemType == L_Const.resType.accessory then
    data = self:parseAccessoryItem(itemID)
  elseif itemType == L_Const.resType.homeBuilding then
    data = self:parseBuildingItem(itemID)
  elseif itemType == L_Const.resType.currency then
    data = self:parseCurrencyItem(itemID)
  elseif itemType == L_Const.resType.mount then
    data = self:parseMountItem(itemID, guid)
  elseif itemType == L_Const.resType.petEgg then
    data = self:parsePetEggItem(itemID, guid)
  elseif itemType == L_Const.resType.pet then
    local petItem = L_PetStore:getPetItem(guid)
    data = self:parsePetItem(itemID, petItem and petItem:isSpecialPet() or false)
  elseif itemType == L_Const.resType.playerTitle then
    data = self:parsePlayerTitleData(itemID, guid)
  elseif itemType == L_Const.resType.stargift then
    data = self:parseStarGiftItem(itemID)
  elseif itemType == L_Const.resType.collectionTool then
    data = self:parseCollectionToolItem(itemID)
  elseif itemType == L_Const.resType.mountSaddle then
    data = self:parseMountSaddleItem(itemID)
  end
  if not table.isEmpty(data) then
    data.itemID = itemID
    data.itemType = itemType
  end
  return data
end

function this:parseCommonItem(itemID)
  local commonItemTpl = L_GameTpl:getCommonItemTpl()
  local tpl = commonItemTpl:getTplById(itemID)
  local config = L_Config:getConfig("common_item", itemID)
  local item = L_BagStore:getItem(itemID)
  local quality = commonItemTpl:getRarity(tpl)
  local key = L_Const.qualityToKey[quality]
  local commonItemTypeIconTpl = L_GameTpl:getCommonItemTypeIconTpl()
  local tab = commonItemTpl:getTab(tpl)
  local commonTpl = commonItemTypeIconTpl:getTplById(L_Const.resType.commonItem)
  local type = commonItemTpl:getType(tpl)
  local desc = commonItemTpl:getDesc(tpl)
  local specialDesc = commonItemTpl:getSpecialDesc(tpl)
  return {
    itemType = L_Const.resType.commonItem,
    itemID = itemID,
    name = commonItemTpl:getName(tpl),
    num = item and item.itemNum or 0,
    icon = commonItemTpl:getIcon(tpl),
    quality = quality,
    config = config,
    desc = desc,
    specialDesc = specialDesc,
    imgQuality = string.format("Page/BSCommon/tex_icon_iconbg_%s", key),
    type = type,
    canUseInBag = commonItemTpl:getCanUse(tpl),
    typeName = commonItemTpl:getTypeName(tpl),
    tab = tab,
    inBag = commonItemTpl:getInBag(tpl),
    subId = commonItemTpl:getSubId(tpl),
    imgType = commonItemTypeIconTpl:getIcon(commonTpl)
  }
end

function this:parseHeroItem(itemID)
  local unitID = L_GameUtil.getUnitIdByRole(itemID, L_Const.roleType.hero)
  local heroTpl = L_GameTpl:getHeroTpl()
  local tpl = heroTpl:getTplById(itemID)
  local key = L_Const.qualityToKey[heroTpl:getRarity(tpl)]
  local _, qualityColor = C_ColorUtility.TryParseHtmlString(L_Const.qualityColor[key])
  local _, carrerColor = C_ColorUtility.TryParseHtmlString(L_Const.elementColor[heroTpl:getElement(tpl)])
  return {
    itemType = L_Const.resType.hero,
    itemID = itemID,
    name = heroTpl:getName(tpl),
    icon = heroTpl:getAvatarTexture(tpl, L_Const.avatarTextureIndex.head) or "",
    iconBattle = heroTpl:getAvatarTexture(tpl, L_Const.avatarTextureIndex.head_circle) or "",
    iconHalf = heroTpl:getAvatarTexture(tpl, L_Const.avatarTextureIndex.half_formation) or "",
    iconRaffle = heroTpl:getAvatarTexture(tpl, L_Const.avatarTextureIndex.half_raffle) or "",
    iconFull = heroTpl:getAvatarTexture(tpl, L_Const.avatarTextureIndex.painting) or "",
    quality = heroTpl:getRarity(tpl),
    desc = heroTpl:getDec(tpl),
    unitID = unitID,
    headBg = string.format("Page/BSHeroStar/tex_herorankup_bg_%s", key),
    heroFrame = string.format("Page/BSHeroStar/tex_herorankup_frame_%s", key),
    imgQuality = string.format("Page/BSCommon/tex_icon_iconbg_%s", key),
    qualityColor = qualityColor,
    carrerColor = carrerColor,
    position = heroTpl:getPosition(tpl),
    lifeSkill = heroTpl:getLifeskillDesc(tpl)
  }
end

function this:parseSpiritronItem(itemID)
  local spiritronTpl = L_GameTpl:getSpiritronTpl()
  local tpl = spiritronTpl:getTplById(itemID)
  local key = L_Const.qualityToKey[spiritronTpl:getRarity(tpl)]
  local avatarTexture = spiritronTpl:getAvatarTexture(tpl, 2)
  return {
    itemType = L_Const.resType.spiritron,
    itemID = itemID,
    name = spiritronTpl:getName(tpl),
    icon = avatarTexture,
    iconHalf = avatarTexture,
    quality = spiritronTpl:getRarity(tpl),
    desc = spiritronTpl:getDesc(tpl),
    spinepath = spiritronTpl:getSpinePath(tpl),
    startAnim = spiritronTpl:getStartAnimation(tpl),
    lvBgColor = L_Const.lvBgColor[spiritronTpl:getRarity(tpl)],
    imgQuality = string.format("PageSpiritron/tex_herospiritron_frame_%s", spiritronTpl:getRarity(tpl)),
    imgQuality_small = string.format("BSCommon/tex_icon_iconbg_%s", key)
  }
end

function this:parseAccessoryItem(itemID)
  local accessoryTpl = L_GameTpl:getAccessoryTpl()
  local tpl = accessoryTpl:getTplById(itemID)
  local key = L_Const.qualityToKey[accessoryTpl:getRarity(tpl)]
  return {
    itemType = L_Const.resType.accessory,
    itemID = itemID,
    name = accessoryTpl:getName(tpl),
    icon = accessoryTpl:getIcon(tpl),
    quality = accessoryTpl:getRarity(tpl),
    imgQuality = string.format("Page/BSCommon/tex_icon_iconbg_%s", key),
    imgQuality_info = string.format("Page/BSCommon/tex_item_tip_title_%s", key),
    imgQuality_slot = string.format("Page/BSJewelry/tex_accessory_icon_bg_%s", key),
    type = accessoryTpl:getType(tpl)
  }
end

function this:parseBuildingItem(itemID)
  local buildingTpl = L_GameTpl:getHomeBuildingTpl()
  local tpl = buildingTpl:getTplById(itemID)
  return {
    itemType = L_Const.resType.building,
    itemID = itemID,
    name = buildingTpl:getName(tpl),
    icon = nil,
    desc = buildingTpl:getDesc(tpl),
    level = buildingTpl:getLevel(tpl),
    prosperity = buildingTpl:getProsperity(tpl)
  }
end

function this:parseMountItem(itemID)
  local mountTpl = L_GameTpl:getMountTpl()
  local tpl = mountTpl:getTplById(itemID)
  local icon = mountTpl:getIconByTpl(tpl)
  local name = mountTpl:getNameByTpl(tpl)
  local quality = mountTpl:getQuatityByTpl(tpl)
  local key = L_Const.qualityToKey[quality]
  local desc = mountTpl:getDescByTpl(tpl)
  return {
    itemType = L_Const.resType.mount,
    itemID = itemID,
    name = name,
    icon = icon,
    quality = quality,
    config = tpl,
    desc = desc,
    imgQuality = string.format("BSCommon/tex_icon_iconbg_%s", key)
  }
end

function this:parseCurrencyItem(itemID)
  local currencyTpl = L_GameTpl:getCurrencyTpl()
  local tpl = currencyTpl:getTplById(itemID)
  local key = L_Const.qualityToKey[currencyTpl:getQuality(tpl)]
  return {
    itemType = L_Const.resType.currency,
    itemID = itemID,
    name = currencyTpl:getName(tpl),
    num = L_PlayerStore:getCurrencyNum(itemID),
    icon = currencyTpl:getIcon(tpl),
    quality = currencyTpl:getQuality(tpl),
    imgQuality = string.format("BSCommon/tex_icon_iconbg_%s", key),
    desc = currencyTpl:getDesc(tpl)
  }
end

function this:parsePlayerTitleData(itemID)
  local playerTitleTpl = L_GameTpl:getPlayerTitleTpl()
  local tpl = playerTitleTpl:getTplById(itemID)
  return {
    itemType = L_Const.resType.playertitle,
    itemID = itemID,
    name = playerTitleTpl:getName(tpl),
    icon = playerTitleTpl:getIcon(tpl),
    desc = playerTitleTpl:getDesc(tpl),
    quality = playerTitleTpl:getRarity(tpl),
    special = playerTitleTpl:getSpecial(tpl),
    showType = playerTitleTpl:getType(tpl)
  }
end

function this:parsePetEggItem(itemID, guid)
  local petEggTpl = L_GameTpl:getPetEggTpl()
  local tpl = petEggTpl:getTplById(itemID)
  local icon = petEggTpl:getEggImgIcon(tpl)
  local name = L_WordsTpl:getValue("ui_itemManager")
  local quality = 1
  local key = L_Const.quality.n
  local desc = ""
  return {
    itemType = L_Const.resType.petEgg,
    itemID = itemID,
    name = name,
    num = 1,
    icon = icon,
    quality = quality,
    config = tpl,
    desc = desc,
    imgQuality = string.format("Temp/tex_icon_raritycirclet_%s", key),
    dropType = L_Const.dropItemType.Crystal,
    dropImg = petEggTpl:getEggImgDrop(tpl)
  }
end

function this:parsePetItem(itemId, isSpecial)
  local id = itemId
  local petInfoTpl = L_GameTpl:getPetTpl()
  local tpl = petInfoTpl:getTplById(id)
  if table.isEmpty(tpl) then
    return {}
  end
  local petIcon = petInfoTpl:getPetIcon(tpl, isSpecial)
  local petName = petInfoTpl:getName(tpl, isSpecial)
  return {
    itemType = L_Const.resType.pet,
    itemID = itemId,
    name = petName,
    icon = petIcon,
    num = 1,
    quality = 1,
    imgQuality = "BSCommon/tex_icon_iconbg_n",
    desc = petName,
    dropType = L_Const.dropItemType.Crystal
  }
end

function this:parseEnemyItem(itemID)
  local dungeonEnemyTpl = L_GameTpl:getDungeonEnemyTpl()
  local tpl = dungeonEnemyTpl:getTplById(itemID)
  return {
    name = dungeonEnemyTpl:getName(tpl),
    icon = dungeonEnemyTpl:getAvatarTexture(tpl),
    desc = dungeonEnemyTpl:getDescription(tpl)
  }
end

function this:parseStarGiftItem(itemID)
  local starGiftTpl = L_GameTpl:getStargiftTpl()
  local tpl = starGiftTpl:getTplById(itemID)
  return {
    itemType = L_Const.resType.stargift,
    itemID = itemID,
    name = starGiftTpl:getName(tpl),
    icon = starGiftTpl:getTexture(tpl, L_Const.avatarTextureIndex.head),
    quality = starGiftTpl:getRarity(tpl),
    config = tpl,
    desc = starGiftTpl:getDec(tpl)
  }
end

function this:parseCollectionToolItem(itemID)
  local collectionToolTpl = L_GameTpl:getCollectionToolTpl()
  local tpl = collectionToolTpl:getTplById(itemID)
  return {
    itemType = L_Const.resType.collectionTool,
    itemID = itemID,
    name = collectionToolTpl:getName(tpl),
    icon = collectionToolTpl:getIcon(tpl),
    quality = collectionToolTpl:getRarity(tpl),
    config = tpl,
    desc = collectionToolTpl:getDesc(tpl)
  }
end

function this:parseMountSaddleItem(itemID)
  local mountSaddleTpl = L_GameTpl:getMountSaddleTpl()
  local tpl = mountSaddleTpl:getTplById(itemID)
  return {
    itemType = L_Const.resType.mountSaddle,
    itemID = itemID,
    name = mountSaddleTpl:getName(tpl),
    icon = mountSaddleTpl:getIcon(tpl),
    quality = mountSaddleTpl:getRarity(tpl),
    config = tpl,
    desc = mountSaddleTpl:getDesc(tpl)
  }
end

return this
