  grid is a game written in Haskell
  Copyright ( C ) 2018 
-- 
-- This file is part of grid.
-- 
-- grid is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation , either version 3 of the License , or
-- (at your option) any later version.
-- 
-- grid is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
  You should have received a copy of the GNU General Public License
-- along with grid.  If not, see </>.
--
module Main.GLFW.Load
  (
    mainLoad,

  ) where

import MyPrelude
import File
import LoadM

import Game
import Game.GameData
import LevelTools.EditWorld
import LevelTools.Helpers
import LevelTools.EditWorld.Make
import LevelTools.Iteration
import LevelTools.Make
import LevelTools.File


import OpenGL
import OpenGL.Helpers
import Data.Maybe

import Graphics.UI.GLFW as GLFW
import System.IO


mainLoad path = do
    
    let init = error "no EnvInit"
    
  run MEnv !
    (edit, _) <- runGLFWMEnv init loadGameData unloadGameData 
                             (begin path) iterate ()

    case findInvalidObject edit of
        Nothing                   -> return ()
        Just (room, node, str)  -> do
            putStrLn $ "warning        : level invalid at RoomIx: " ++ show room ++ ", " ++ show node ++
                       " (" ++ str ++ ")"

   
    putStrLn $ "name is " ++ (levelName $ editLevel edit) ++ ". rename? "

    bool <- getBool
    case bool of
        False   -> do
            writeEditAsLevel edit path

        True    -> do
            -- prompt for names and write to file
            putStr $    "name           : "
            hFlush stdout

            name <- getLine
            let edit' = editModifyLevel edit $ \level -> level { levelName = name }
            path <- case name of 
                    []    -> findSimilarFileName "_" ".ldef"
                    name  -> findSimilarFileName (basenameFromString name) ".ldef"

            writeEditAsLevel edit' path
            putStrLn $  "written to file: " ++ path


    where
      getBool = do
          putStr "(no/yes) "
          hFlush stdout

          line <- getLine
          case line of
              "no"    -> return False
              "yes"   -> return True
              _       -> getBool

      begin path _ = do
          io $ do
              -- OpenGL --
  see invariants.txt for GL state
              glClearColor 0 0 0 1

              glEnable gl_BLEND                              
              glBlendFunc gl_SRC_ALPHA gl_ONE_MINUS_SRC_ALPHA 
              glDepthFunc gl_LEQUAL
              glEnable gl_MULTISAMPLE
         
  if first run , create folders and files for dynamic data
          createDynamicData

          edit <- makeEditWorldLoad path

          -- play game
          -- return (edit, (), [])
          return (edit, (), [iterationEdit])

      iterate (a, b, stack) = do
          runABStack a b stack


--------------------------------------------------------------------------------
--  

runABStack :: a -> b -> IterationStack a b -> MEnv' (a, b)
runABStack a b (i:is) = do
    screenBegin
    tickBegin
    keysBegin
    
    (a', b', top) <- (iteration i) a b
    
    keysEnd
    tickEnd
    screenEnd

 -- tmp 
     state    $ GLFW.getKey ( GLFW.SpecialKey GLFW.ESC ) 
     case state of 
       GLFW.Release     - > runABStack a ' b ' ( top + + is ) 
       GLFW.Press       - > return ( a ' , b ' ) 
 -- 

-- tmp
    state <- io $ GLFW.getKey (GLFW.SpecialKey GLFW.ESC)
    case state of
      GLFW.Release    -> runABStack a' b' (top ++ is)
      GLFW.Press      -> return (a', b')
--
-}
    runABStack a' b' (top ++ is)


runABStack a b [] = do
    return (a, b)


 
 This file is part of grid.
 
 grid is free software: you can redistribute it and/or modify
 (at your option) any later version.
 
 grid is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 along with grid.  If not, see </>.

 prompt for names and write to file
 OpenGL --
 play game
 return (edit, (), [])
------------------------------------------------------------------------------
  
 tmp 
 
 tmp

  grid is a game written in Haskell
  Copyright ( C ) 2018 
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation , either version 3 of the License , or
  You should have received a copy of the GNU General Public License
module Main.GLFW.Load
  (
    mainLoad,

  ) where

import MyPrelude
import File
import LoadM

import Game
import Game.GameData
import LevelTools.EditWorld
import LevelTools.Helpers
import LevelTools.EditWorld.Make
import LevelTools.Iteration
import LevelTools.Make
import LevelTools.File


import OpenGL
import OpenGL.Helpers
import Data.Maybe

import Graphics.UI.GLFW as GLFW
import System.IO


mainLoad path = do
    
    let init = error "no EnvInit"
    
  run MEnv !
    (edit, _) <- runGLFWMEnv init loadGameData unloadGameData 
                             (begin path) iterate ()

    case findInvalidObject edit of
        Nothing                   -> return ()
        Just (room, node, str)  -> do
            putStrLn $ "warning        : level invalid at RoomIx: " ++ show room ++ ", " ++ show node ++
                       " (" ++ str ++ ")"

   
    putStrLn $ "name is " ++ (levelName $ editLevel edit) ++ ". rename? "

    bool <- getBool
    case bool of
        False   -> do
            writeEditAsLevel edit path

        True    -> do
            putStr $    "name           : "
            hFlush stdout

            name <- getLine
            let edit' = editModifyLevel edit $ \level -> level { levelName = name }
            path <- case name of 
                    []    -> findSimilarFileName "_" ".ldef"
                    name  -> findSimilarFileName (basenameFromString name) ".ldef"

            writeEditAsLevel edit' path
            putStrLn $  "written to file: " ++ path


    where
      getBool = do
          putStr "(no/yes) "
          hFlush stdout

          line <- getLine
          case line of
              "no"    -> return False
              "yes"   -> return True
              _       -> getBool

      begin path _ = do
          io $ do
  see invariants.txt for GL state
              glClearColor 0 0 0 1

              glEnable gl_BLEND                              
              glBlendFunc gl_SRC_ALPHA gl_ONE_MINUS_SRC_ALPHA 
              glDepthFunc gl_LEQUAL
              glEnable gl_MULTISAMPLE
         
  if first run , create folders and files for dynamic data
          createDynamicData

          edit <- makeEditWorldLoad path

          return (edit, (), [iterationEdit])

      iterate (a, b, stack) = do
          runABStack a b stack



runABStack :: a -> b -> IterationStack a b -> MEnv' (a, b)
runABStack a b (i:is) = do
    screenBegin
    tickBegin
    keysBegin
    
    (a', b', top) <- (iteration i) a b
    
    keysEnd
    tickEnd
    screenEnd

     state    $ GLFW.getKey ( GLFW.SpecialKey GLFW.ESC ) 
     case state of 
       GLFW.Release     - > runABStack a ' b ' ( top + + is ) 
       GLFW.Press       - > return ( a ' , b ' ) 

    state <- io $ GLFW.getKey (GLFW.SpecialKey GLFW.ESC)
    case state of
      GLFW.Release    -> runABStack a' b' (top ++ is)
      GLFW.Press      -> return (a', b')
-}
    runABStack a' b' (top ++ is)


runABStack a b [] = do
    return (a, b)

