module WeatherCommon (Month(JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC),
		      MyDate(MyDate),
		      Prediction,
		      Actual,
		      readDate,
		      prevMonth,
		      prevDate,
		      weatherCommonTests)
		      
    where

import HUnit

data Month
    = JAN
    | FEB
    | MAR
    | APR
    | MAY
    | JUN
    | JUL
    | AUG
    | SEP
    | OCT
    | NOV
    | DEC
    deriving (Eq, Enum, Read, Show)

data MyDate = MyDate (Month, Int)
	    deriving (Eq)

type Prediction = (String, String, String)
type Actual = (Int, Int, String)

instance Show MyDate where
    show (MyDate(m, d)) = (show m) ++ (if d < 10 then "0" else "") ++ (show d)

showMyDateTests =
    TestLabel "showMyDateTest" 
		  (TestList [show (MyDate(JAN, 1)) ~?= "JAN01",
			     show (MyDate(JUN, 30)) ~?= "JUN30"])
					  
readDate :: String -> MyDate
readDate date = 
    let mpart = (take 3 date)
	dpart = (drop 3 date) in
		MyDate( read mpart, read dpart)

readDateTests =
    TestLabel "readDateTest"
	      (TestList [readDate "JAN01" ~?= (MyDate(JAN, 1)),
			 readDate "JUN30" ~?= (MyDate(JUN, 30))])
			  
prevMonth :: Month -> Month
prevMonth JAN = DEC
prevMonth m = pred m

prevMonthTests =
    TestLabel "prevMonthTest"
	      (TestList [prevMonth JAN ~?= DEC,
			 prevMonth JUL ~?= JUN])

prevDate :: MyDate -> MyDate
prevDate (MyDate (m, d)) | d > 1 = MyDate(m, d - 1)
			 | ((m == JAN) || (m == FEB) || (m == APR) ||
			    (m == JUN) || (m == AUG) || (m == SEP) ||
			    (m == NOV)) = MyDate(prevMonth m, 31)
			 | m == MAR = MyDate(FEB, 29)
			 | True = MyDate(prevMonth m, 30)

prevDateTests =
    TestLabel "prevDateTest"
	      (TestList [prevDate (MyDate(MAR, 30)) ~?= (MyDate(MAR, 29)),
			 prevDate (MyDate(JAN, 1)) ~?= (MyDate(DEC, 31)),
			 prevDate (MyDate(MAR, 1)) ~?= (MyDate(FEB, 29)),
			 prevDate (MyDate(JUL, 1)) ~?= (MyDate(JUN, 30))])

weatherCommonTests = TestList [showMyDateTests, readDateTests, prevMonthTests, prevDateTests]
