summaryrefslogtreecommitdiff
path: root/src/Lib.hs
blob: 6158602ebda93ec4ffb71bc77b5569a9cc1dced0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase                 #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE NamedFieldPuns             #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RankNTypes                 #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE TypeOperators              #-}
{-# LANGUAGE UndecidableInstances       #-}

module Lib
    ( startApp
    , app
    , createDomain
    , withTx
    , makeDefaultGraphSpec
    , DomainName(..)
    ) where

import           Control.Exception                (throw)
import qualified Control.Exception                as E
import qualified Control.Exception.Safe           as CES
import           Control.Monad
import           Control.Monad.IO.Class
import           Data.Bits
import           Data.Char
import           Data.List
import qualified Data.Map.Strict                  as M
import           Data.Maybe
import qualified Data.Set                         as S
import           Data.String.Conversions          (cs)
import qualified Data.Text                        as T
import           Database.SQLite.Simple
import           Database.SQLite.Simple.FromField
import           Database.SQLite.Simple.ToField
import           GHC.Generics
import           Lucid
import           Network.Wai.Handler.Warp
import           Numeric
import           Servant                          hiding ((:.))
import           Servant.HTML.Lucid
import           System.Random.Stateful
import           Web.FormUrlEncoded

newtype DomainIden = DomainIden Integer deriving (FromField, FromHttpApiData, ToHttpApiData, ToField)
newtype LineIden = LineIden Integer deriving (Eq, Ord, FromField, FromHttpApiData, ToHttpApiData, ToField, Generic, Num)
newtype Color = Color T.Text deriving (FromField, ToHttpApiData, ToField)

hexDigits :: T.Text
hexDigits = "0123456789abcdef"

instance Uniform Color where
    uniformM g = do
        indices <- replicateM 6 (uniformRM (0, 15) g)
        return $ Color $ T.pack $ fmap (T.index hexDigits) indices

sampleColor :: StatefulGen g m => g -> m Color
sampleColor g = iterateUntil (not . isLight) (uniformM g) -- unbounded loop OK in expectation
    where iterateUntil f m = m >>= (\x -> if f x then return x else iterateUntil f m)

instance FromHttpApiData Color where
    parseUrlPiece "" = Left "color is empty"
    parseUrlPiece s
        | 6 /= T.length t = Left "color not 6 characters"
        | not $ T.all isHexDigit t = Left "color not hex"
        | otherwise = Right (Color t)
        where t = T.drop 1 s

newtype LineText = LineText T.Text deriving (FromField, ToHtml, ToHttpApiData, ToField)
instance FromHttpApiData LineText where
    parseUrlPiece s
        | T.length s < 1 = Left "line text empty"
        | T.length s > 120 = Left "line text over 120 characters"
        | not $ T.all isPrint s = Left "line text not printable"
        | otherwise = Right $ LineText s

newtype DomainName = DomainName T.Text deriving (Eq, FromField, ToHttpApiData, ToHtml, ToField)
instance FromHttpApiData DomainName where
    parseUrlPiece s
        | T.length s < 1 = Left "subgraph name empty"
        | T.length s > 120 = Left "subgraph name over 120 characters"
        | not $ T.all isAscii s = Left "subgraph name not ASCII"
        | not $ T.all (\c -> isAlphaNum c || T.isInfixOf (T.singleton c) "-_.") s =
            Left "subgraph name not in [A-Za-z0-9] or -, _, ."
        | otherwise = Right (DomainName s)

data DomainNamePart
  = Oxbow | Limber | Steadfast | Regicide | Swarm | Weave | Bough | Canopy | Herald | Scorn |
    Alder | Aerial | Welkin | Acrid | Kindling | Rapture | Myrtle | Envy | Solstice |
    Juniper | Cleaving | Stream | Reaper | Sluice | Conduit | Disdain | Sylvan | Ravish | Atrium |
    Thresh | Harvest | Water | Renewal | Rosy | Frieze | Portal | Vespers | Litany | Serpent |
    Primate | Incite | Canon | Acquiese | Mirror | Script | Seal | Privy |
    Piercing | Heresy | Subduct | Sceptre | Arrogance | Ivory | Accrete | Cluster |
    Sepulchre | Summon | Pleading | Myriad | Exalted | Sentry | Shriven | River | Threshold
    deriving (Enum, Show, Bounded)

randomDomainName :: StatefulGen g m => g -> m DomainName
randomDomainName g = do
    parts <- replicateM 8 (randomPart g)
    return $ DomainName $ T.intercalate "-" (fmap (T.pack . show) parts)
    where
        randomPart :: StatefulGen g m => g -> m DomainNamePart
        randomPart g' = uniformRM (0, length [(minBound :: DomainNamePart)..maxBound] - 1) g' >>= (return . toEnum)

data Line = Line LineIden Color LineText
instance FromRow Line where
    fromRow = Line <$> field <*> field <*> field
data DomainedLine = DomainedLine DomainName Line
data Domain = Domain DomainIden DomainName
data DomainInfo = DomainInfo { numNodes :: Integer, numEdges :: Integer }

-- a Verse is a list of lines that form a path in the graph
newtype Verse = Verse [Line]
newtype VerseSpec = VerseSpec [LineIden]
instance ToHttpApiData VerseSpec where
    toUrlPiece (VerseSpec lineIdens) = T.intercalate "-" $ fmap toUrlPiece lineIdens
instance FromHttpApiData VerseSpec where
    parseUrlPiece s = VerseSpec <$> mapM parseUrlPiece (T.splitOn "-" s)

data WriteSubmitForm = WriteSubmitForm { src   :: Maybe LineIden
                                       , dst   :: Maybe LineIden
                                       , txt   :: LineText
                                       , color :: Color } deriving (Generic)
instance FromForm WriteSubmitForm

data HomeView = HomeView
data DomainView = DomainView Domain DomainInfo Bool
data DomainReadView = DomainReadView DomainName (Maybe Verse)
data DomainWriteView = DomainWriteView DomainName (Maybe Line) (Maybe Line) Color
data DomainWalkView = DomainWalkView DomainName (Maybe (Line, [Line], [Line]))

type HomeAPI = "spectral-renga" :> Get '[HTML] HomeView
type NewDomainAPI = "spectral-renga" :> "new-subgraph" :> Get '[HTML] NoContent
type DomainAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :> Get '[HTML] DomainView
type DomainReadAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :>
    "read" :> QueryParam "verse" VerseSpec :> Get '[HTML] DomainReadView
type DomainWriteAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :>
    "write" :> QueryParam "src" LineIden :> QueryParam "dst" LineIden :>
        QueryParam "init" () :> Get '[HTML] DomainWriteView
type DomainWriteSubmitAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :>
    "write-submit" :> ReqBody '[FormUrlEncoded] WriteSubmitForm :> Post '[HTML] NoContent
type DomainWalkAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :>
    "walk" :> QueryParam "line" LineIden :> Get '[HTML] DomainWalkView
type DomainClearAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :> "clear" :> Post '[HTML] NoContent
type DomainResetAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :> "reset" :> Post '[HTML] NoContent
type DomainDeleteAPI = "spectral-renga" :>  "g" :> Capture "domainName" DomainName :> "delete" :> Post '[HTML] NoContent
type API = HomeAPI
      :<|> NewDomainAPI
      :<|> DomainAPI
      :<|> DomainReadAPI
      :<|> DomainWriteAPI
      :<|> DomainWriteSubmitAPI
      :<|> DomainWalkAPI
      :<|> DomainClearAPI
      :<|> DomainResetAPI
      :<|> DomainDeleteAPI
      :<|> "spectral-renga" :> "static" :> Raw

startApp :: IO ()
startApp = run 8080 app

app :: Application
app = serve api $ hoistServer api handleErrors server

handleErrors :: Handler a -> Handler a
handleErrors f = CES.catches f [ CES.Handler (loggify handleAppException)
                               , CES.Handler (loggify handleDbException)
                               , CES.Handler (loggify handleArbitraryException)]

api :: Proxy API
api = Proxy

server :: Server API
server = handleHome
    :<|> handleNewDomain
    :<|> handleDomain
    :<|> handleDomainRead
    :<|> handleDomainWrite
    :<|> handleDomainWriteSubmit
    :<|> handleDomainWalk
    :<|> handleDomainClear
    :<|> handleDomainReset
    :<|> handleDomainDelete
    :<|> serveDirectoryWebApp "static/"

redirectTo :: T.Text -> Handler a
redirectTo uri = throwError err302 { errHeaders=[("Location", cs uri)] }

------------------------------

publicDomainName :: DomainName
publicDomainName = DomainName "piazza"

rejectOnPublicDomain :: DomainName -> IO ()
rejectOnPublicDomain domainName = when (domainName == publicDomainName) $ throw IllegalOperation

data AppException = IllegalOrphanException | DomainNameCreationFailure | VerseSpecTooLong | IllegalOperation deriving (Show)
instance E.Exception AppException

data DbException = NotFoundException | EmptyVerseException | BrokenVerseException deriving (Show)
instance E.Exception DbException

loggify :: E.Exception b => (b -> Handler a) -> b -> Handler a
loggify f ex = do
    liftIO $ print ex
    f ex

handleArbitraryException :: E.SomeException -> Handler a
handleArbitraryException _ = throwError err500 {errBody="500: An unexpected error occurred."}

handleAppException :: AppException -> Handler a
handleAppException IllegalOrphanException =
    throwError err400 {errBody="400: Cannot create an orphaned node in a nonempty subgraph."}
handleAppException DomainNameCreationFailure =
    throwError err500 {errBody="500: Failed to create subgraph name."}
handleAppException VerseSpecTooLong =
    throwError err400 {errBody="400: Verse specification is too long."}
handleAppException IllegalOperation =
    throwError err403 {errBody="403: You can't do that."}

handleDbException :: DbException -> Handler a
handleDbException NotFoundException =
    throwError err404 {errBody="404: The requested resource could not be found."}
handleDbException EmptyVerseException =
    throwError err400 {errBody="400: Cannot display an empty verse."}
handleDbException BrokenVerseException =
    throwError err400 {errBody="400: The requested lines do not compose a path in the subgraph."}

withDb :: (Connection -> IO a) -> IO a
withDb = withConnection "spectralrenga.db"

withTx :: (Connection -> IO a) -> IO a
withTx f = withDb $ \conn -> withTransaction conn (f conn)

handleHome :: Handler HomeView
handleHome = return HomeView

-- Thread g?
oneNewDomain :: Connection -> IO (Maybe DomainName)
oneNewDomain conn = do
    g <- newStdGen >>= newIOGenM
    domainName <- randomDomainName g
    resolveDomainName conn domainName >>= \case
      Nothing -> return (Just domainName)
      Just _  -> return Nothing

getDomainUntil :: Connection -> IO DomainName
getDomainUntil conn = getDomainUntil' conn 16

getDomainUntil' :: Connection -> Int -> IO DomainName
getDomainUntil' _ 0 = throw DomainNameCreationFailure
getDomainUntil' conn n =
    oneNewDomain conn >>= \case
      Just domain' -> return domain'
      Nothing      -> getDomainUntil' conn (n-1)

handleNewDomain :: Handler NoContent
handleNewDomain = do
    newDomainName <- liftIO $ withDb getDomainUntil
    redirectTo $ domainURI newDomainName

handleDomain :: DomainName -> Handler DomainView
handleDomain domainName = do
    g <- liftIO $ newStdGen >>= newIOGenM
    graphSpec <- liftIO $ makeDefaultGraphSpec g
    (domain, domainInfo) <- liftIO $ dbFn domainName graphSpec
    return (DomainView domain domainInfo (domainName /= publicDomainName))
    where
        dbFn domainName' graphSpec' = withDb $ \conn -> withTransaction conn $ do
            domain <- resolveDomainName conn domainName' >>= \case
              Nothing     -> createDomain conn domainName' graphSpec'
              Just domain -> return domain
            domainInfo <- getDomainInfo conn domain
            return (domain, domainInfo)

maxReadLength :: Int
maxReadLength = 16 -- verse could be potentially infinite due to loops, so take a prefix
handleDomainRead :: DomainName -> Maybe VerseSpec -> Handler DomainReadView
handleDomainRead domainName Nothing =
    liftIO (dbFn domainName) >>= \case
      Nothing -> return $ DomainReadView domainName Nothing
      Just verse -> let lineIdens = fmap (\(Line lineIden _ _) -> lineIden) verse in
                        redirectTo $ domainReadURI domainName (Just (VerseSpec lineIdens))
    where
        dbFn domainName' = withTx $ \conn -> do
            domain <- mustResolveDomainName conn domainName'
            src <- randomLine conn domain
            mapMaybeM src (collectUntil maxReadLength (getRandomSuccessor conn domain))
handleDomainRead _ (Just (VerseSpec [])) = throwError err404 { errBody="Need some lines!" }
handleDomainRead domainName (Just verseSpec@(VerseSpec lineIdens)) = do
    liftIO $ when (length lineIdens > maxReadLength) $ throw VerseSpecTooLong
    verse <- liftIO $ dbFn domainName
    return $ DomainReadView domainName (Just verse)
    where
        dbFn domainName' = withTx $ \conn -> do
            domain <- mustResolveDomainName conn domainName'
            resolveVerseSpec conn domain verseSpec

handleDomainWrite :: DomainName -> Maybe LineIden -> Maybe LineIden -> Maybe () -> Handler DomainWriteView
handleDomainWrite domainName Nothing Nothing Nothing = do
    (src, dst) <- liftIO $ dbFn domainName
    r <- liftIO $ randomRIO (0, 3) :: Handler Integer
    let srcLineIden = if r <= 2 then getLineIden <$> src else Nothing
    let dstLineIden = if r <= 1 || r == 3 then getLineIden <$> dst else Nothing
    let initFlag = if isNothing srcLineIden && isNothing dstLineIden then Just () else Nothing
    redirectTo $ domainWriteURI domainName srcLineIden dstLineIden initFlag
    where
        getLineIden (Line lineIden _ _) = lineIden
        dbFn domainName' = withTx $ \conn -> do
            domain <- mustResolveDomainName conn domainName'
            (,) <$> randomLine conn domain <*> randomLine conn domain
handleDomainWrite domainName msrcLineIden mdstLineIden _ = do
    (src, dst) <- liftIO $ dbFn domainName msrcLineIden mdstLineIden
    color <- liftIO $ newStdGen >>= newIOGenM >>= sampleColor
    return $ DomainWriteView domainName src dst color
    where
        dbFn domainName' msrcLineIden' mdstLineIden' = withTx $ \conn -> do
            domain <- mustResolveDomainName conn domainName'
            src <- mapMaybeM msrcLineIden' (getLineById conn domain)
            dst <- mapMaybeM mdstLineIden' (getLineById conn domain)
            return (src, dst)

handleDomainWriteSubmit :: DomainName -> WriteSubmitForm -> Handler NoContent
handleDomainWriteSubmit domainName form = do
    (Line newLineIden _ _) <- liftIO $ dbFn domainName form
    redirectTo $ domainWalkURI domainName (Just newLineIden)
    where
        dbFn domainName' WriteSubmitForm{src=srcLineIden, dst=dstLineIden, txt, color} = withTx $ \conn -> do
            domain <- mustResolveDomainName conn domainName'
            when (isNothing srcLineIden && isNothing dstLineIden) $ do
                c <- numLinesInDomain conn domain
                -- an orphaned node can only be inserted into an empty domain
                when (c > 0) $ throw IllegalOrphanException
            newLine <- addLine conn domain txt color
            case srcLineIden of
              Nothing -> return ()
              Just srcLineIden' -> getLineById conn domain srcLineIden' >>= (\x -> addArrow conn domain x newLine)
            case dstLineIden of
              Nothing -> return ()
              Just dstLineIden' -> getLineById conn domain dstLineIden' >>= addArrow conn domain newLine
            return newLine

handleDomainWalk :: DomainName -> Maybe LineIden -> Handler DomainWalkView
handleDomainWalk domainName Nothing =
    liftIO (dbFn domainName) >>= \case
      Nothing -> return $ DomainWalkView domainName Nothing
      Just (Line lineIden _ _) -> redirectTo $ domainWalkURI domainName (Just lineIden)
    where
        dbFn domainName' = withTx $ \conn -> do
            domain <- mustResolveDomainName conn domainName'
            randomLine conn domain
handleDomainWalk domainName (Just lineIden) =
    DomainWalkView domainName . Just <$> liftIO (dbFn domainName lineIden)
    where
        dbFn domainName' lineIden' = withTx $ \conn -> do
            domain <- mustResolveDomainName conn domainName'
            line <- getLineById conn domain lineIden'
            prevs <- getPrevs conn domain line
            nexts <- getNexts conn domain line
            return (line, prevs, nexts)

handleDomainClear :: DomainName -> Handler NoContent
handleDomainClear domainName = managingHandler clearDomain (domainURI domainName) domainName

handleDomainReset :: DomainName -> Handler NoContent
handleDomainReset domainName = do
    g <- liftIO $ newStdGen >>= newIOGenM
    graphSpec <- liftIO $ makeDefaultGraphSpec g
    managingHandler (\c d -> resetDomain c d graphSpec) (domainURI domainName) domainName

handleDomainDelete :: DomainName -> Handler NoContent
handleDomainDelete = managingHandler deleteDomain homeURI

managingHandler :: (Connection -> Domain -> IO ()) -> T.Text -> DomainName -> Handler NoContent
managingHandler f uri = \domainName -> do
    liftIO $ rejectOnPublicDomain domainName
    liftIO (dbFn domainName)
    redirectTo uri
        where
            dbFn domainName' = withTx $ \conn -> do
                domain <- mustResolveDomainName conn domainName'
                f conn domain

-------------------------

resolveDomainName :: Connection -> DomainName -> IO (Maybe Domain)
resolveDomainName conn domainName = do
    ret <- headMay <$> query conn "SELECT iden \
                                  \FROM domains \
                                  \WHERE name=?" (Only domainName)
    case ret of
        Nothing -> return Nothing
        Just (Only domainIden) -> return . Just $ Domain domainIden domainName

mustResolveDomainName :: Connection -> DomainName -> IO Domain
mustResolveDomainName conn domainName =
    resolveDomainName conn domainName >>= \case
      Nothing     -> throw NotFoundException
      Just domain -> return domain

createDomain :: Connection -> DomainName -> GraphSpec -> IO Domain
createDomain conn domainName graphSpec = do
    execute conn "INSERT INTO domains (name) VALUES(?)" (Only domainName)
    domainIden <- DomainIden . fromIntegral <$> lastInsertRowId conn
    let domain = Domain domainIden domainName
    insertGraphSpec conn domain graphSpec
    return domain

getDomainInfo :: Connection -> Domain -> IO DomainInfo
getDomainInfo conn (Domain domainIden _) = do
    (Only numNodes) <- query conn "SELECT COUNT(*) \
                                   \FROM lines \
                                   \WHERE domain_iden=?" (Only domainIden) >>= mustHead
    (Only numEdges) <- query conn "SELECT COUNT(*) \
                                   \FROM arrows \
                                   \WHERE domain_iden=?" (Only domainIden) >>= mustHead
    return $ DomainInfo { numNodes=numNodes, numEdges=numEdges }

getLineById :: Connection -> Domain -> LineIden -> IO Line
getLineById conn (Domain domainIden _) lineIden =
    headMay <$> query conn "SELECT lines.iden, color, txt \
                           \FROM lines \
                           \WHERE domain_iden=? AND lines.iden=?" (domainIden, lineIden) >>= \case
      Nothing -> throw NotFoundException
      Just line -> return line

siblingPairs :: [a] -> [(a, a)]
siblingPairs []         = []
siblingPairs [_]        = []
siblingPairs (x:y:rest) = (x, y):siblingPairs (y:rest)

resolveVerseSpec :: Connection -> Domain -> VerseSpec -> IO Verse
resolveVerseSpec _ _ (VerseSpec []) = throw EmptyVerseException
resolveVerseSpec conn domain (VerseSpec [x]) = Verse . (: []) <$> getLineById conn domain x
resolveVerseSpec conn (Domain domainIden _) (VerseSpec lineIdens) = do
    let spairs = siblingPairs lineIdens
        spairsFlat = concatMap (\(a, b) -> [a, b]) spairs
        queryFragment = "(" <>
                T.intercalate " OR " (replicate (length spairs) "(a.iden=? AND b.iden=?)") <>
            ")"
    rows <- query conn (Query $ "SELECT a.iden, a.color, a.txt, b.iden, b.color, b.txt \
                                \FROM lines a \
                                \INNER JOIN lines b USING(domain_iden) \
                                \INNER JOIN arrows USING(domain_iden) \
                                \WHERE arrows.src=a.iden AND arrows.dst=b.iden \
                                \AND domain_iden=? AND " <> queryFragment)
                       (toField domainIden : fmap toField spairsFlat)
    let linePairs = fmap (\(x :. y) -> (x, y)) rows
        lineIdenPairs = fmap (\(Line aIden _ _, Line bIden _ _) -> (aIden, bIden)) linePairs
        m = M.fromList $ zip lineIdenPairs linePairs
    case mapM (`M.lookup` m) spairs of
        Just versePairs -> return . Verse $ fmap fst versePairs ++ [snd (last versePairs)]
        Nothing -> throw BrokenVerseException

randomLine :: Connection -> Domain -> IO (Maybe Line)
randomLine conn (Domain domainIden _) = headMay <$>
    query conn "SELECT lines.iden, color, txt \
               \FROM lines \
               \WHERE domain_iden=? \
               \ORDER BY RANDOM() LIMIT 1" (Only domainIden)

getRandomSuccessor :: Connection -> Domain -> Line -> IO (Maybe Line)
getRandomSuccessor conn (Domain domainIden _) (Line lineIden _ _) = headMay <$>
    query conn "SELECT lines.iden, color, txt FROM lines \
               \INNER JOIN arrows USING (domain_iden) \
               \WHERE domain_iden=? AND arrows.src=? AND lines.iden=arrows.dst \
               \ORDER BY RANDOM() LIMIT 1" (domainIden, lineIden)

numLinesInDomain :: Connection -> Domain -> IO Integer
numLinesInDomain conn (Domain domainIden _) = do
    (Only c) <- query conn "SELECT COUNT(*) FROM lines WHERE domain_iden=?" (Only domainIden) >>= mustHead
    return c

addLine :: Connection -> Domain -> LineText -> Color -> IO Line
addLine conn (Domain domainIden _) txt color = do
    execute conn "INSERT INTO lines (domain_iden, txt, color) VALUES(?, ?, ?)" (domainIden, txt, color)
    newLineIden <- fromIntegral <$> lastInsertRowId conn
    return $ Line newLineIden color txt

addArrow :: Connection -> Domain -> Line -> Line -> IO ()
addArrow conn (Domain domainIden _) (Line srcIden _ _) (Line dstIden _ _) =
    execute conn "INSERT INTO arrows (domain_iden, src, dst) VALUES(?, ?, ?)" (domainIden, srcIden, dstIden)

getPrevs :: Connection -> Domain -> Line -> IO [Line]
getPrevs conn (Domain domainIden _) (Line lineIden _ _) = query conn
    "SELECT lines.iden, color, txt FROM lines \
    \INNER JOIN arrows USING (domain_iden) \
    \WHERE domain_iden=? AND arrows.src=iden AND arrows.dst=?" (domainIden, lineIden)

getNexts :: Connection -> Domain -> Line -> IO [Line]
getNexts conn (Domain domainIden _) (Line lineIden _ _) = query conn
    "SELECT lines.iden, color, txt FROM lines \
    \INNER JOIN arrows USING (domain_iden) \
    \WHERE domain_iden=? AND arrows.dst=iden AND arrows.src=?" (domainIden, lineIden)

insertGraphSpec :: Connection -> Domain -> GraphSpec -> IO ()
insertGraphSpec conn domain (GraphSpec lineSpecs arrowSpecs) = do
    newLines <- mapM (uncurry (addLine conn domain)) lineSpecs
    mapM_ (\(a, b) -> addArrow conn domain (newLines !! a) (newLines !! b)) arrowSpecs

clearDomain :: Connection -> Domain -> IO ()
clearDomain conn (Domain domainIden _) = do
    execute conn "DELETE FROM lines WHERE domain_iden=?" (Only domainIden)
    execute conn "DELETE FROM arrows WHERE domain_iden=?" (Only domainIden)

resetDomain :: Connection -> Domain -> GraphSpec -> IO ()
resetDomain conn domain graphSpec = do
    clearDomain conn domain
    insertGraphSpec conn domain graphSpec

deleteDomain :: Connection -> Domain -> IO ()
deleteDomain conn domain@(Domain domainIden _) = do
    clearDomain conn domain
    execute conn "DELETE FROM domains WHERE iden=?" (Only domainIden)

-------------------------------

homeURI :: T.Text
homeURI = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy HomeAPI) :: Link)

