module Handler.AccountEdit where

import Import
import Text.Markdown
import Yesod.Text.Markdown

getUser303 :: Handler User
getUser303 = do
  maybeUser <- lookupUser
  case maybeUser of
    Nothing -> do
      setMessage "You need to be logged in to do that."
      redirect AccountLoginR
    Just user -> return user

getUser403 :: Handler User
getUser403 = do
  maybeUser <- lookupUser
  case maybeUser of
    Nothing -> permissionDenied "You need to be logged in to do that."
    Just user -> return user

getAccountEditR :: Handler Html
getAccountEditR = do
  user <- getUser303
  (widget, enctype) <- generateFormPost $ mkAccountForm user
  defaultLayout $ do
    [whamlet|
      <h1>Edit #{userIdent user}
      <form method=post enctype=#{enctype} role=form>
        ^{widget}
    |]
    
postAccountEditR :: Handler Html
postAccountEditR = do
  user <- getUser403
  ((result, widget), enctype) <- runFormPost $ mkAccountForm user
  let ferror = formError widget enctype
  case result of
    FormFailure errorMessages -> ferror errorMessages
    FormMissing -> ferror ["You didn't submit any data."]
    FormSuccess (AccountInput nom email description) -> do
      (Entity uid user) <- runDB . getBy404 . UniqueUser $ userIdent user
      _ <- runDB $ update uid [ UserRealName =. nom
                              , UserDescription =. description
                              ]
      let existentEmail = (userEmail user)
      _ <- if email == existentEmail
        then return ()
        else do
          newKey <- lift $ genKey 64
          runDB $ update uid [ UserEmail =. email
                             , UserEmailConfirmed =. False
                             , UserEmailConfirmKey =. newKey
                             ]
      redirect . UserR $ userIdent user
accountForm :: Maybe AccountInput -> AForm Handler AccountInput
accountForm ai = AccountInput
  <$> aopt textField (rbfs "Real name") (realName <$> ai)
  <*> aopt emailField (rbfs "Email (optional)") (email <$> ai)
  <*> aopt markdownField (rbfs "Description (parsed as Markdown)") (description <$> ai)
  <*  bootstrapSubmit ("Update" :: BootstrapSubmit Text)

mkAccountForm :: User -> Form AccountInput
mkAccountForm user = renderBootstrap . accountForm . Just $ AccountInput
  { realName    = userRealName user
  , email       = userEmail user
  , description = userDescription user
  }

data AccountInput = AccountInput
  { realName        :: Maybe Text
  , email           :: Maybe Text
  , description     :: Maybe Markdown
  }

--   -- This is what a "null user" looks like, for reference
--   -- { userIdent           = mempty
--   -- , userPassword        = mempty
--   -- , userEmail           = mempty
--   -- , userEmailConfirmed  = False
--   -- , userEmailConfirmKey = mempty
--   -- , userLoginKey        = mempty
--   -- , userVideos          = mempty
--   -- , userComments        = mempty
--   -- , userTextPosts       = mempty
--   -- , userDescription     = mempty
--   -- , userRealName        = mempty
--   -- , userResolutionWidth = 480
--   -- } 

-- userToAccountInput :: User -> AccountInput
-- userToAccountInput u = AccountInput
--   { password = userPassword u
--   , email = userEmail u
--   , emailConfirmKey = userEmailConfirmKey u
--   , videos = userVideos u
--   , comments = userComments u
--   , textPosts = userTextPosts u
--   , description = 
--   }
