-- | This contains the main program executable for the program when built with
-- the Cairo libary
module GIS.Exe.OptCairo
    ( -- * Data types
    Program (..)
    -- * Functions
    , exec
    ) where

import           Control.Lens
import           Data.Semigroup
import           GIS.Exe.Parser       hiding (projection)
import           GIS.Graphics.PlotPNG
import           GIS.Graphics.Types   hiding (projection)
import           GIS.Hylo
import           Options.Applicative
import           System.Directory

-- | Main executable; parses command line options and runs program
exec :: IO ()
exec = pick =<< execParser helpDisplay

-- | Execute the program with parsed command-line input
pick :: Program -> IO ()
pick (Program (MapMaker outfile False projection) infile) = let p = pickProjection projection in
    mkMap outfile =<< districtToMapP p <$> getDistricts infile -- svg now idk?
pick (Program (MapMaker _ True projection) infile) = let p = pickProjection projection in
    makeFoldersPng =<< districtToMapFilesP p <$> getDistricts infile -- TODO fix this
pick (Program (Computation comp Nothing) infile) = --slightly wrong but eh.
    case comp of
        "perimeter" -> putStrLn =<< districtPerimeter <$> getDistricts infile
        "area" -> putStrLn =<< districtArea <$> getDistricts infile
        "compactness" -> putStrLn =<< districtCompactness <$> getDistricts infile
        _ -> putStrLn "computation not recognized"
pick (Program (MapLabel outfile False _ lensName) infile) =
    mkLensMap "" outfile (pickLens lensName) =<< getDistricts infile -- FIXME make it work with -a (generate-all) and make it not bind things together??
pick _ = error "not yet supported."

-- | Make maps as png files.
makeFoldersPng :: [Map] -> IO ()
makeFoldersPng maps = do
    createDirectoryIfMissing False "maps"
    mapM_ (\m -> mkMapPng ("maps/" <> view title m <> ".png") m) maps