newDomainURI :: T.Text
newDomainURI = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy NewDomainAPI) :: Link)

domainURI :: DomainName -> T.Text
domainURI domainName = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainAPI) domainName :: Link)

domainReadURI :: DomainName -> Maybe VerseSpec -> T.Text
domainReadURI domainName verseSpec = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainReadAPI) domainName verseSpec :: Link)

domainWriteURI :: DomainName -> Maybe LineIden -> Maybe LineIden -> Maybe () -> T.Text
domainWriteURI domainName src dst initFlag = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainWriteAPI) domainName src dst initFlag :: Link)

domainWriteSubmitURI :: DomainName -> T.Text
domainWriteSubmitURI domainName = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainWriteSubmitAPI) domainName :: Link)

domainWalkURI :: DomainName -> Maybe LineIden -> T.Text
domainWalkURI domainName lineIden = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainWalkAPI) domainName lineIden :: Link)

domainClearURI :: DomainName -> T.Text
domainClearURI domainName = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainClearAPI) domainName :: Link)

domainResetURI :: DomainName -> T.Text
domainResetURI domainName = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainResetAPI) domainName :: Link)

domainDeleteURI :: DomainName -> T.Text
domainDeleteURI domainName = "/" <> toUrlPiece (safeLink api (Proxy :: Proxy DomainDeleteAPI) domainName :: Link)

