module WykopEntries (
      indexEntry
    , addEntry
    , editEntry
    , deleteEntry
    , addCommEntry
    , editCommEntry
    , deleteCommEntry
    , voteEntry
    , unvoteEntry
    , voteCommEntry
    , unvoteCommEntry
    , module WykopTypes
    ) where

import WykopTypes
import WykopUtils

import Data.Aeson (Value)

indexEntry :: Keys -> Int -> IO (Maybe Entry)
indexEntry k id = get k [] [] $ "entries/index/" ++ show id

addEntry :: Keys -> String -> Maybe String -> Userkey -> IO (Maybe Value)
addEntry k body embed userKey = get k postData (toGet userKey) res
    where
        postData = ("userkey", userKey) : embedToGet
        embedToGet = case embed of
            (Just x) -> [("embed", x)]
            _        -> []

        res = "entries/add"

editEntry :: Keys -> Int -> String -> Userkey -> IO (Maybe Value)
editEntry k id body userKey = get k [("body", body)] (toGet userKey) res
    where
        res = "entries/edit/" ++ show id

deleteEntry :: Keys -> Int -> Userkey -> IO (Maybe Value)
deleteEntry k id userKey = get k [] (toGet userKey) res
    where
        res = "entries/delete/" ++ show id

addCommEntry :: Keys -> Int -> String -> Maybe String -> Userkey -> IO (Maybe Value)
addCommEntry k id body embed userKey = get k postData (toGet userKey) res
    where
        postData = ("userkey", userKey) : embedToGet
        embedToGet = case embed of
            (Just x) -> [("embed", x)]
            _        -> []

        res = "entries/addcomment/" ++ show id

editCommEntry :: Keys -> Int -> Int -> String -> Userkey -> IO (Maybe Value)
editCommEntry k entryId commId body userKey = get k postData getData res
    where
        postData = [("body", body)]
        getData = toGet userKey
        res = "entries/editcomment/" ++ show entryId ++ "/" ++ show commId

deleteCommEntry :: Keys -> Int -> Int -> Userkey -> IO (Maybe Value)
deleteCommEntry k entryId commId userKey = get k [] getData res
    where
        getData = toGet userKey
        res = "entries/deletecomment/" ++ show entryId ++ "/" ++ show commId

voteEntry' :: String -> Keys -> Int -> Userkey -> IO (Maybe Value)
voteEntry' v k entryId userKey = get k [] (toGet userKey) res
    where
        res = "entries/" ++ v ++ "/entry/" ++ show entryId

voteEntry :: Keys -> Int -> Userkey -> IO (Maybe Value)
voteEntry = voteEntry' "vote"

unvoteEntry :: Keys -> Int -> Userkey -> IO (Maybe Value)
unvoteEntry = voteEntry' "unvote"

voteCommEntry' :: String -> Keys -> Int -> Int -> Userkey -> IO (Maybe Value)
voteCommEntry' v k entryId commId userKey = get k [] (toGet userKey) res
    where
        res = "entries/" ++ v ++ "/comment/" ++ show entryId ++ "/" ++ show commId

voteCommEntry :: Keys -> Int -> Int -> Userkey -> IO (Maybe Value)
voteCommEntry = voteCommEntry' "vote"

unvoteCommEntry :: Keys -> Int -> Int -> Userkey -> IO (Maybe Value)
unvoteCommEntry = voteCommEntry' "unvote"
