local module = class("moduleOptionalRaffle", G_UIModuleBase)
local spiritTpl = L_GameTpl:getSpiritronTpl()
local gachaTpl = L_GameTpl:getGachaTpl()

function module:ctor(...)
  module.super.ctor(self, ...)
  self.data = {
    choose_id = 0,
    choose_type = 0,
    spirit_name = "",
    pool_id = 0,
    choose_num = 0,
    spirit_list = nil
  }
end

function module.bind()
  return {
    chooseList = {
      moduleName = "pages/Raffle/cellSpiritItem"
    },
    show_chooseList = false,
    show_iconButton = true,
    show_choosedInfo = false,
    spirit_icon = "",
    up_getCount = "0/2",
    choosed_name = ""
  }
end

function module.methods()
  return {
    onClick_close = function(self)
      self.parent:enableSpiritChooseModule(false)
    end,
    onClick_cancelChoosedSpirit = function(self)
      L_UI:open("confirm", {
        title = L_WordsTpl:getValue("notice_modulePetList_03"),
        content = L_WordsTpl:getValue("ui_moduleOptionalRaffle"),
        type = 2,
        ok = function()
          self.data = {
            choose_id = 0,
            choose_type = 0,
            pool_id = self.data.pool_id
          }
          L_RaffleStore:req_raffleChoose(self.data)
        end
      })
    end,
    onClick_chooseSpirit = function(self)
      self.bind.show_chooseList = true
      self:initChooseList(self.data)
    end,
    onClick_backToMain = function(self)
      self.bind.show_chooseList = false
    end,
    onClick_backToMain_mask = function(self)
      self.bind.show_chooseList = false
    end,
    chooseList = {
      onClick_chooseSpirit = function(self, data)
        local sendData = {
          choose_id = data.spirit_id,
          choose_type = data.spirit_type,
          pool_id = self.data.pool_id
        }
        L_RaffleStore:req_raffleChoose(sendData, function()
          self.data.choose_id = sendData.choose_id
          self.data.choose_type = sendData.choose_type
          self.bind.show_chooseList = false
        end)
      end
    }
  }
end

function module:open()
  L_RaffleStore:listenCallFunc(L_RaffleStore.event.refreshCardPoolList, self.onEvent_refreshPoolInfo, self)
end

function module:init(poolInfo)
  self.data.choose_id = poolInfo.choose_id
  self.data.choose_type = poolInfo.choose_type
  self.data.choose_num = poolInfo.choose_num
  self.data.pool_id = poolInfo.pool_id
  self.data.spirit_list = gachaTpl:getChooseId(gachaTpl:getTplById(poolInfo.pool_id))
  self.bind.show_iconButton = poolInfo.choose_id == 0 and true or false
  self.bind.show_choosedInfo = poolInfo.choose_id ~= 0 and true or false
  self.bind.choosed_name = poolInfo.choose_id ~= 0 and spiritTpl:getName(spiritTpl:getTplById(poolInfo.choose_id)) or ""
  self.bind.up_getCount = string.format("<color=#fc2b2c>%d</color>/2", poolInfo.choose_num)
  self.bind.spirit_icon = poolInfo.choose_id == 0 and "" or L_ItemManager:parseSpiritronItem(poolInfo.choose_id).iconHalf
end

function module:close()
  L_RaffleStore:unListenCallFunc(L_RaffleStore.event.refreshCardPoolList, self.onEvent_refreshPoolInfo)
end

function module:onEvent_refreshPoolInfo(poolInfo)
  self:init(poolInfo)
end

function module:initChooseList(data)
  self.bind.chooseList:clear()
  local data_list = {}
  local list = data.spirit_list
  for i, v in pairs(list) do
    table.insert(data_list, {
      spirit_id = v,
      spirit_type = 9,
      spirit_name = spiritTpl:getName(spiritTpl:getTplById(v))
    })
  end
  self.bind.chooseList:insert_array(data_list)
end

return module
