local this = {}

function this:initLongPress()
  self._pressFillNode = (L_DeviceTpl:getIsPc() and self.longPressRoot:Find("pressFill_pc") or self.longPressRoot:Find("pressFill")):GetComponent(typeof(C_RectTransform))
  self._pressAnim = self.longPressRoot:GetComponent(typeof(C_Animation))
  self._pressFillImage = self._pressFillNode:GetComponent(typeof(C_LImage))
  
  function self._stopLongPress()
    self:killLongPressTween()
  end
  
  C_ScreenTouch.OnUI_StayEnd("+", self._stopLongPress)
end

function this:startLongPress(gesture, time, callback)
  self:killLongPressTween()
  local touchPos = L_Vector3.screenToLocalInRectangle(self.longPressRoot, gesture.position)
  if L_DeviceTpl:getIsPc() then
    touchPos = touchPos + L_Vector3.new(36, 12, 0)
  else
    touchPos = touchPos + L_Vector3.new(0, 12 + gesture.radius, 0)
  end
  self:startLongPressByPos(touchPos, time, callback)
end

function this:startLongPressByPos(rectPos, time, callback)
  self:killLongPressTween()
  self._pressFillNode.gameObject:SetActive(true)
  self:setPressShowState(true)
  L_Vector3.setAnchored(self._pressFillNode, rectPos)
  self._pressFillImage.fillAmount = 0
  self._longPressTween = self._pressFillImage:DOFillAmount(0.85, time * 0.85)
  
  function self._longPressTween.onComplete()
    self:setPressShowState(false)
    if callback then
      callback()
    end
    self._longPressTween = self._pressFillImage:DOFillAmount(1, time * 0.15)
  end
end

function this:killLongPressTween()
  if self._longPressTween then
    printf("Midorimon - \233\149\191\230\140\137\231\187\136\230\173\162")
    self._longPressTween:Kill()
    self._longPressTween = nil
  end
  self:setPressShowState(false)
end

function this:setPressShowState(isShow)
  if self._currPressShow == isShow then
    return
  end
  if isShow then
    self._pressAnim:Play("anim_longpress_in")
  else
    self._pressAnim:Play("anim_longpress_out")
  end
  self._currPressShow = isShow
end

function this:disposeLongPress()
  C_ScreenTouch.OnUI_StayEnd("-", self._stopLongPress)
  self:killLongPressTween()
  self._stopLongPress = nil
  self._pressFillNode = nil
  self._pressFillImage = nil
end

return this
