{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE JavaScriptFFI #-}
{-# LANGUAGE OverloadedStrings #-}

module Etude
( someFunc ) where

import Control.Monad
import Control.Monad.IO.Class (MonadIO(..))

import GHCJS.DOM (currentDocument, currentWindow)
import GHCJS.DOM.Document (getElementById, getBodyUnsafe, createTextNode, getBody)
import qualified GHCJS.DOM.Document as D (click)
import GHCJS.DOM.HTMLElement (setInnerText, toHTMLElement, getOuterText)
import GHCJS.DOM.Element (setInnerHTML, getInnerHTML)
import GHCJS.DOM.HTMLTextAreaElement as TextArea
import GHCJS.DOM.Window (alert, getLocation)
import GHCJS.DOM.EventM (on, mouseClientXY)
import GHCJS.DOM.JSFFI.Generated.Location (replace)
import GHCJS.DOM.Types (Window, Document, toDocument, Element)
import qualified GHCJS.Types as T
import qualified GHCJS.Foreign as F
import qualified GHCJS.Foreign.Callback as CB
import qualified GHCJS.Concurrent as CC

foreign import javascript unsafe "$1[\"alert\"]($2)" my_alert :: Window -> T.JSString -> IO ()
foreign import javascript unsafe "window.alert($1)" my_alert2 :: T.JSString -> IO ()
foreign import javascript unsafe "$1[\"onload\"] = $2" my_onload :: Window -> CB.Callback a -> IO()
foreign import javascript unsafe "window.onload = $1" my_onload2 :: CB.Callback a -> IO()
foreign import javascript unsafe "$1.click($2)" my_click :: Element -> CB.Callback a -> IO()
foreign import javascript unsafe "document.onlick = $1" my_click2 :: CB.Callback a -> IO ()

someFunc :: IO()
someFunc = do
    putStrLn "I am working!" >> putStrLn "and then..."
    Just win <- currentWindow
    asyncCB <- CB.asyncCallback $ do
        my_alert win "my first callback!" >> putStrLn "onload!"
    syncCB <- CB.syncCallback CC.ContinueAsync $ do
        putStrLn " call back!!!"
    --my_onload win asyncCB
    my_onload2 syncCB
    --my_onload2 asyncCB
    --my_click2 asyncCB
    CB.releaseCallback asyncCB
    CB.releaseCallback syncCB
    putStrLn "Over!2"

