-- | The `HTMLGenerator' module generates the HTML content for the index.html generated
module Data.GPS.Gps2HtmlReport.HTMLGenerator (
  generateHtmlPage -- :: [WptType] -> Html
  ) where

import Text.Html
import Data.GPS hiding (src,link,href)
import Text.Printf
import Data.Maybe

import Data.GPS.Gps2HtmlReport.JourneyStats (journeyDistance,meanElevation,journeyTime,maxSpeed,meanJourneySpeed,findPoint,dateOfJourney)

-- | Takes all the WayPoints and generates the HTML file
generateHtmlPage :: [WptType] -> Html
generateHtmlPage points = 
   let header1 = h1 $ stringToHtml "Journey Statistics"
       header2 = h1 $ stringToHtml "Charts of the Journey"
       header3 = h1 $ stringToHtml "OpenStreetMap Chart"
       title = thetitle $ stringToHtml "GPX Track Report"
       theStyle = style (stringToHtml cssContent) ! [thetype "text/css"]
       theHeader = header $ concatHtml [title,theStyle]
       mainArea = thediv (concatHtml [header1,statsTable points,br,header2,chartTable,header3,osmImg])
       theBody = body mainArea
   in concatHtml [theHeader,theBody,pgFooter]

-- | The OpenStreetMap image area
osmImg :: Html
osmImg = anchor (image ! [src "osm.png"]) ! [href "osm_fullsize.png"]

-- | Takes all the WayPoints and calculates the journey statistics
statsTable :: [WptType] -> Html
statsTable points = 
   let tblHeader1 = th $ stringToHtml "Journey Details"
       tblHeader2 = th $ stringToHtml "Elevation"
       tblHeader3 = th $ stringToHtml "Speed"
       distTravelled = printf "%.2f" $ journeyDistance points
       maybeMaxElePt = findPoint points (head points) ele (>)
       maybeMinElePt = findPoint points (head points) ele (<)
       maxElevation = printf "%.1f" (if isJust maybeMaxElePt then snd $ fromJust maybeMaxElePt else 0.0)
       minElevation = printf "%.1f" (if isJust maybeMinElePt then snd $ fromJust maybeMinElePt else 0.0)
       meanEle = printf "%.1f" $ meanElevation points
       journeyMins = show $ round (journeyTime points) `div` 60
       journeySecs = show $ round (journeyTime points) `rem` 60
       mxSpd = printf "%.1f" $ maxSpeed points
       meanSpd = printf "%.1f" $ meanJourneySpeed points
       maybeDateOfJourney = dateOfJourney points
       journeyDate = (if isJust maybeDateOfJourney then show $ fromJust maybeDateOfJourney else "") 
       li1c1 = li $ stringToHtml ("Journey Date: "++ journeyDate)
       li2c1 = li $ stringToHtml ("Distance Travelled: "++ distTravelled++"m")
       li3c1 = li $ stringToHtml ("Journey Time: "++journeyMins++"m "++journeySecs++"s")
       li1c2 = li $ stringToHtml ("Maximum Elevation: "++ maxElevation++"m")
       li2c2 = li $ stringToHtml ("Minimum Elevation: "++minElevation++"m")
       li3c2 = li $ stringToHtml ("Mean Elevation: "++ meanEle++"m")
       li1c3 = li $ stringToHtml ("Maximum speed: "++ mxSpd++"m/s")
       li2c3 = li $ stringToHtml ("Mean speed: "++meanSpd++"m/s")
       col1 = td (concatHtml [li1c1,li2c1,li3c1]) ! [valign "top"]
       col2 = td  (concatHtml [li1c2,li2c2,li3c2]) ! [valign "top"]
       col3 = td  (concatHtml [li1c3,li2c3]) ! [valign "top"]
       row1 = tr $ concatHtml [tblHeader1,tblHeader2,tblHeader3]
       row2 = tr $ concatHtml [col1,col2,col3]
       tbl = table (concatHtml [row1,row2]) -- ! [cellspacing 10]
   in center tbl
   
-- | The CSS style text to format the rendering of the HTML page. It would be good to replace with Haskell HTML combinator library functions

cssContent = "h1 {color: #2C558A; font-weight: normal; font-size: x-large; text-shadow: white 0px 1px 1px; letter-spacing: 0.1em; font-family: 'Gill Sans', 'PT Sans', 'Arial', sans-serif; text-transform: uppercase;} div {    width: 900px; margin-top: 50px; margin:0 auto;} table {border-spacing: 20px 0px;} footer {text-align:right; background-color:#EEEEEE; width:900px; margin:0 auto; margin-top: 30px}"


-- | The area holding the Cairo charts
chartTable = 
   let img1 = image ! [src "chart1.png"]
       img2 = image ! [src "chart2.png"]
       cell1 =  td img1
       cell2 = td img2
       row1 = tr $ concatHtml [cell1,cell2]
       tbl = table row1
   in center tbl

-- | The footer
pgFooter = 
   let projectLink = anchor (stringToHtml "gps2htmlReport") ! [href "https://github.com/robstewart57/Gps2HtmlReport"]
       infoStr = stringToHtml "Report generated by "
   in footer (concatHtml [infoStr,projectLink]) ! [identifier "main"]

-- | This appears to be missing from the `html' package
footer = tag "FOOTER"