local WeaponInfoComp = class("WeaponInfoComp", LuaComponent)
local BTN_ICON = {"common_btn_green_2", "common_btn_grey_2"}
local DEFAULT_FACTOR = GConst.BattleConst.DEFAULT_FACTOR
-- 升段
local EFFECT_UP_SECTION = "assets/prefabs/effects/ui/vfs_ui_wuqi_up_b01.prefab"
-- 升级
local EFFECT_UP_GRADE = "assets/prefabs/effects/ui/vfs_ui_wuqi_up_b02.prefab"
-- 常驻
local EFFECT_FIX = {
    "assets/prefabs/effects/ui/vfs_ui_wuqi_changzhu_b01.prefab",
    "assets/prefabs/effects/ui/vfs_ui_wuqi_changzhu_b02.prefab",
    "assets/prefabs/effects/ui/vfs_ui_wuqi_changzhu_b03.prefab",
}

function WeaponInfoComp:init()
    local uiMap = self:getUIMap()

    self.txName = uiMap["weapon_info.name.tx_name"]
    self.imgWeapon = uiMap["weapon_info.img_weapon"]
    self.rootEffect = uiMap["weapon_info.root_effect"]
    self.txLevel = uiMap["weapon_info.level.tx_level"]
    self.txDesc1 = uiMap["weapon_info.tx_desc_1"]
    self.txDesc2 = uiMap["weapon_info.tx_desc_2"]
    self.attrContent = uiMap["weapon_info.ScrollView.Viewport.Content"]
    self.txAttr = uiMap["weapon_info.ScrollView.Viewport.Content.tx_attr"]
    self.btnUp = uiMap["weapon_info.upgrade.btn_up"]
    self.txUp = uiMap["weapon_info.upgrade.btn_up.tx_desc"]
    self.txNum = uiMap["weapon_info.upgrade.btn_up.tx_num"]
    self.attr = {}
    for i = 1, 2 do
        self.attr[i] = uiMap["weapon_info.attr_" .. i]
    end
    self.canvasWeapon = self.imgWeapon:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS)

    self.txDesc1:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_4))
    self.txDesc2:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_5))
    self.txUp:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_3))

    DataManager.ShopData:checkPopGift(PayManager.PURCHARSE_ACT_TYPE.WEAPON_GIFT)

    self.btnUp:addClickListener(function()
        ModuleManager.EquipManager:reqUpgrade(self.heroEntity:getCfgId(), GConst.EquipConst.PART_TYPE.WEAPON)
    end)
end

function WeaponInfoComp:setUI(ui)
    self.uiRoot = ui
end

function WeaponInfoComp:setHeroData(heroEntity)
    self.heroEntity = heroEntity
    self.weaponEntity = DataManager.EquipData:getEquip(self.heroEntity:getCfgId(), GConst.EquipConst.PART_TYPE.WEAPON)
    self:bind(self.weaponEntity, "isDirty", function()
        self:refresh()
    end)
    self:bind(DataManager.BagData.ItemData, "dirty", function()
        self:refresh()
    end)
    self:playEffect()
end

