local this = class("modulePhotoStampHandle", G_UIModuleBase)
local _maxScale = 2
local _minScale = 0.5

function this.bind()
  return {}
end

function this.methods()
  return {
    onDrag = function(self, screenPos)
      self:onDrag(screenPos)
    end,
    onBeginDrag = function(self, screenPos)
      self.lastScreenPos = screenPos
    end,
    onDragHandle = function(self, screenPos)
      self:onDragHandle(screenPos)
    end,
    onBeginDragHandle = function(self, screenPos)
      self.lastScreenPos = screenPos
    end,
    onClickDel = function(self)
      self:emit("onClickDel", self.bindRect)
    end
  }
end

function this:open()
  self.rectTransform = self.gameObject:GetComponent(typeof(C_RectTransform))
  self.bindComponents.btnDrag.DragObjParent = self.rectTransform.parent
end

function this:bindStamp(uiRect)
  self.bindRect = uiRect
  self.rectTransform.localPosition = uiRect.localPosition
  self.rectTransform.sizeDelta = uiRect.sizeDelta
  self.oriSize = uiRect.sizeDelta
  self.rectTransform.sizeDelta = self.oriSize
  local img = uiRect.gameObject:GetComponent(typeof(C_LImage))
  local width = img.sprite.texture.width
  local height = img.sprite.texture.height
  self.maxSize = C_Vector2(width * _maxScale, height * _maxScale)
  self.minSize = C_Vector2(width * _minScale, height * _minScale)
  self.rectTransform:SetAsLastSibling()
end

function this:setActive(b)
  self.gameObject:SetActive(b)
  if not b then
    self.bindRect = nil
  end
end

function this:onDrag(screenPos)
  if self.lastScreenPos == nil then
    self.lastScreenPos = screenPos
    return
  end
  local prePos = L_GameUtil.spToLpInRectangle(self.rectTransform.parent, self.lastScreenPos)
  local curPos = L_GameUtil.spToLpInRectangle(self.rectTransform.parent, screenPos)
  local offset = curPos - prePos
  local targetPos = self.rectTransform.localPosition + offset
  local sizeLimit = self.rectTransform.parent.rect.size
  targetPos.x = math.clamp(targetPos.x, -sizeLimit.x / 2, sizeLimit.x / 2)
  targetPos.y = math.clamp(targetPos.y, -sizeLimit.y / 2, sizeLimit.y / 2)
  self.rectTransform.localPosition = targetPos
  self.bindRect.localPosition = targetPos
  self.lastScreenPos = screenPos
end

function this:onDragHandle(screenPos)
  if self.lastScreenPos == nil then
    self.lastScreenPos = screenPos
    return
  end
  local prePos = L_GameUtil.spToLpInRectangle(self.rectTransform.parent, self.lastScreenPos)
  local curPos = L_GameUtil.spToLpInRectangle(self.rectTransform.parent, screenPos)
  local offset = curPos - prePos
  offset.x = -offset.x * 2
  offset.y = -offset.y * 2
  local curSize = self.rectTransform.sizeDelta
  local ratio = curSize.x / curSize.y
  local targetSize = curSize + offset
  if math.abs(offset.x) > math.abs(offset.y) then
    targetSize.y = targetSize.x / ratio
  else
    targetSize.x = targetSize.y * ratio
  end
  targetSize.x = math.clamp(targetSize.x, self.minSize.x, self.maxSize.x)
  targetSize.y = math.clamp(targetSize.y, self.minSize.y, self.maxSize.y)
  self.rectTransform.sizeDelta = targetSize
  self.bindRect.sizeDelta = targetSize
  self.lastScreenPos = screenPos
end

function this:refresh()
end

function this:close()
end

return this
