local this = class("pageCreateTestBattle", G_UIPageBase)
local pageType = {
  enemy = 1,
  save = 2,
  load = 3
}

function this.bind()
  return {
    scrollBattle = {
      moduleName = "pages/bugWorld/cellItem/cellTestClickItem"
    },
    scrollEnemy = {
      moduleName = "pages/bugWorld/cellItem/cellTestClickItem"
    },
    goEnemy = true,
    goPlan = false,
    scrollPlan = {
      moduleName = "pages/bugWorld/cellItem/cellTestClickItem"
    },
    scrollChoiceEnemy = {
      moduleName = "pages/bugWorld/cellItem/cellTestClickItem"
    }
  }
end

function this.methods()
  return {
    onClick_nowBattle = function(self)
      self:onClick_nowBattle()
    end,
    onClick_autoBattle = function(self)
      self:onClick_autoBattle()
    end,
    onClick_noTeam = function(self)
      self:onClick_noTeam()
    end,
    onClick_goConfirmBattle = function(self)
    end,
    onClick_saveConfig = function(self)
      self:onClick_saveConfig()
    end,
    onClick_loadConfig = function(self)
      self:onClick_loadConfig()
    end,
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_add = function(self)
      self:onClick_add()
    end,
    onValueChangedsearch = function(self, value)
      self:refreshEnemyChoiceList(value)
    end,
    scrollBattle = {
      onClick = function(self, bind)
        self:setChoice(bind.battleId)
      end
    },
    scrollEnemy = {
      onClick = function(self, bind)
        self:onClick_Delete(bind.id)
      end
    },
    scrollPlan = {
      onClick = function(self, bind)
        self:onClick_planChoice(bind.id)
      end
    },
    scrollChoiceEnemy = {
      onClick = function(self, bind)
        self:onClick_choiceEnemy(bind.id)
      end
    }
  }
end

function this:ctor()
  this.super.ctor(self)
end

function this:open(options)
  this.super.open(self, options)
  self.bind.data = {
    battleList = {},
    enemyList = {},
    curEnemyId = nil,
    curEnemyLevelId = nil,
    curBattleId = nil,
    curDifficultId = nil
  }
  self.bindComponents.input_enemyId.text = ""
  self.bindComponents.input_lv.text = ""
  self.bindComponents.input_choice_lv.text = "1"
  self:refreshType(pageType.enemy)
  self:refreshEnemyChoiceList()
end

function this:refreshEnemyChoiceList(search)
  local enemyList = {}
  local insertList = {}
  local _enemyTpl = L_GameTpl:getEnemyTpl()
  enemyList = table.toArray(_enemyTpl.data)
  for i, v in ipairs(enemyList) do
    if v.isUsable == 1 then
      if string.isEmpty(search) then
        local item = {}
        item.id = _enemyTpl:getId(v)
        item.txt_name = _enemyTpl:getId(v) .. " - " .. _enemyTpl:getName(v)
        table.insert(insertList, item)
      else
        local item = {}
        item.id = _enemyTpl:getId(v)
        item.txt_name = _enemyTpl:getId(v) .. " - " .. _enemyTpl:getName(v)
        if string.find(item.txt_name, search) then
          table.insert(insertList, item)
        end
      end
    end
  end
  self.bind.scrollChoiceEnemy:clear()
  self.bind.scrollChoiceEnemy:insert_array(insertList)
end

function this:refreshType(type)
  self.type = type
  self.bind.goEnemy = self.type == pageType.enemy
  self.bind.goPlan = self.type ~= pageType.enemy
  if self.type == pageType.enemy then
    self:refreshEnemyList()
  else
    self:refreshPlanList()
  end
end

function this:refreshPlanList()
  local enemyList = {}
  local dic = L_BattleEnemyStore:getEnemySaveDic()
  local list = {}
  for i, v in pairs(dic) do
    local item = {}
    item.id = i
    item.txt_name = (self.type == pageType.save and L_WordsTpl:getValue("ui_systemMessage_01") or L_WordsTpl:getValue("ui_systemMessage_02")) .. L_WordsTpl:getValue("ui_systemMessage_03") .. tostring(i)
    table.insert(list, item)
  end
  self.bind.scrollPlan:clear()
  self.bind.scrollPlan:insert_array(list)
end

function this:close(options)
  this.super.close(self, options)
end

function this:setBattleChoice(id)
end

function this:onClick_Delete(id)
  L_BattleEnemyStore:deleteEnemyItem(id)
  self:refreshEnemyList()
end

function this:onClick_add()
  if string.isEmpty(self.bindComponents.input_enemyId.text) then
    return
  end
  if string.isEmpty(self.bindComponents.input_lv.text) then
    return
  end
  local enemyId = tonumber(self.bindComponents.input_enemyId.text)
  if L_GameTpl:getEnemyTpl():getTplById(enemyId) == nil then
    L_FlyMsgManager:showNormalMsg("\228\189\160\229\161\171\228\186\134\228\184\170\233\148\164\229\173\144\239\188\140\230\178\161\232\191\153id\231\154\132\230\128\170\231\137\169")
    return
  end
  L_BattleEnemyStore:createEnemyItem(enemyId, tonumber(self.bindComponents.input_lv.text))
  self:refreshEnemyList()
end

function this:refreshEnemyList()
  local enemyList = {}
  local dic = L_BattleEnemyStore:getEnemyDic()
  local _enemyTpl = L_GameTpl:getEnemyTpl()
  enemyList = table.toArray(dic)
  for i, v in ipairs(enemyList) do
    v.id = v.guid
    v.txt_name = v.enemyId .. "-" .. _enemyTpl:getName(_enemyTpl:getTplById(v.enemyId)) .. "-lv" .. v.lv
  end
  self.bind.scrollEnemy:clear()
  self.bind.scrollEnemy:insert_array(enemyList)
end

function this:onClick_noTeam()
  L_UI:close(self.pageName)
  L_BattleManager:startDebugBattleNoTeam()
end

function this:onClick_nowBattle()
  L_UI:close(self.pageName)
  L_BattleManager:startDebugBattle()
end

function this:onClick_autoBattle()
  L_UI:close(self.pageName)
  L_BattleManager:startDebugBattle(true)
end

function this:onClick_saveConfig()
  self:refreshType(pageType.save)
end

function this:onClick_loadConfig()
  self:refreshType(pageType.load)
end

function this:onClick_planChoice(index)
  if self.type == pageType.load then
    L_BattleEnemyStore:resetLoadEnemy(index)
  elseif self.type == pageType.save then
    L_BattleEnemyStore:saveCurEnemy(index)
  end
  self:refreshType(pageType.enemy)
end

function this:onClick_choiceEnemy(enemyId)
  if string.isEmpty(self.bindComponents.input_choice_lv.text) then
    errorf("\229\161\171\231\173\137\231\186\167")
    return
  end
  L_BattleEnemyStore:createEnemyItem(enemyId, tonumber(self.bindComponents.input_choice_lv.text))
  self:refreshEnemyList()
end

return this
