{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}



import Prelude hiding (filter)
import Data.Csv -- this is from the Cassava library
import GHC.IO.Exception
import Data.List
import Control.Exception (try)
import qualified Data.ByteString.Lazy as BL
import qualified Data.Vector as V
import Graphics.Matplotlib

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)

data CsvData = 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 :: Either ErrorMsg CsvData -> [Float]
getList maybeCsvData = case maybeCsvData of
  CsvData (_, v) -> map datum v
  ErrorMsg _ -> []

getList parseCsv dataPath

d1 =<< (fmap (\x -> ) (parseCsv dataPath))

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

onscreen $ getList parseCsv dataPath