-------------------------------

instance ToHtml HomeView where
    toHtml HomeView = pageTemplate
        Nothing
        []
        homeBody
            where
                homeBody = div_ $ do
                    p_ $ toHtmlRaw $ T.pack
                        "The passerine sorcerer, Roseacrucis, aims to destroy the connections between\
                        \ the Library&rsquo;s lines of poetry. If he is not stopped, each line will face\
                        \ a lifetime adrift at sea, by itself and without any context."
                    p_ $ toHtmlRaw $ T.pack "Your task is to strengthen the network of invisible edges between each line\
                    \ of poetry. Traditionally, each line only connects to one predecessor and one\
                    \ successor, but circumstances require us to create additional edges, possibly backwards\
                    \ or even in cycles."
                    p_ $ toHtmlRaw $ T.pack "Luckily, you are not alone. Other Librarians are also hard at work building\
                    \ the collaborative graph of poetry we call the <em>Spectral Renga</em>.\
                    \ Will we succeed? Can we maximize the\
                    \ min-cut and minimize the max-flow? Or will Roseacrucis prevail, leaving us\
                    \ bereft of such joys as enjambment, caesura, and rhyme? There&rsquo;s only one way to find out."
                    p_ $ do
                        a_ [href_ (domainURI (DomainName "piazza"))] "Join the public piazza"
                        span_ ", or "
                        a_ [href_ newDomainURI] "create a private subgraph"
                        span_ "."
                    p_ $ small_ $ do
                        a_ [href_ "/"] "cyfraeviolae.org"
                        span_ $ toHtmlRaw (T.pack " &middot; ")
                        a_ [href_ "https://www.nlsun.com"] "nlsun.com"

    toHtmlRaw = toHtml

