local this = class("pageMiniGameDragonPlate", G_UIPageBase)

function this.bind()
  return {
    module_commonTop = L_Const.ModuleInfo.CommonTop,
    isActive_main = true
  }
end

function this.methods()
  return {
    onClick_left = function(self)
      self:callRotateLeft()
    end,
    onClick_right = function(self)
      self:callRotateRight()
    end,
    onClick_reset = function(self)
      self:callReset()
    end,
    onClick_start = function(self)
      self:callFlow()
    end
  }
end

function this:preOpen(options)
  self.rotating = false
  self:initCommonTopModule()
  self.onFlowEndHandle = handler(self, self.onFlowEnd)
  self.onFlowStartHandle = handler(self, self.onFlowStart)
  self.onRotateStartHandle = handler(self, self.onRotateStart)
  self.onRotateEndHandle = handler(self, self.onRotateEnd)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnRotatePlateRotateBegin, self.onRotateStartHandle)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnRotatePlateRotateEnd, self.onRotateEndHandle)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnRotatePlateFlowBegin, self.onFlowStartHandle)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnRotatePlateFlowEnd, self.onFlowEndHandle)
end

function this:close(options)
end

function this:escHandle()
  if self.bind.isActive_main then
    self:callQuit()
    L_UI:close(self.pageName)
  end
end

function this:initCommonTopModule()
  local data = {
    name = "\230\152\159\230\178\153\229\136\187\231\151\149",
    closeCurPage = function()
      self:callQuit()
      L_UI:close(self.pageName)
    end,
    helpBtnBool = true,
    helpBtn = function()
      L_UI:open("pageCommonIntroduction", {groupId = 200})
    end
  }
  self.modules.module_commonTop:initModule(data)
end

function this:callRotateLeft()
  if self.rotating then
    return
  end
  local list = C_VarList:Get()
  list:AddInt(1)
  C_WorldEvent.instance:Dispatch(C_EWorldEvent.OnRotatePlateRotate, list)
end

function this:callRotateRight()
  if self.rotating then
    return
  end
  local list = C_VarList:Get()
  list:AddInt(-1)
  C_WorldEvent.instance:Dispatch(C_EWorldEvent.OnRotatePlateRotate, list)
end

function this:callReset()
  if self.rotating then
    return
  end
  C_WorldEvent.instance:Dispatch(C_EWorldEvent.OnRotatePlateReset)
end

function this:callFlow()
  if self.rotating then
    return
  end
  C_WorldEvent.instance:Dispatch(C_EWorldEvent.OnRotatePlateFlow)
end

function this:callQuit()
  C_WorldEvent.instance:Dispatch(C_EWorldEvent.OnRotatePlateQuit)
end

function this:onRotateStart()
  self.rotating = true
end

function this:onRotateEnd()
  self.rotating = false
end

function this:onFlowStart()
  self.rotating = true
  self.bind.isActive_main = false
end

function this:onFlowEnd(id, param)
  self.rotating = false
  local result = param:GetInt(0)
  if result == 0 then
    self.bind.isActive_main = true
  elseif result == 1 then
    L_UI:close(self.pageName)
  end
end

function this:close()
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnRotatePlateRotateBegin, self.onRotateStartHandle)
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnRotatePlateRotateEnd, self.onRotateEndHandle)
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnRotatePlateFlowBegin, self.onFlowStartHandle)
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnRotatePlateFlowEnd, self.onFlowEndHandle)
end

return this
