{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
-- This requires my custom BootTidal.hs

import Prelude hiding (filter)
import qualified Data.ByteString.Lazy as BL
import Data.Csv -- this is from the Cassava library
import qualified Data.Vector as V
-- import System.Directory (doesFileExist)
-- import Text.Printf
-- import Control.Monad (forM_)
import GHC.IO.Exception
import Control.Exception (try)


data TimestampedDatum = TimestampedDatum {
    timestamp :: String,
    datum :: Float
} deriving Show

dataPath = "../data/weather/balcony_temperature.csv"

instance FromNamedRecord TimestampedDatum where
  parseNamedRecord r = 
    TimestampedDatum 
    <$> r .: "time" 
    <*> r .: "C.value"


type ErrorMsg = String
type CsvData = (Header, V.Vector TimestampedDatum)

parseCsv :: FilePath -> IO (Either ErrorMsg CsvData)
parseCsv filePath = do
  result <- try $ BL.readFile filePath
  return $ case result of
             Left (exception :: IOException) -> Left $ show exception
             Right contents -> decodeByName contents

getList :: CsvData -> V.Vector TimestampedDatum
getList = snd

-- getValue :: TimestampedDatum -> Float
-- getValue = datum
