module Villefort.Time  where

import Data.List.Split as S
import Data.Time
import Data.Time.Calendar.OrdinalDate

data D = D { year :: Integer,
             month :: Int,
             day :: Int} deriving (Show)

fromZonedTimeToDay :: String -> Day
fromZonedTimeToDay x = fromGregorian (year up) (month up ) (day up)
  where up = unpackStringToDate x

unpackStringToDate :: [Char] -> D
unpackStringToDate x = D (read (nums !! 0) :: Integer) (read (nums !! 1) :: Int) (read (nums !! 2) :: Int)
  where nums = S.splitOn "-" $  take 10 x
  
  
daysUntil :: [Char] -> IO Integer
daysUntil date = do
  let split = S.splitOn "-" date
  current <- fromZonedTimeToDay <$> show <$> getZonedTime
  let due     = fromGregorian (read (split !! 0) :: Integer) (read (split !! 1) :: Int) (read (split !! 2) :: Int)
  return $ (diffDays  due current) 

getDate :: IO Day
getDate = fromZonedTimeToDay <$> show <$> getZonedTime

getDateD = unpackStringToDate <$> show <$> getZonedTime

getDay :: IO Int
getDay = do
  z <- getDate
  return $ snd $mondayStartWeek z


getStartOfWeek :: IO Day
getStartOfWeek = do
  currentDay <- toInteger <$> getDay
  today <- getDate
  return $ addDays (-currentDay) today
 
 
getDatesOfWeek :: IO [Day]
getDatesOfWeek = do
  start <- getStartOfWeek
  currentDay <- getDay
  return $ take currentDay $ scanl next start [1,1 .. ]
  where next  day x = addDays (x) day