local this = class("pageTemplateTip", G_UIPageBase)

function this.bind()
  return {
    module_commonTipLarge = L_Const.ModuleInfo.CommonTipLarge,
    inputName = "",
    inputMaxNum = 0
  }
end

function this.methods()
  return {
    module_commonTipLarge = {
      onClick_close = function(self)
        self:closeSelf(self.close_cb)
      end,
      onClick_cancel = function(self)
        self:closeSelf(self.cancel_cb)
      end,
      onClick_confirm = function(self)
        C_AudioManager.Play("Play_SFX_System_UI_General_Comfirm_L2")
        self:closeSelf(function()
          if self.ok_cb then
            self.ok_cb(self.name)
          end
        end)
      end,
      onClick_bg = function(self)
        self:closeSelf(self.close_cb)
      end
    },
    onChangeName = function(self, setStr)
      if not string.isEmpty(setStr) and self.name ~= setStr then
        self.name = C_LuaUtility.StringSub(setStr, 0, L_PhotoStore.tempRenameMaxNum)
        self.bind.inputName = self.name
      end
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self:initCommonTipModule()
  self.ok_cb = options.ok_cb
  self.cancel_cb = options.cancel_cb
  self.close_cb = options.close_cb
  self.rt = options.rt
  self.name = options.name or L_WordsTpl:getValue("ui_pageTemplateTip_04")
  local mat = C_UnityObject.Instantiate(self.bindComponents.rawImage.material)
  mat:EnableKeyword("FIX_TO_GAMMA")
  self.bindComponents.rawImage.material = mat
  self.bindComponents.rawImage.texture = self.rt
  self:autoFit()
  self.bind.inputName = self.name
end

function this:initCommonTipModule()
  local moduleTip = self.modules.module_commonTipLarge
  moduleTip:initModule({
    txtTitle = L_WordsTpl:getValue("ui_pageTemplateTip_03"),
    txtConfirm = L_WordsTpl:getValue("ui_pageTemplateTip_01"),
    txtCancel = L_WordsTpl:getValue("ui_pageTemplateTip_02")
  })
end

function this:autoFit()
  local parentRect = self.bindComponents.imgNode
  local parentWidth = parentRect.rect.size.x
  local parentHeight = parentRect.rect.size.y
  local imgWidth = self.rt.width
  local imgHeight = self.rt.height
  local ratio = imgWidth / imgHeight
  local targetWidth, targetHeight
  if ratio < parentWidth / parentHeight then
    targetHeight = parentHeight
    targetWidth = targetHeight * ratio
  else
    targetWidth = parentWidth
    targetHeight = targetWidth / ratio
  end
  local size = C_Vector2(targetWidth, targetHeight)
  self.bindComponents.rawImage.gameObject:GetComponent(typeof(C_RectTransform)).sizeDelta = size
end

function this:close()
  if self.rt then
    C_RenderTexture.ReleaseTemporary(self.rt)
  end
end

function this:closeSelf(cb)
  L_UI:close(self.pageName, nil, function()
    if cb then
      cb()
    end
  end)
end

return this
