local this = class("cellPhotoTemplate", G_UIModuleBase)

function this.bind()
  return {
    outLineAphla = 0.3,
    txt_name = "",
    activeChangeName = false,
    activeDeleteBtn = false,
    activeInfoNode = false,
    activeAddBtn = false,
    interactableInput = false,
    inputName = "",
    inputMaxNum = 0
  }
end

function this.methods()
  return {
    onChangeName = function(self, setStr)
      local systemId = L_SystemConst.enum.renamePhotoTemplate
      if L_SystemBreakManager:checkBanSystemAndTip(systemId) then
        return
      end
      if not self.bind.id then
        return
      end
      local data = L_PhotoStore:getTemplateData(self.bind.type, self.bind.id, true)
      if data and not string.isEmpty(setStr) and data.name ~= setStr then
        setStr = C_LuaUtility.StringSub(setStr, 0, L_PhotoStore.tempRenameMaxNum)
        L_PhotoStore:req_renameTemplate(data.id, setStr, function(isSuccess)
          if isSuccess then
            data.name = setStr
          end
          self.bind.inputName = data.name
          self:layoutName()
        end)
      end
    end,
    onDelete = function(self)
      if not self.bind.id then
        return
      end
      L_GameUtil.showCommonTip({
        txtTitle = L_WordsTpl:getValue("ui_cellPhotoTemplate_02"),
        txtContent = L_WordsTpl:getValue("ui_cellPhotoTemplate_01"),
        confirmCallback = function()
          if L_PhotoStore:removeTemplateData(self.bind.type, self.bind.id) then
            self.parent:refreshMain()
          end
        end
      })
    end,
    onClickCell = function(self)
      if self.bind.isAddBtn then
        return
      end
      self:emit("onClickCell", self)
    end,
    onClick_add = function(self)
      self:emit("onClickAdd", self.bind)
    end
  }
end

function this:open()
  self.templateData = self.templateData or table.empty
  self.bind.isSelect = false
  self:refreshMain()
end

function this:layoutName()
  Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.inputFitter)
end

function this:refreshMain()
  local showAddBtn = self.bind.isAddBtn
  self.bind.activeAddBtn = showAddBtn
  self.bind.activeInfoNode = not showAddBtn
  self:refreshSelect()
  local showChange = self.bind.type == L_Const.photoTemplateDataType.custom and not showAddBtn
  self.bind.activeChangeName = showChange
  self.bind.activeDeleteBtn = showChange
  self.bind.interactableInput = showChange
  if self.bind.type == L_Const.photoTemplateDataType.custom then
    self:createTexWithName(self.bind.bgImgName)
  elseif self.bind.type == L_Const.photoTemplateDataType.config then
    self.bindComponents.bgImg:LoadSprite(self.bind.bgImgName, false)
  else
    self.bindComponents.bgImg:LoadSprite(self.bind.bgImgName)
  end
  self:layoutName()
end

function this:refreshSelect()
  self.bind.outLineAphla = self.bind.isSelect and 1 or 0.3
end

function this:refreshImg()
  if self.bindComponents.bgImg ~= nil and self.sprite ~= nil then
    self.bindComponents.bgImg.sprite = self.sprite
    self.bindComponents.bgImg:SetNativeSize()
  end
end

function this:createTexWithName(name)
  if string.isEmpty(name) then
    return
  end
  L_PhotoManager:loadHeadTex(name, function(tex)
    if tex then
      self.texture = tex
      local rect = C_Rect(0, 0, tex.width, tex.height)
      local pivot = C_Vector2.one * 0.5
      self.sprite = Unity.Sprite.Create(tex, rect, pivot)
      self:refreshImg()
    end
  end, L_PhotoStore.path.tempBgSavePath, L_PhotoStore.path.tempBgCDNPath)
end

return this
