
dofile("anty_quick_click.lua")

local LAST_FRAME = 1
local CURRENT_FRAME = 2

local lastPos = {X = 0, Y = 0, Z = 0}
local ButtonsStates = {}
local ClickTime = { }
local ClickCount = 1
ClickTime[LAST_FRAME] = 0
ClickTime[CURRENT_FRAME] = 0

ButtonsStates[LAST_FRAME] = {MB_LEFT = false, MB_RIGHT = false, MB_MIDDLE = false}
ButtonsStates[CURRENT_FRAME] = {MB_LEFT = false, MB_RIGHT = false, MB_MIDDLE = false}

ClickHolder = QuickClickBlocker()
local Click = true

function LeftClick()
 if UI:IsScreenCovered(-1, -1) then
    return false
  end
  
  if not ClickHolder:CanClick() then
    return false
  end
  
  return Click and (ButtonsStates[CURRENT_FRAME][MB_LEFT] == false and ButtonsStates[LAST_FRAME][MB_LEFT] == true)
end

function OldLeftClick()
  if UI:IsScreenCovered(-1, -1) then
    return false
  end
  
  if not ClickHolder:CanClick() then
    return false
  end
  
  return ButtonsStates[CURRENT_FRAME][MB_LEFT] == true and ButtonsStates[LAST_FRAME][MB_LEFT] == false
end

function DoubleClick()
  return LeftClick() and ClickCount == 2 and ( ClickTime[CURRENT_FRAME] - ClickTime[LAST_FRAME] < 500)
end

function RightClick()
  return ButtonsStates[CURRENT_FRAME][MB_RIGHT] == true and ButtonsStates[LAST_FRAME][MB_RIGHT] == false
end

-- MB_LEFT, MB_RIGHT, MB_MIDDLE
function IsMouseButtonDown(key)
    return  ButtonsStates[CURRENT_FRAME][key] == true
end

function GetPosition()
  return Ogre.Vector2(lastPos.X, lastPos.Y)
end

function OnMove(_X, _Y, _Z)
  
  if Widgets.Inventory ~= nil then
    if lastPos.Y < 0.95 and _Y >= 0.95 then
      Widgets.Inventory.MouseEnter()
    elseif lastPos.Y >= 0.84 and _Y < 0.84  then
      Widgets.Inventory.MouseLeave()
    end
  end
  
  lastPos.X = _X
  lastPos.Y = _Y
  lastPos.Z = _Z
end

function OnPress( _X, _Y, _Z, _Btn)
  Click = true
  local viewport = MystViewportManager:GetInstance():GetFocusedViewport()
  if viewport == nil then
    return
  end
  local view = viewport:GetView()
  
  if _Btn == MB_RIGHT then 
    
    if MystInventory:GetActive() ~= nil then
      local Inv = MystInventory:GetActive()
      if Inv:GetSelectedItem() ~= "" then
        Inv:ReleaseItem()
      end
    end
    
    pos = viewport:ViewportToTargetAbs(Ogre.Vector2(_X, _Y))
    if  view ~= nil and ( not UI:IsScreenCovered(pos.x, pos.y) ) then
      view:LuaCall("OnRightClick") 
    end
    
  elseif _Btn == MB_LEFT then
    ClickTime[LAST_FRAME] = ClickTime[CURRENT_FRAME]
    ClickTime[CURRENT_FRAME] = Time:GetAbsoluteTime() / 1000
    
    if ClickCount == 2 then
      ClickCount = 1
    else
      ClickCount = 2
    end
    
    pos = viewport:ViewportToTargetAbs(Ogre.Vector2(_X, _Y))
    if  view ~= nil and ( not UI:IsScreenCovered(pos.x, pos.y) ) then   
      view:LuaCall("OnClick") 
    end
  end -- MB_RIGHT
end

function OnRelease( _X, _Y, _Z, _Btn )  
  local viewport = MystViewportManager:GetInstance():GetFocusedViewport()
  if viewport == nil then
    return
  end
  
  local view = viewport:GetView()
  if view == nil then return end
  if _Btn == MB_LEFT then       
	view:LuaCall("OnRelease")
  elseif _Btn == MB_RIGHT then       
	view:LuaCall("OnRightRelease")
  end
end
local i = 0
function Update(dt, leftBtnDown, rightBtnDown, midBtnDown)
  
  ButtonsStates[LAST_FRAME][MB_LEFT]   = ButtonsStates[CURRENT_FRAME][MB_LEFT] 
  ButtonsStates[LAST_FRAME][MB_RIGHT]  = ButtonsStates[CURRENT_FRAME][MB_RIGHT] 
  ButtonsStates[LAST_FRAME][MB_MIDDLE] = ButtonsStates[CURRENT_FRAME][MB_MIDDLE] 
    
  ButtonsStates[CURRENT_FRAME][MB_LEFT]   = leftBtnDown
  ButtonsStates[CURRENT_FRAME][MB_RIGHT]  = rightBtnDown
  ButtonsStates[CURRENT_FRAME][MB_MIDDLE] = midBtnDown  
  
  ClickHolder:Update()
end

function NoClick()
  Click = false
end

function NotifyEmptyClick()
  ClickHolder:AddClick()
end