instance ToHtml DomainView where
    toHtml (DomainView (Domain _ domainName) DomainInfo {numNodes, numEdges} showControls) = pageTemplate
        (Just domainName)
        [domainCrumb domainName, domainActionsCrumb domainName None]
        domainBody
        where
            domainBody = do
                div_ $ do
                    p_ $ do
                        span_ (toHtmlRaw (T.pack "Welcome to "))
                        span_ [class_ "domain-name"] (toHtml domainName)
                        span_ $ toHtml $ ". This subgraph contains " <> show numNodes <> " nodes and " <> show numEdges <> " edges."
                    p_ $ em_ $ do
                        a_ [href_ (domainReadURI domainName Nothing)] "Read a poem"
                        span_ ", "
                        a_ [href_ (domainWriteURI domainName Nothing Nothing Nothing)] "write a line"
                        span_ ", or "
                        a_ [href_ (domainWalkURI domainName Nothing)] "walk the graph"
                        span_ "."
                when showControls $ div_ [class_ "manage-box"] $ do
                    form_ [action_ (domainClearURI domainName), method_ "post", class_ "manage-form"] $
                        button_ [type_ "submit", class_ "manage-btn"] "Clear"
                    form_ [action_ (domainResetURI domainName), method_ "post", class_ "manage-form"] $
                        button_ [type_ "submit", class_ "manage-btn"] "Reset"
                    form_ [action_ (domainDeleteURI domainName), method_ "post", class_ "manage-form"] $
                        button_ [type_ "submit", class_ "manage-btn"] "Delete"

    toHtmlRaw = toHtml

