local this = class("modulePetBoxName", G_UIModuleBase)
local petLevelTpl = L_GameTpl:getPetLevelTpl()
local petTpl = L_GameTpl:getPetTpl()

function this.bind()
  return {
    pet_name = "",
    show_modifyBtn = true,
    showLockModule = true,
    isLock = false,
    lv = "",
    maxLv = ""
  }
end

function this.methods()
  return {
    onClick_modify = function(self)
      local systemId = L_SystemConst.enum.renameKibo
      if L_SystemBreakManager:checkBanSystemAndTip(systemId) then
        return
      end
      local pet = L_PetStore:getPetItem(self.petId)
      local tpl = petTpl:getTplById(pet.id)
      local canRename = petTpl:getIsRename(tpl)
      if not canRename then
        L_FlyMsgManager:showNormalMsgByKey("ui_modulePetBoxName_01")
        return
      end
      local data = {
        txtTitle = L_WordsTpl:getValue("ui_modulePetBoxName_02"),
        placeHolder = L_WordsTpl:getValue("ui_modulePetBoxName_03"),
        limit = 7,
        textBoxType = L_Const.CommonTextBoxType.Rename,
        confirmCallback = function(txt)
          if string.isEmpty(string.trim(txt)) then
            L_FlyMsgManager:showNormalMsgByKey("notice_pageFormationMain_03")
            return
          end
          if string.gsub(txt, " ", "") ~= txt then
            L_FlyMsgManager:showNormalMsgByKey("notice_pageFormationMain_02")
            return
          end
          local contains_special_char = function(str)
            local special_char_pattern = "[^%w\228\184\128-\233\190\165]"
            return string.find(str, special_char_pattern) ~= nil
          end
          local hasSpecial = contains_special_char(txt)
          if hasSpecial == true then
            L_FlyMsgManager:showNormalMsgByKey("ui_modulePetBoxName_04")
            return
          end
          L_PetStore:req_changePetName(txt, self.petId, function(name)
            if name == nil then
              self:setGuid(self.petId)
              L_FlyMsgManager:showNormalMsgByKey("ui_modulePetBoxName_05")
            end
          end)
        end
      }
      L_GameUtil.showNormalTextBox(data)
    end,
    onClick_lockPet = function(self)
      local pet = L_PetStore:getPetItem(self.petId)
      if pet.is_lock == true then
        L_PetStore:req_lockPet(self.petId, false, function()
          self:setGuid(self.petId)
          L_FlyMsgManager:showNormalMsgByKey("notice_pagePetGet_02")
        end)
      else
        L_PetStore:req_lockPet(self.petId, true, function()
          self:setGuid(self.petId)
          L_FlyMsgManager:showNormalMsgByKey("notice_pagePetGet_03")
        end)
      end
    end
  }
end

function this:open()
  L_PetStore:listenCallFunc(L_PetStore.event.setPetReleaseMode, self.onChangePetReleaseMode, self)
end

function this:close()
  L_PetStore:unListenCallFunc(L_PetStore.event.setPetReleaseMode, self.onChangePetReleaseMode, self)
end

function this:setGuid(guid)
  local pet = L_PetStore:getPetItem(guid)
  self.petId = guid
  self.maxLv = L_PetStore:getPetMaxLevel(true)
  self.lv = pet.lv
  self.bind.lv = tostring(pet.lv)
  self.bind.pet_name = pet.pet_name
  self.bind.maxLv = string.format("/%d", self.maxLv)
  self.bind.isLock = pet.is_lock
  self:onChangePetReleaseMode()
end

function this:setConfigId(configId, isDetail)
  local tpl = petTpl:getTplById(configId)
  self.maxLv = L_PetStore:getPetMaxLevel()
  self.lv = 1
  self.bind.pet_name = petTpl:getName(tpl)
  self.bind.showLockModule = false
  self.bind.show_modifyBtn = false
  if isDetail then
    self.bind.lv = "??"
    self.bind.maxLv = "/??"
  else
    self.bind.lv = tostring(self.lv)
    self.bind.maxLv = string.format("/%d", self.maxLv)
  end
end

function this:onChangePetReleaseMode()
  self.bind.showLockModule = not L_PetReleaseManager:getIsReleaseMode()
end

return this
