local this = class("pageBagUseFood", G_UIPageBase)
local foodTpl = L_GameTpl:getFoodTpl()
local wordsTpl = L_GameTpl:getWordsTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {
    curItemId = nil,
    curNum = 1,
    selectHeroId = nil,
    curFormationType = nil,
    useCondition = {hpOverFlow = false},
    curFoodType = nil,
    curFood = nil
  }
end

function this.bind()
  return {
    scrollList_hero = {
      moduleName = "pages/Bag/BagUse/cellUseFoodHero"
    },
    module_selectNum = L_Const.ModuleInfo.SelectNum,
    desc = "",
    moduleCommonTipMedium = L_Const.ModuleInfo.CommonTipMedium
  }
end

function this.methods()
  return {
    moduleCommonTipMedium = {
      onClick_close = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_cancel = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_confirm = function(self)
        if self.data.curFoodType == L_Const.foodType.heal then
          self:use_hp()
        elseif self.data.curFoodType == L_Const.foodType.reborn then
          self:use_reborn()
        elseif self.data.curFoodType == L_Const.foodType.defence or self.data.curFoodType == L_Const.foodType.attack then
          self:use_attackOrDefence()
        elseif self.data.curFoodType == L_Const.foodType.adventure then
          self:use_adventure()
        end
      end,
      onClick_bg = function(self)
        L_UI:close(self.pageName)
      end
    },
    scrollList_hero = {
      onClick_selectHero = function(self, id)
        self.data.selectHeroId = id
        self:setHeroListActive()
        self.modules.module_selectNum:changeNum(1)
        L_AudioUtil.playSound("Play_SFX_System_UI_Bag_Food_CHAR_Click")
      end,
      onClick_charge = function(self)
        self.data.selectHeroId = 0
        self:setHeroListActive()
        self.modules.module_selectNum:changeNum(1)
      end
    }
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.data.curItemId = options.itemId
  self.data.curNum = 1
  self.data.curFormationType = L_FormationManager:getCurFormationType()
  local food = foodTpl:getTplById(options.itemId)
  self.data.curFoodType = food.foodType
  self.data.curFood = food
  local satiety = foodTpl:getSatietyScore(food)
  local tpl_commonItem = _commonItemTpl:getTplById(self.data.curItemId)
  self.bind.desc = string.concat(L_WordsTpl:getValue("ui_pet_food_desc"), satiety, "\227\128\130", _commonItemTpl:getDesc(tpl_commonItem))
  if food.foodType == L_Const.foodType.heal then
    self:initHeroList(food)
  elseif food.foodType == L_Const.foodType.reborn then
    self:initHeroList(food)
  elseif food.foodType == L_Const.foodType.defence or food.foodType == L_Const.foodType.attack then
    self:initHeroList(food)
  elseif food.foodType == L_Const.foodType.adventure then
  end
  self:initSelectNumModule()
  self:initCommonTipModule()
end

function this:initSelectNumModule()
  local max = function()
    return L_BagManager:getMaxCanUseNum_food(self.data.curItemId, self.data.selectHeroId)
  end
  local func = function(num)
    self.data.curNum = num
    self:refreshNum()
  end
  self.modules.module_selectNum:initModule(func, max)
  func(self.data.curNum)
end

function this:initHeroList(food)
  local temp_list = {}
  if not table.isEmpty(food.foodCharge) then
    local posData = L_FormationStore:getFormationPosData(self.data.curFormationType)
    for i, heroGuid in pairs(posData) do
      if heroGuid ~= 0 then
        local hero = L_HeroStore:getHero(heroGuid)
        local item = L_ItemManager:parseHeroItem(L_HeroStore:getHeroConfigId(hero))
        local imgHeroIcon = L_GameUtil.getAvatarTexturePath(L_HeroStore:getHeroConfigId(hero), L_Const.resourceIndexOfAvatarTextureInHeroTpl.mid)
        local hp_max = L_AttributeManager:getAttValueByHeroGuid(heroGuid, L_Const.battleAtt.MAXHP)
        local hp = L_FormationStore:getFormationHeroHp(heroGuid)
        if not self.data.selectHeroId and hp_max > hp then
          self.data.selectHeroId = heroGuid
        end
        local cell = {
          heroId = heroGuid,
          configData = item,
          img_icon = imgHeroIcon,
          scale_hp = hp / hp_max,
          hpText = string.format("%d/%d", hp, hp_max),
          scale_add = 0
        }
        if heroGuid == L_HeroStore:getDefaultHeroGuid() then
          cell.url_defaultHero = L_PlayerStore:getAvatarTexturePath(L_Const.avatarTextureIndex.head_talk)
        end
        table.insert(temp_list, cell)
      end
    end
  end
  if self.data.curFoodType == L_Const.foodType.heal or self.data.curFoodType == L_Const.foodType.attack or self.data.curFoodType == L_Const.foodType.defence then
    local charge = food.charge or 0
    local chargeData = L_StarHealingStore:getCharge()
    local percentage = (chargeData.cur_value + charge) / L_StarHealingStore:getMaxCharge()
    local addValue = 1 <= percentage and 1 or percentage
    table.insert(temp_list, {
      heroId = nil,
      scale_hp = chargeData.cur_value / L_StarHealingStore:getMaxCharge(),
      scale_add = addValue
    })
  end
  if not self.data.selectHeroId then
    self.data.selectHeroId = temp_list[1].heroId or 0
  end
  self.bind.scrollList_hero:clear()
  self.bind.scrollList_hero:insert_array(temp_list)
  self:setHeroListActive()
end

function this:setHeroListActive()
  for i = 1, #self.bind.scrollList_hero do
    local isActive = false
    local heroId = self.bind.scrollList_hero:getValue(i, "heroId")
    if not math.isEmpty(heroId) then
      if heroId == self.data.selectHeroId then
        isActive = true
      end
    elseif heroId == nil and self.data.selectHeroId == 0 then
      isActive = true
    end
    local cellUseFoodHero = self.bind.scrollList_hero:getItemCls(i)
  end
end

function this:refreshNum()
  if self.data.curFoodType == L_Const.foodType.heal then
    self:refreshNum_hp()
  elseif self.data.curFoodType == L_Const.foodType.reborn then
    self:refreshNum_reborn()
  elseif self.data.curFoodType == L_Const.foodType.defence or self.data.curFoodType == L_Const.foodType.attack then
    self:refreshNum_hp()
  elseif self.data.curFoodType == L_Const.foodType.adventure then
    self:refreshNum_adventure()
  end
end

function this:refreshNum_hp()
  local item = L_ItemManager:parseCommonItem(self.data.curItemId)
  for i = 1, #self.bind.scrollList_hero do
    local scale = 0
    local oneItemFunc = 0
    local tpl = foodTpl:getTplById(self.data.curItemId)
    local heroId = self.bind.scrollList_hero:getValue(i, "heroId")
    if not math.isEmpty(heroId) and heroId == self.data.selectHeroId then
      local hp_max = L_AttributeManager:getAttValueByHeroGuid(self.data.selectHeroId, L_Const.battleAtt.MAXHP)
      if self.data.selectHeroId == 0 then
        oneItemFunc = foodTpl:getCharge(tpl) or 0
      else
        local foodCharge = foodTpl:getFoodCharge(tpl)
        for i, v in ipairs(foodCharge) do
          if v[1] == L_Const.HPRechargeType.HPRT_VALUE then
            oneItemFunc = v[2] + oneItemFunc
          else
            oneItemFunc = v[2] / 10000 * hp_max + oneItemFunc
          end
        end
      end
      local hp = L_FormationStore:getFormationHeroHp(self.data.selectHeroId)
      self.data.useCondition.hpOverFlow = hp_max < hp + self.data.curNum * oneItemFunc
      scale = math.clamp((hp + self.data.curNum * oneItemFunc) / hp_max, 0, 1)
      self.bind.scrollList_hero:change(i, {
        scale_add = scale,
        hpText = string.format("%d/%d", hp, hp_max)
      })
    elseif not math.isEmpty(heroId) and heroId ~= self.data.selectHeroId then
      local cellUseFoodHero = self.bind.scrollList_hero:getItemCls(i)
      cellUseFoodHero:resetAddhpWhileUnselect()
    end
    if math.isEmpty(heroId) and math.isEmpty(self.data.selectHeroId) then
      oneItemFunc = foodTpl:getCharge(tpl) or 0
      local chargeData = L_StarHealingStore:getCharge().cur_value
      local limitCharge = L_StarHealingStore:getMaxCharge()
      local addNum = chargeData + self.data.curNum * oneItemFunc
      self.bind.scrollList_hero:change(i, {
        scale_add = math.clamp(addNum / limitCharge, 0, 1)
      })
    end
  end
end

function this:refreshNum_reborn()
end

function this:refreshNum_adventure()
end

function this:use_hp()
  if self.data.selectHeroId == 0 then
    local func = function()
      L_BagManager:reqUseItem(self.data.curItemId, self.data.curNum, {
        L_Const.UseFoodTarget.UFT_HERO,
        0
      }, function()
        L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_useSuccess"))
        if L_BagStore:getItemNum(self.data.curItemId) < 1 then
          L_UI:close(self.pageName)
        else
          self.data.curNum = 1
          self:initHeroList(self.data.curFood)
          self:refreshNum()
        end
      end)
    end
    local chargeData = L_StarHealingStore:getCharge()
    if chargeData.cur_value >= L_StarHealingStore:getMaxCharge() then
      L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_chargeOver"))
      return
    end
    func()
  else
    local func = function()
      L_BagManager:reqUseItem(self.data.curItemId, self.data.curNum, {
        L_Const.UseFoodTarget.UFT_HERO,
        self.data.selectHeroId
      }, function()
        L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_useSuccess"))
        if L_BagStore:getItemNum(self.data.curItemId) < 1 then
          L_UI:close(self.pageName)
        else
          self.data.curNum = 1
          self:initHeroList(self.data.curFood)
          self:refreshNum()
        end
      end)
    end
    local hp_max = L_AttributeManager:getAttValueByHeroGuid(self.data.selectHeroId, L_Const.battleAtt.MAXHP)
    local hp = L_FormationStore:getFormationHeroHp(self.data.selectHeroId)
    if hp_max <= hp then
      L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_hpOver"))
      return
    end
    if hp <= 0 then
      L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_hpEmpty"))
      return
    end
    func()
  end
end

function this:use_reborn()
  local func = function()
    L_BagManager:reqUseItem(self.data.curItemId, self.data.curNum, {
      L_Const.UseFoodTarget.UFT_HERO,
      self.data.selectHeroId
    }, function()
      L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_useSuccess"))
      if L_BagStore:getItemNum(self.data.curItemId) < 1 then
        L_UI:close(self.pageName)
      else
        self.data.curNum = 1
        self:initHeroList(self.data.curFood)
        self:refreshNum()
      end
    end)
  end
  local hp = L_FormationStore:getFormationHeroHp(self.data.selectHeroId)
  if 0 < hp then
    L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_undead"))
    return
  end
  func()
end

function this:use_attackOrDefence()
  L_BagManager:reqUseItem(self.data.curItemId, self.data.curNum, {
    L_Const.UseFoodTarget.UFT_HERO,
    0
  }, function()
    L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_useSuccess"))
    if L_BagStore:getItemNum(self.data.curItemId) < 1 then
      L_UI:close(self.pageName)
    else
      self.data.curNum = 1
      self:initHeroList(self.data.curFood)
      self:refreshNum()
    end
  end)
end

function this:use_adventure()
  L_BagManager:reqUseItem(self.data.curItemId, self.data.curNum, {
    L_Const.UseFoodTarget.UFT_HERO,
    0
  }, function()
    L_FlyMsgManager:showNormalMsg(wordsTpl:getValue("notice_bag_useSuccess"))
    if L_BagStore:getItemNum(self.data.curItemId) < 1 then
      L_UI:close(self.pageName)
    else
      self.data.curNum = 1
      self:refreshNum()
    end
  end)
end

function this:initCommonTipModule()
  local moduleTip = self.modules.moduleCommonTipMedium
  local param = {}
  param.txtConfirm = "\228\189\191\231\148\168"
  local tpl_commonItem = _commonItemTpl:getTplById(self.data.curItemId)
  param.txt_title = _commonItemTpl:getName(tpl_commonItem)
  moduleTip:initModule(param)
end

return this