initialWritePrompt :: Monad m => DomainName -> HtmlT m ()
initialWritePrompt domainName = p_ $ do
    span_ "Nothing here yet. "
    a_ [href_ (domainWriteURI domainName Nothing Nothing Nothing)] "Write a line"
    span_ "."

instance ToHtml DomainReadView where
    toHtml (DomainReadView domainName verse) = pageTemplate
        (Just domainName)
        [domainCrumb domainName, domainActionsCrumb domainName Read]
        (case verse of
           Nothing -> initialWritePrompt domainName
           Just (Verse lines') -> div_ [class_ "verse-container"] $ ul_ [class_ "verse"] $ mapM_ (li_ . toHtml . DomainedLine domainName) lines')
    toHtmlRaw = toHtml

instance ToHtml DomainWriteView where
    toHtml (DomainWriteView domainName msrc mdst (Color color)) = pageTemplate
        (Just domainName)
        [domainCrumb domainName, domainActionsCrumb domainName Write]
        (div_ $ form_ [action_ (domainWriteSubmitURI domainName), method_ "post"] $ do
            case msrc of
              Just src@(Line (LineIden lineIdenInt) _ _) -> p_ $ do
                  label_ [] "previous line: "
                  span_ $ toHtml (DomainedLine domainName src)
                  input_ [type_ "text", name_ "src", value_ (T.pack $ show lineIdenInt), hidden_ "1"]
              Nothing -> div_ ""
            p_ $ do
                label_ [for_ "txt", class_ "focus"] "your line: "
                input_ [type_ "text", name_ "txt", id_ "txt", value_ "", maxlength_ "120", required_ "1"]
            p_ $ do
                label_ [for_ "color", class_ "focus"] "your color: "
                input_ [type_ "color", name_ "color", id_ "color", value_ ("#" <> color)]
            case mdst of
              Just dst@(Line (LineIden lineIdenInt) _ _) -> p_ $ do
                  input_ [type_ "text", name_ "dst", value_ (T.pack $ show lineIdenInt), hidden_ "1"]
                  label_ [] "next line: "
                  span_ $ toHtml (DomainedLine domainName dst)
              Nothing -> div_ ""
            p_ $
                input_ [type_ "submit", value_ "Submit"]
            )
    toHtmlRaw = toHtml

instance ToHtml DomainWalkView where
    toHtml (DomainWalkView domainName info) = pageTemplate
        (Just domainName)
        [domainCrumb domainName, domainActionsCrumb domainName Walk]
        (case info of
            Nothing -> initialWritePrompt domainName
            Just (line@(Line lineIden _ _), prevs, nexts) -> div_ $ do
                div_ [class_ "verse-container"] $ do
                    small_ $ do
                        em_ $ toHtmlRaw (T.pack "predecessors &middot; ")
                        em_ $ a_ [href_ (domainWriteURI domainName Nothing (Just lineIden) Nothing)] "write"
                    case prevs of
                      [] -> p_ "Nothing here yet."
                      _ -> ul_ [class_ "verse"] $ mapM_ (li_ . toHtml . DomainedLine domainName) prevs
                div_ [class_ "verse-container"] $ do
                    small_ $ em_ "node"
                    div_ $ strong_ $ toHtml (DomainedLine domainName line)
                div_ [class_ "verse-container"] $ do
                    small_ $ do
                        em_ $ toHtmlRaw (T.pack "successors &middot; ")
                        em_ $ a_ [href_ (domainWriteURI domainName (Just lineIden) Nothing Nothing)] "write"
                    case nexts of
                      [] -> p_ "Nothing here yet."
                      _ -> ul_ [class_ "verse"] $ mapM_ (li_ . toHtml . DomainedLine domainName) nexts)
    toHtmlRaw = toHtml

instance ToHtml DomainedLine where
    toHtml (DomainedLine domainName (Line iden color@(Color colorStr) txt)) =
        a_ [href_ (domainWalkURI domainName (Just iden)),
            style_ ("color: #" <> colorStr),
            class_ (if isLight color then "outline" else "")] (toHtml txt)
    toHtmlRaw = toHtml

domainCrumb :: Monad m => DomainName -> HtmlT m ()
domainCrumb domainName = a_ [href_ (domainURI domainName), class_ "focus domain-name"] (toHtml domainName)

data ActionMode = None | Read | Write | Walk deriving (Eq)
domainActionsCrumb :: Monad m => DomainName -> ActionMode -> HtmlT m ()
domainActionsCrumb domainName mode =
    span_ [class_ "nonbreaking"] $ do
        span_ "{ "
        a_ [href_ (domainReadURI domainName Nothing), class_ readClass] "read"
        span_ ", "
        a_ [href_ (domainWriteURI domainName Nothing Nothing Nothing), class_ writeClass] "write"
        span_ ", "
        a_ [href_ (domainWalkURI domainName Nothing), class_ walkClass] "walk"
        span_ " }"
    where
        readClass = if mode == Read then "focus" else ""
        writeClass = if mode == Write then "focus" else ""
        walkClass = if mode == Walk then "focus" else ""

pageTemplate :: Monad m => Maybe DomainName -> [HtmlT m ()] -> HtmlT m () -> HtmlT m ()
pageTemplate domainName crumbs body = doctypehtml_ $ do
    head_ $ do
        title_ $ toHtml $ "Spectral Renga" <> titleDomain
        meta_ [charset_ "utf-8"]
        meta_ [name_ "viewport", content_ "width=device-width, initial-scale=1.0"]
        link_ [rel_ "shortcut icon", type_ "image/x-icon", href_ "/spectral-renga/static/favicon.ico"]
        link_ [rel_ "stylesheet", type_ "text/css", href_ "/static/styles.css"]
        link_ [rel_ "stylesheet", type_ "text/css", href_ "/spectral-renga/static/styles.css"]
    body_ $ div_ [class_ "container"] $ do
        div_ $ do
            div_ [class_ "home"] $ do
                a_ [href_ homeURI, class_ "home-title"] "Spectral Renga"
                span_ " at "
                a_ [href_ "/"] "cyfraeviolae.org"
            div_ [class_ "crumbs"] $ sequence_ $
                intersperse (span_ [class_ "sep"] (toHtmlRaw (T.pack " &middot; "))) (srcCrumb:crumbs)
        body
    where
        srcCrumb = a_ [href_ "/git/spectralrenga"] "source code"
        titleDomain = case domainName of
                        Nothing -> ""
                        Just (DomainName domainNameStr) -> " - " <> domainNameStr

--------------

sampleLines :: [LineText]
sampleLines = fmap LineText [
    "A turmoil of wars-men, spread over the middle kingdom,"
    , "And a head in the freakish Atlantic"
    , "And drank coffee, and talked for an hour."
    , "And he was always human when he talked;"
    , "Babel of arcades and stairways,"
    , "I then began seeking for some alternative. I felt"
    , "I with the Nymphs will haunt Mount Maenalus,"
    , "In a convex mirror, such as is used by barbers"
    , "My mad singing startles the valleys and hills:"
    , "Of tendrils, leaves, and rough nuts brown"
    , "One Mite wrung from the Labrers hands"
    , "One like a wombat prowl’d obtuse and furry,"
    , "One warbling for the mere bright day’s delight,"
    , "Parting track'd by arriving, perpetual payment of perpetual loan,"
    , "Rolled round in earth's diurnal course,"
    , "Scarce seem'd a vision; I would ne'er have striven"
    , "Swimmer, your body is pure as the water;"
    , "The all-beholding sun shall see no more"
    , "The sea, whose sob created my gentle roll,"
    , "The snows of the Tyrol, the clear beer of Vienna"
    , "Thou mighty Poet, e'en to frenzy bold!"
    , "Though wise men at their end know dark is right,"
    , "Tonight I can write the saddest lines."
    , "To resume their compulsory game:"
    , "and the village is flooded"
    , "i carry your heart with me(i carry it in"
    , "in whom Death the gardener wove different veins."
    , "which is the part of stories one never quite believes."
    , "who burned cigarette holes in their arms protesting the narcotic tobacco haze of Capitalism,"
    ]

data GraphSpec = GraphSpec [(LineText, Color)] [(Int, Int)]

makeConnectedGraph :: StatefulGen g m => g -> [LineText] -> m GraphSpec
makeConnectedGraph _ [] = return $ GraphSpec [] []
makeConnectedGraph g [lineText] = do
    newColor <- sampleColor g
    return $ GraphSpec [(lineText, newColor)] []
makeConnectedGraph g (lineText:lineTexts) = do
    GraphSpec lineSpecs arrowSpecs <- makeConnectedGraph g lineTexts
    let newIdx = length lineSpecs
    oldIdx <- uniformRM (0, newIdx - 1) g
    newLineIsSource <- uniformM g
    newColor <- sampleColor g
    let newArrow = if newLineIsSource then (newIdx, oldIdx) else (oldIdx, newIdx)
    return $ GraphSpec (lineSpecs ++ [(lineText, newColor)]) (arrowSpecs ++ [newArrow])

reduceSparsity :: StatefulGen g m => g -> Int -> Int -> [(Int, Int)] -> m [(Int, Int)]
reduceSparsity g n numNewEdges arrows = do
    newArrows <- replicateM numNewEdges ((,) <$> uniformRM (0, n-1) g <*> uniformRM (0, n-1) g)
    let filteredArrows = filter (uncurry (/=)) (arrows ++ newArrows) -- no trivial loops
    return $ S.toList $ S.fromList filteredArrows -- no multiedges


numNodesInDefault :: Int
numNodesInDefault = min 7 (length sampleLines)

numExtraArrowsInDefault :: Int
numExtraArrowsInDefault = 7

makeDefaultGraphSpec :: StatefulGen g m => g -> m GraphSpec
makeDefaultGraphSpec g = do
    cutAt <- uniformRM (0, length sampleLines - 1) g
    let lineTexts = take numNodesInDefault $ drop cutAt sampleLines ++ take cutAt sampleLines
    GraphSpec lineSpecs arrowSpecs <- makeConnectedGraph g lineTexts
    newArrowSpecs <- reduceSparsity g (length lineSpecs) numExtraArrowsInDefault arrowSpecs
    return $ GraphSpec lineSpecs newArrowSpecs

--------------

mapMaybeM :: Monad m => Maybe a -> (a -> m b) -> m (Maybe b)
mapMaybeM m f = maybe (return Nothing) (fmap Just . f) m

parseHex :: Color -> Integer
parseHex (Color s) = case readHex (T.unpack s) of
                       [(n, _)] -> n
                       _ -> error "should not happen; color is well-formed"
firstByte :: Integer -> Float
firstByte n = fromIntegral $ (n .&. 0xff0000) `shiftR` 16
secondByte :: Integer -> Float
secondByte n = fromIntegral $ (n .&. 0x00ff00) `shiftR` 8
thirdByte :: Integer -> Float
thirdByte n = fromIntegral $ n .&. 0x0000ff
brightness :: Integer -> Float
brightness n = 1.17*firstByte n + 2.30*secondByte n + 0.45*thirdByte n
isLight :: Color -> Bool
isLight color = (brightness . parseHex) color >= 800

collectUntil :: Int -> (a -> IO (Maybe a)) -> a -> IO [a]
collectUntil 0 _ _ = return []
collectUntil n f src =
    f src >>= \case
      Nothing -> return [src]
      Just dst' -> (:) src <$> collectUntil (n-1) f dst'

headMay :: [a] -> Maybe a
headMay []    = Nothing
headMay (x:_) = Just x

data HeadException = HeadException deriving (Show)

instance E.Exception HeadException

mustHead :: [a] -> IO a
mustHead []    = throw HeadException
mustHead (x:_) = return x