module GIS.Exe.Opt where

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

pick :: Program -> IO ()
pick (Program (MapMaker outfile False projection) infile) = let p = pickProjection projection in
    mkMapSVG outfile =<< districtToMapP p <$> getDistricts infile
pick (Program (MapMaker _ True projection) infile) = let p = pickProjection projection in
    makeFoldersSVG =<< districtToMapFilesP p <$> getDistricts infile
pick (Program (MapLabel outfile False projection lensName) infile) = let p = pickProjection projection in
    mkMapSVG outfile =<< districtToMapLensP p (pickLens lensName) <$> getDistricts infile -- FIXME make it work with -a (generate-all)
pick _ = error "not yet implemented."

exec :: IO ()
exec = pick =<< execParser helpDisplay

makeFoldersSVG :: [Map] -> IO ()
makeFoldersSVG maps = do
    createDirectoryIfMissing False "maps"
    mapM_ (\m -> mkMapSVG ("maps/" <> view title m <> ".svg") m) maps