local this = class("moduleTutorial", G_UIModuleBase)
local tutorialTpl = L_GameTpl:getTutorialTpl()
local tutorialTypeTpl = L_GameTpl:getTutorialTypeTpl()

function this.bind()
  return {
    tutorialActive = false,
    icon = "",
    go_key = false,
    go_progressMobile = false,
    go_progressPC = false,
    progressMobile = 1,
    progressPC = 1,
    txt_name = ""
  }
end

function this.methods()
  return {}
end

function this:open()
  L_GuideManager:addListener(G_GuideClasses.GuideEvent.TutotialGuideStart, self.onTutotialGuideStart, self)
  L_GuideManager:addListener(G_GuideClasses.GuideEvent.TutotialGuideEnd, self.onTutotialGuideEnd, self)
  self.isPlatformPC = L_DeviceTpl:getIsPc()
  self:init()
end

function this:close()
  L_GuideManager:removeListener(G_GuideClasses.GuideEvent.TutotialGuideStart, self.onTutotialGuideStart, self)
  L_GuideManager:removeListener(G_GuideClasses.GuideEvent.TutotialGuideEnd, self.onTutotialGuideEnd, self)
  self:disposeTween()
  if self.timer then
    Timer.remove(self.timer)
    self.timer = nil
  end
end

function this:init()
  self.bind.go_progressPC = self.isPlatformPC
  self.bind.go_key = self.isPlatformPC
  self.bind.go_progressMobile = not self.isPlatformPC
end

function this:onTutotialGuideEnd()
  self:disposeTween()
  self.bindComponents.tutorialNodeAnim:Play("anim_guide_tutorialbtn_done")
  self.timer = Timer.once(0.733, function()
    self.bind.tutorialActive = false
  end, self)
end

function this:onTutotialGuideStart(info)
  local tutorialConfig = tutorialTpl:getTplById(info.tutorialId)
  local tutorialType = tutorialTpl:getTutorialTypeId(tutorialConfig)
  local typeTpl = tutorialTypeTpl:getTplById(tutorialType)
  self.bind.icon = tutorialTypeTpl:getIcon(typeTpl)
  self.bind.txt_name = tutorialTpl:getTutorialName(tutorialConfig)
  self.bind.tutorialActive = true
  self.bindComponents.tutorialNodeAnim:Play("anim_guide_tutorialbtn_show")
  if self.timer then
    Timer.remove(self.timer)
    self.timer = nil
  end
  self.bind.progressMobile = 1
  self.bind.progressPC = 1
  local getter = function()
    if self.isPlatformPC then
      return self.bind.progressPC
    else
      return self.bind.progressMobile
    end
  end
  local starMobileRect = self.bindComponents.starM
  local starPCRect = self.bindComponents.starP
  local progressMobileRect = self.bindComponents.progressMobileRect
  local progressPCRect = self.bindComponents.progressPCRect
  local setter = function(r)
    if self.isPlatformPC then
      local offset = r * 0.908
      self.bind.progressPC = offset
      if offset < 0.03 then
        return
      end
      starPCRect.anchoredPosition = self:CalculatePointOnCircle(offset * 360, progressPCRect.sizeDelta.x / 2)
      starPCRect.localRotation = self:CalculateTangentOnCircle(progressPCRect.anchoredPosition, starPCRect.anchoredPosition)
    else
      self.bind.progressMobile = r
      starMobileRect.anchoredPosition = self:CalculatePointOnCircle(r * 360, progressMobileRect.sizeDelta.x / 2)
      starMobileRect.localRotation = self:CalculateTangentOnCircle(progressMobileRect.anchoredPosition, starMobileRect.anchoredPosition)
    end
  end
  self.tween = DOTween.To(getter, setter, 0, info.showTime):SetEase(Tweening.Ease.Linear):OnComplete(function()
    self.bindComponents.tutorialNodeAnim:Play("anim_guide_tutorialbtn_hide")
    self:disposeTween()
    self.timer = Timer.once(0.233, function()
      self.bind.tutorialActive = false
    end, self)
  end)
end

function this:CalculatePointOnCircle(angleDegrees, radius)
  local angleRadians = angleDegrees * (Mathf.PI / 180)
  local x = Mathf.Cos(angleRadians + Mathf.PI / 2 + Mathf.PI / 20) * radius
  local y = Mathf.Sin(angleRadians + Mathf.PI / 2 + Mathf.PI / 20) * radius * -1
  return C_Vector2(x, y)
end

function this:CalculateTangentOnCircle(circleCenter, selfPos)
  local direction = (selfPos - circleCenter).normalized
  local angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg
  return C_Quaternion.Euler(0, 0, angle)
end

function this:disposeTween()
  if L_CommonUtil.isValid(self.tween) then
    self.tween:Kill()
    self.tween = nil
  end
end

function this:activate()
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
end

return this