function WeaponInfoComp:refresh()
    local nextWeaponEntity = self.weaponEntity:getNextLevelEntity()

    self.txName:setText(self.weaponEntity:getName())
    self.imgWeapon:setTexture("assets/arts/textures/background/weapon/".. self.weaponEntity:getIconId() .. ".png")
    self.txLevel:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_3) .. self.weaponEntity:getLevel().."/"..self.weaponEntity:getMaxLevel())
    self.txAttr:setText(self.weaponEntity:getAttrDesc())
    local height = self.txAttr:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredHeight
    self.attrContent:setSizeDeltaY(height + 5)
	self.attrContent:setAnchoredPosition(0, 0)

    -- 基础属性
    local curAtk = self.weaponEntity:getAttack()
    local curNormalHurt = self.weaponEntity:getNormalHurt()
    local curSkillHurt = self.weaponEntity:getSkillHurt()
    local curHp = self.weaponEntity:getHp()
    local diffBaseAtk = (nextWeaponEntity:getBaseAttack() - self.weaponEntity:getBaseAttack()) // DEFAULT_FACTOR
    local diffBaseNormalHurt = (nextWeaponEntity:getNormalHurt() - self.weaponEntity:getNormalHurt()) // DEFAULT_FACTOR
    local diffBaseSkillHurt = (nextWeaponEntity:getSkillHurt() - self.weaponEntity:getSkillHurt()) // DEFAULT_FACTOR
    local diffBaseHp = (nextWeaponEntity:getBaseHp() - self.weaponEntity:getBaseHp()) // DEFAULT_FACTOR
    local showAttrType = {}
    for index, obj in ipairs(self.attr) do
        local map = obj:genAllChildren()
        local imgIcon = map["img_icon"]
        local txTitle = map["tx_title"]
        local txNum = map["tx_num"]
        obj:setVisible(true)

        if not table.containValue(showAttrType, GConst.MATCH_ATTACK_NAME[self.heroEntity:getMatchType()]) and (curAtk > 0 or diffBaseAtk > 0) then
            table.insert(showAttrType, GConst.MATCH_ATTACK_NAME[self.heroEntity:getMatchType()])
            imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_5")
            txTitle:setText("<color=#FCB501>"..I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_3).."</color>")
            local numStr = curAtk // DEFAULT_FACTOR
            if diffBaseAtk > 0 then
                numStr = numStr .. "<color=#A2FF29>+" .. diffBaseAtk .. "</color>"
            end
            txNum:setText(numStr)
        elseif not table.containValue(showAttrType, GConst.MATCH_NORMAL_HURT_NAME[self.heroEntity:getMatchType()]) and (curNormalHurt > 0 or diffBaseNormalHurt > 0) then
            table.insert(showAttrType, GConst.MATCH_NORMAL_HURT_NAME[self.heroEntity:getMatchType()])
            imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_20")
            txTitle:setText("<color=#4CCFFA>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_NORMAL_HURT).."</color>")
            local numStr = curNormalHurt // DEFAULT_FACTOR
            if diffBaseNormalHurt > 0 then
                numStr = numStr .. "<color=#A2FF29>+" .. diffBaseNormalHurt .. "</color>"
            end
            txNum:setText(numStr)
        elseif not table.containValue(showAttrType, GConst.MATCH_SKILL_HURT_NAME[self.heroEntity:getMatchType()]) and (curSkillHurt > 0 or diffBaseSkillHurt > 0) then
            table.insert(showAttrType, GConst.MATCH_SKILL_HURT_NAME[self.heroEntity:getMatchType()])
            imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_21")
            txTitle:setText("<color=#EC80FF>"..I18N:getGlobalText(I18N.GlobalConst.ATTR_SKILL_HURT).."</color>")
            local numStr = curSkillHurt // DEFAULT_FACTOR
            if diffBaseSkillHurt > 0 then
                numStr = numStr .. "<color=#A2FF29>+" .. diffBaseSkillHurt .. "</color>"
            end
            txNum:setText(numStr)
        elseif not table.containValue(showAttrType, GConst.MATCH_HP_NAME[self.heroEntity:getMatchType()]) and (curHp > 0 or diffBaseHp > 0) then
            table.insert(showAttrType, GConst.MATCH_HP_NAME[self.heroEntity:getMatchType()])
            imgIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_dec_4")
            txTitle:setText("<color=#FB6895>"..I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_2).."</color>")
            local numStr = curHp // DEFAULT_FACTOR
            if diffBaseHp > 0 then
                numStr = numStr .. "<color=#A2FF29>+" .. diffBaseHp .. "</color>"
            end
            txNum:setText(numStr)
        else
            obj:setVisible(false)
        end
    end

    -- 消耗材料
    local cost = self.weaponEntity:getUpgradeMaterials()
    for i = 1, 3 do
        local uiMap = self:getUIMap()
        local costNode = uiMap["weapon_info.upgrade.cost.cost_" .. i]
        if cost[i] then
            costNode:setActive(true)
            local icon = uiMap["weapon_info.upgrade.cost.cost_" .. i .. ".img_icon"]
            local num = uiMap["weapon_info.upgrade.cost.cost_" .. i .. ".tx_num"]
            local add = uiMap["weapon_info.upgrade.cost.cost_" .. i .. ".img_icon.img_add"]

            local costId = GFunc.getRewardId(cost[i])
            local costNum = GFunc.getRewardNum(cost[i])
            local haveNum = DataManager.BagData.ItemData:getItemNumById(costId)
            if haveNum < costNum then
                num:setText("<color=#FF5252>" .. haveNum .. "</color>/" .. costNum)
            else
                num:setText(haveNum .. "/" .. costNum)
            end
            local atlas, name = GFunc.getIconRes(costId)
            icon:setSprite(atlas, name, function ()
                icon:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):SetNativeSize()
            end)
            add:setActive(costNum - haveNum > 0)
            icon:addClickListener(function()
                ModuleManager.EquipManager:showItemGetPop(self.weaponEntity:getHeroId(), self.weaponEntity:getPart(), costId, costNum - haveNum < 0 and 0 or costNum - haveNum)
            end)
        else
            costNode:setActive(false)
        end
    end
    if self.weaponEntity:isEnoughGold() then
        self.txNum:setText(self.weaponEntity:getUpgradeGoldNum())
    else
        self.txNum:setText("<color=#FF5252>" .. self.weaponEntity:getUpgradeGoldNum() .. "</color>")
    end

    self.btnUp:setTouchEnable(self.heroEntity:isActived())
    local canLvUp = self.weaponEntity:canLevelUp()
    if self.heroEntity:isActived() and canLvUp then
        self.btnUp:addRedPoint(120, 50, 0.6)
        self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
    else
        self.btnUp:removeRedPoint()
        self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[2])
    end
end

-- 播放动效
function WeaponInfoComp:playEffect(isUpgrade, isUpSection)
    self.rootEffect:removeAllChildren()

    if isUpgrade or isUpSection then
        AudioManager:playEffect(AudioManager.EFFECT_ID.EQUIP_WEAPON_UP)
    end
    -- 升级
    if isUpgrade then
        EffectManager:loadUIEffectAsync(EFFECT_UP_GRADE, self.uiRoot, self.rootEffect, GConst.UI_EFFECT_ORDER.LEVEL5, function(obj)
            obj:play()
        end)
    end
    -- 升段
    if isUpSection then
        EffectManager:loadUIEffectAsync(EFFECT_UP_SECTION, self.uiRoot, self.rootEffect, GConst.UI_EFFECT_ORDER.LEVEL5, function(obj)
            obj:play()
        end)
    end
    -- 常驻特效
    local effectIdx = math.ceil(self.weaponEntity:getStage() / 2)
    if EFFECT_FIX[effectIdx] then
        EffectManager:loadUIEffectAsync(EFFECT_FIX[effectIdx], self.uiRoot, self.rootEffect, GConst.UI_EFFECT_ORDER.LEVEL1, function(obj)
            if self.canvasWeapon and self.uiRoot then
                self.canvasWeapon.overrideSorting = true
                self.canvasWeapon.sortingOrder = self.uiRoot:getUIOrder() + GConst.UI_EFFECT_ORDER.LEVEL1
            end
            obj:setIsLoop(true)
            obj:play()
        end)
    end
end

return WeaponInfoComp