import Text.XHtml
import Music.Diatonic
import Music.Diatonic.Scale
import Music.Diatonic.Key
import Music.Diatonic.Harmony
import Music.Diatonic.Chord
import List (intersperse, sortBy)
import DD.Html
import System (getArgs)
import Maybe (catMaybes)
import Data.Maybe (maybeToList)
import Data.Function (on)
import qualified Data.Map as M
head' n =
header <<
thetitle << ("Diatonic Dashboard: Note " +++ (displayMusicHTML . show $ n)) +++
thelink ! [rel "stylesheet", thetype "text/css", href "dd.css"] << noHtml
mapKeyDegrees :: Note -> M.Map Degree [Key]
mapKeyDegrees n = foldr (\(d, s) m -> M.insertWith (++) d [key s] m) M.empty degreePairs
where scales = map scale $ majorKeys ++ minorKeys
degreePairs = concatMap (\s -> map (\(d, n') -> (d, s)) . filter (\(d, n') -> n' == n) . degrees $ s) scales
mapChordDegrees :: Note -> M.Map Degree [Chord]
mapChordDegrees n = foldr (\(d, s) m -> M.insertWith (++) d [s] m) M.empty degreePairs
where chords = concatMap (\c -> map ($ c) chordTypes) allNotes
degreePairs = concatMap (\s -> map (\(d, n') -> (d, s)) . filter (\(d, n') -> n' == n) . degrees $ s) chords
mapHarmonyDegrees :: Chord -> M.Map Degree [Key]
mapHarmonyDegrees c = foldr (\(d, k) m -> M.insertWith (++) d [k] m) M.empty degreePairs
where keys = majorKeys ++ minorKeys
degreePairs = concatMap (\k -> map (\(d, c') -> (d, k)) . filter (\(d, c') -> c' == c) . degrees . harmony . scale $ k) keys
dtable :: M.Map Degree [a] -> (a -> Html) -> Html
dtable m f = table ! [theclass "center", cellspacing 2, cellpadding 4] << rows
where rows = displayScaleDegrees ds f
ds = map (\d -> (d, concat . maybeToList $ M.lookup d m))
[First, Second, flat $# Third, Third, Fourth, Fifth, flat $# Sixth, Sixth, flat $# Seventh, Seventh]
main = do
args <- getArgs
let n = read (head args) :: Note
putStrLn . renderHtml $
head' n
+++ body ! [theclass "center"]
<< br +++ h2 << ("Diatonic Dashboard: Note " ++ show n)
+++ br +++ h3 << "\"Circle\" of Fifths"
+++ fifths n id linkNote
+++ br +++ h3 << "Keys" +++ italics ("In the following keys, the note " ++ show n ++ " appears at the specified degree:" +++ br +++ br)
+++ dtable (mapKeyDegrees n) linkKey
+++ br +++ h3 << "Chords" +++ italics ("In the following chords, the note " ++ show n ++ " appears at the specified degree:" +++ br +++ br)
+++ dtable (mapChordDegrees n) linkChord
+++ br +++ br +++ hotlink "index.html" (stringToHtml "[Home]")