{-# LANGUAGE TemplateHaskell #-}

module Calamity.Types.Model.Avatar (
  Avatar (..),
  MemberAvatar (..),
) where

import Calamity.Types.CDNAsset (CDNAsset (..))
import {-# SOURCE #-} Calamity.Types.Model.Guild.Guild
import {-# SOURCE #-} Calamity.Types.Model.User
import Calamity.Types.Snowflake (Snowflake, fromSnowflake)
import Calamity.Utils.CDNUrl (assetHashFile, cdnURL)
import Data.Text qualified as T
import Network.HTTP.Req ((/:), (/~))
import Optics (makeFieldLabelsNoPrefix)
import TextShow (showt)
import TextShow.TH (deriveTextShow)

data Avatar = Avatar
  { Avatar -> Maybe Text
hash :: Maybe T.Text
  , Avatar -> Snowflake User
userID :: Snowflake User
  , Avatar -> Int
discrim :: Int
  }
  deriving (Int -> Avatar -> ShowS
[Avatar] -> ShowS
Avatar -> String
(Int -> Avatar -> ShowS)
-> (Avatar -> String) -> ([Avatar] -> ShowS) -> Show Avatar
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Avatar -> ShowS
showsPrec :: Int -> Avatar -> ShowS
$cshow :: Avatar -> String
show :: Avatar -> String
$cshowList :: [Avatar] -> ShowS
showList :: [Avatar] -> ShowS
Show, Avatar -> Avatar -> Bool
(Avatar -> Avatar -> Bool)
-> (Avatar -> Avatar -> Bool) -> Eq Avatar
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Avatar -> Avatar -> Bool
== :: Avatar -> Avatar -> Bool
$c/= :: Avatar -> Avatar -> Bool
/= :: Avatar -> Avatar -> Bool
Eq)

instance CDNAsset Avatar where
  assetURL :: Avatar -> Url 'Https
assetURL Avatar {$sel:hash:Avatar :: Avatar -> Maybe Text
hash = Just Text
h, Snowflake User
$sel:userID:Avatar :: Avatar -> Snowflake User
userID :: Snowflake User
userID} =
    Url 'Https
cdnURL Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"avatars" Url 'Https -> Word64 -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ Snowflake User -> Word64
forall t. Snowflake t -> Word64
fromSnowflake Snowflake User
userID Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text -> Text
assetHashFile Text
h
  assetURL Avatar {Int
$sel:discrim:Avatar :: Avatar -> Int
discrim :: Int
discrim} =
    Url 'Https
cdnURL Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"embed" Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"avatars" Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: (Int -> Text
forall a. TextShow a => a -> Text
showt (Int
discrim Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
5) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".png")

-- | A member's custom guild avatar
data MemberAvatar = MemberAvatar
  { MemberAvatar -> Text
hash :: T.Text
  , MemberAvatar -> Snowflake Guild
guildID :: Snowflake Guild
  , MemberAvatar -> Snowflake User
userID :: Snowflake User
  }
  deriving (Int -> MemberAvatar -> ShowS
[MemberAvatar] -> ShowS
MemberAvatar -> String
(Int -> MemberAvatar -> ShowS)
-> (MemberAvatar -> String)
-> ([MemberAvatar] -> ShowS)
-> Show MemberAvatar
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MemberAvatar -> ShowS
showsPrec :: Int -> MemberAvatar -> ShowS
$cshow :: MemberAvatar -> String
show :: MemberAvatar -> String
$cshowList :: [MemberAvatar] -> ShowS
showList :: [MemberAvatar] -> ShowS
Show, MemberAvatar -> MemberAvatar -> Bool
(MemberAvatar -> MemberAvatar -> Bool)
-> (MemberAvatar -> MemberAvatar -> Bool) -> Eq MemberAvatar
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MemberAvatar -> MemberAvatar -> Bool
== :: MemberAvatar -> MemberAvatar -> Bool
$c/= :: MemberAvatar -> MemberAvatar -> Bool
/= :: MemberAvatar -> MemberAvatar -> Bool
Eq)

instance CDNAsset MemberAvatar where
  assetURL :: MemberAvatar -> Url 'Https
assetURL MemberAvatar {Text
$sel:hash:MemberAvatar :: MemberAvatar -> Text
hash :: Text
hash, Snowflake Guild
$sel:guildID:MemberAvatar :: MemberAvatar -> Snowflake Guild
guildID :: Snowflake Guild
guildID, Snowflake User
$sel:userID:MemberAvatar :: MemberAvatar -> Snowflake User
userID :: Snowflake User
userID} =
    Url 'Https
cdnURL Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"guilds" Url 'Https -> Snowflake Guild -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ Snowflake Guild
guildID Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"users" Url 'Https -> Snowflake User -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ Snowflake User
userID Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"avatars" Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text -> Text
assetHashFile Text
hash

$(deriveTextShow ''Avatar)
$(makeFieldLabelsNoPrefix ''Avatar)
$(deriveTextShow ''MemberAvatar)
$(makeFieldLabelsNoPrefix ''MemberAvatar)