module EventLoop.Input.Mouse(
    Mouse(..),
    MouseButton(..)
) where

import EventLoop.Json
import EventLoop.Config
import EventLoop.CommonTypes


-- Possible messages:
-- {"type":"mouse","button":"<mkey>","x":<number>,"y":<number>,"element":"<name>"}
-- <mkey> = left | right | middle
-- <name> = (a | b | c | ... | z | A | B | C ...)+
-- <numbers> = (1 | 2 | 3 | ... | 0)+

-- Mouse Messages In
data Mouse = MouseClick MouseButton Pos Element
           | MouseUp MouseButton Pos Element
           | MouseDown MouseButton Pos Element
           deriving Show
           
data MouseButton = MLeft | MRight | MMiddle
                    deriving Show

instance FromJSON Mouse where
    fromJsonMessage (JSONObject ms) | mouseType == mouseclickS = MouseClick button pos element
                                    | mouseType == mouseupS    = MouseUp button pos element
                                    | mouseType == mousedownS  = MouseDown button pos element
                                    where
                                        JSONString mouseType = retrieveError mousetypeS ms
                                        JSONFloat x = retrieveError xS ms
                                        JSONFloat y = retrieveError yS ms
                                        pos = (x, y)
                                        JSONString element = retrieveError elementS ms
                                        jsonButton = retrieveError buttonS ms
                                        button = fromJsonMessage jsonButton
       
instance FromJSON MouseButton where
    fromJsonMessage (JSONString but) | leftS   == but = MLeft
                                     | middleS == but = MMiddle
                                     | rightS  == but = MRight
                                     | otherwise = error ("Could not create a mouse button from value: " ++ but)