module Action where

import Storage
import Glue

generateSolutions :: IO ()
generateSolutions = do
  prepareDB dbName
  mRest <- dbDo getRest
  case mRest of
    Nothing -> do
      (sols, rest) <- return $ firstSolutions 1
      dbDo (addSolutions sols)
      dbDo (replaceRest rest)
    Just rest -> do
      (sols, newRest) <- return $ makeSolutions 1 rest
      dbDo (addSolutions sols)
      dbDo (replaceRest newRest)

generatePuzzles :: IO ()
generatePuzzles = do
  prepareDB dbName
  sols <- dbDo getSolutions
  puzzles <- return $ makePuzzles sols
  dbDo (addPuzzles puzzles)

generateGradedPuzzles :: IO ()
generateGradedPuzzles = do
  prepareDB dbName
  sols <- dbDo getSolutions
  gradedPuzzles <- return $ makeAndGradePuzzles sols
  (sols, gradedList) <- return $ unzip gradedPuzzles
  puzzles <- return $ map (\graded -> fst $ unzip graded) gradedList
  dbDo (addPuzzles (zip sols puzzles))
  dbDo (addGrades (concat gradedList))
