import Allie
import Geom
import Control.Monad (forM_)

goodColorOfCube :: (Integral a) => a -> Vec3
goodColorOfCube i = ((Vec3 100 0 0) ^*^ t) ^+^ ((Vec3 0 50 90) ^*^ (1 - t)) where t = ((fromIntegral i :: Float) / 4)

goodLightPos :: (Integral a) => a -> Vec3
goodLightPos i = ((Vec3 0 2 1) ^*^ t) ^+^ ((Vec3 5 1 1) ^*^ (1 - t)) where t = ((fromIntegral i :: Float) / 4)

main :: IO()
main = do
  alice <- newAlice
  aliceSetSkyColor alice (Vec4 0.9 0 0.6 1)
  face <- aliceNewLucyFace alice "src/l3/fonts/DMSerifText-Regular.ttf"
  faceOf40 <- aliceLucyFaceOfSize face 40
  lucyFaceAddGlyphs faceOf40 32 (126 - 32 + 1)
  aliceAddText alice faceOf40 (Vec4 1 1 0 1) "Privet" 100 200

  weirdStructure <- aliceAddGenericMeshHand alice "gen/l2/models/log_10_2_6.AliceGenericMesh"
    "src/l3/textures/log_10_2_6_diffuse.png" "gen/l2/textures/log_10_2_6_NORMAL.png" "src/l3/textures/log_10_2_6_specular.png"
  aliceGenericMeshResizeInstanceArr alice weirdStructure 1
  aliceGenericMeshSetInst weirdStructure 0 (AliceGenericMeshInstance (mat4Transit (Vec3 (-3.0) (-2.0) (-5.0))))

  cube <- aliceAddShinyMeshHand alice "gen/l2/models/cube.AliceShinyMesh"
  aliceShinyMeshResizeInstanceArr alice cube 5
  

  aliceSetPointLightCount alice 5
  forM_ [0..4] $ \i -> do
    aliceShinyMeshSetInst cube i (AliceShinyMeshInstance (mat4Transit (goodLightPos i)) (Vec3 1 1 1) (goodColorOfCube i))  
    aliceSetPointLight alice (fromIntegral i) (AlicePointLight (goodLightPos i) (goodColorOfCube i))

  -- state <- newIORef 67
  -- Create the Callbacks structure.
  let callbacks = Callbacks myonKeyboardKey myonAnotherFrame where
      myonKeyboardKey keysym keyAction = do
        -- old <- readIORef state
        -- writeIORef state (old + 1)
        putStrLn ("Got a keypress") 
      myonAnotherFrame fl = do
        oldPos <- aliceGetCamPos alice
        goForward <- aliceIsPressed alice 0x77
        if goForward
            then do
                backDir <- aliceGetCamBack alice
                aliceSetCamPos alice (oldPos ^+^ (backDir ^*^ (-fl * 10)))
            else return ()

        --cur <- readIORef state
        --aliceClearScreenTextLabel alicePerm
        --aliceAddScreenTextLabel alicePerm ("Current value is = " ++ show cur)
  

  -- Allocate space for the struct, poke it, and pass to C.
  aliceMainloop alice callbacks
