{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
import ShadowLibrary.Core

import Text.XML.HXT.Core
import Text.XML.HXT.XPath
-- import Text.XML.HXT.Curl
import Data.List
import Data.List.Utils (replace)

import Text.Regex.Posix
import Text.Printf


extractNestedRecords = extractLinksWithText "//a[@class='archiveVolume' and not(contains(@href, '.PDF')) and not(contains(@href, '.pdf'))]"  -- pary adres-tytuł
                 >>> second (arr $ replace "%09" "") -- czyścimy drugi element pary, czyli tytuł z niepotrzebnych białych znaków
                 >>> first (extractLinksWithText "//a[contains(@href,'.pdf')]") -- pobieramy stronę z adresu URL i wyciągamy linki z tej strony pasujące do wyrażenia XPathowego
                 -- ostatecznie wyjdą trójki ((adres URL, tytuł artykułu), tytuł rocznika)
                 
extractRecords = extractLinksWithText "//a[@class='archiveVolume' and (contains(@href,'.pdf') or contains(@href,'.PDF'))]" 
	    >>> second (arr $ replace "\t" " ")                

-- ... a tutaj te trójki przerabiamy do docelowej struktury ShadowItem
toShadowItem :: ((String, String), String) -> ShadowItem
toShadowItem ((url, articleTitle), yearlyTitle) =
  (defaultShadowItem url title) {
    originalDate = Just date,
    itype = "periodical",
    format = Just "pdf",
    finalUrl = url
    }
  where title = "Zagadnienia ekonomiki rolnej " ++ (replace "\t" "" (replace "\r\n" "" yearlyTitle)) ++ " " ++ (replace "\t" "" (replace "\r\n" "" (replace "  " "" articleTitle)))
	date = getDate yearlyTitle

toShadowItemTop :: (String, String) -> ShadowItem
toShadowItemTop (url, articleTitle) =
  (defaultShadowItem url title) {
    originalDate = Just date,
    itype = "periodical",
    format = Just "pdf",
    finalUrl = url
    }
  where title = "Zagadnienia ekonomiki rolnej " ++ " " ++ (replace "\t" "" (replace "\r\n          " "" articleTitle))
	date = getDateTop url

	
getDate url =
  case url =~~ "[0-9]?[0-9]/(19[0-9][0-9]|20[0-9][0-9])" :: Maybe [[String]] of
    Just [[month, year]] -> month
    otherwise -> error $ "unexpected url: " ++ url

getDateTop url =
  case url =~~ "[0-9]*(19[0-9][0-9]|20[0-9][0-9])" :: Maybe [[String]] of
    Just [[_, year]] -> year
    otherwise -> error $ "unexpected url: " ++ url

main = do
    let start = "http://www.zer.waw.pl/Archive"
    let shadowLibrary = ShadowLibrary {logoUrl=Nothing,
                                       lname="Ekonomika Rolna",
                                       abbrev="EkonRol",
                                       lLevel=0,
                                       webpage=start}
    extractItemsStartingFromUrl shadowLibrary start (extractNestedRecords >>> arr toShadowItem)
    extractItemsStartingFromUrl shadowLibrary start (extractRecords >>> arr toShadowItemTop)