-- | Updateable objects
module Calamity.Internal.Updateable (Updateable (..)) where

import Calamity.Internal.Utils
import Calamity.Types.Model.Channel
import Calamity.Types.Model.Channel.UpdatedMessage
import Calamity.Types.Model.Guild.Guild
import Calamity.Types.Model.User
import Control.Lens
import Data.Generics.Product.Fields
import Data.Maybe
import GHC.TypeLits

class Updateable a where
    type Updated a
    type Updated a = a

    update :: Updated a -> a -> a

-- | sets original field to new field
setF :: forall (f :: Symbol) o n b. (HasField' f o b, HasField' f o b, HasField' f n b) => n -> o -> o
setF :: n -> o -> o
setF n
n = forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f ((b -> Identity b) -> o -> Identity o) -> b -> o -> o
forall s t a b. ASetter s t a b -> b -> s -> t
.~ n
n n -> Getting b n b -> b
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f

-- | sets original field to unwrapped new field if new field is not Nothing
mergeF :: forall (f :: Symbol) o n b. (HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) => n -> o -> o
mergeF :: n -> o -> o
mergeF n
n = forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f ((b -> Identity b) -> o -> Identity o) -> (b -> b) -> o -> o
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \b
oldv -> b -> Maybe b -> b
forall a. a -> Maybe a -> a
fromMaybe b
oldv (n
n n -> Getting (Maybe b) n (Maybe b) -> Maybe b
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f)

-- | sets original field to new field if new field is not nothing
mergeF' ::
    forall (f :: Symbol) old new v.
    ( HasField' f old (Maybe v)
    , HasField' f new (Maybe v)
    ) =>
    new ->
    old ->
    old
mergeF' :: new -> old -> old
mergeF' new
new = forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f ((Maybe v -> Identity (Maybe v)) -> old -> Identity old)
-> (Maybe v -> Maybe v) -> old -> old
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \Maybe v
oldv -> Maybe v -> Maybe v -> Maybe v
forall a. Maybe a -> Maybe a -> Maybe a
lastMaybe Maybe v
oldv (new
new new -> Getting (Maybe v) new (Maybe v) -> Maybe v
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f)

-- | sets original field to new field if new field was present
updateNullableDest ::
    forall (f :: Symbol) old new v.
    ( HasField' f old (Maybe v)
    , HasField' f new (Maybe (MaybeNull v))
    ) =>
    new ->
    old ->
    old
updateNullableDest :: new -> old -> old
updateNullableDest new
new = case new
new new
-> Getting (Maybe (MaybeNull v)) new (Maybe (MaybeNull v))
-> Maybe (MaybeNull v)
forall s a. s -> Getting a s a -> a
^. forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f of
    Just (NotNull v
x) -> forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f ((Maybe v -> Identity (Maybe v)) -> old -> Identity old)
-> v -> old -> old
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ v
x
    Just MaybeNull v
WasNull -> forall s a. HasField' f s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
field' @f ((Maybe v -> Identity (Maybe v)) -> old -> Identity old)
-> Maybe v -> old -> old
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe v
forall a. Maybe a
Nothing
    Maybe (MaybeNull v)
Nothing -> old -> old
forall a. a -> a
Prelude.id

-- NOTE: afaik only messages get partial updates
instance Updateable Message where
    type Updated Message = UpdatedMessage

    update :: Updated Message -> Message -> Message
update Updated Message
n Message
o = Message
o
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"content" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v),
 HasField' f new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"editedTimestamp" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"tts" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"mentionEveryone" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"mentions" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"mentionRoles" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"mentionChannels" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"attachments" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"embeds" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"reactions" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"pinned" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v),
 HasField' f new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"webhookID" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"type_" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v),
 HasField' f new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"activity" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v),
 HasField' f new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"application" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v),
 HasField' f new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"messageReference" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"flags" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v),
 HasField' f new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"referencedMessage" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v),
 HasField' f new (Maybe (MaybeNull v))) =>
new -> old -> old
updateNullableDest @"interaction" UpdatedMessage
Updated Message
n
      Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& UpdatedMessage -> Message -> Message
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"components" UpdatedMessage
Updated Message
n

instance Updateable Channel where
    update :: Updated Channel -> Channel -> Channel
update Updated Channel
n Channel
_ = Channel
Updated Channel
n

instance Updateable DMChannel where
    update :: Updated DMChannel -> DMChannel -> DMChannel
update Updated DMChannel
n DMChannel
_ = DMChannel
Updated DMChannel
n

instance Updateable GuildChannel where
    update :: Updated GuildChannel -> GuildChannel -> GuildChannel
update Updated GuildChannel
n GuildChannel
_ = GuildChannel
Updated GuildChannel
n

instance Updateable Guild where
    type Updated Guild = UpdatedGuild

    update :: Updated Guild -> Guild -> Guild
update Updated Guild
n Guild
o =
        Guild
o
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"name" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"icon" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"splash" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"owner" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"ownerID" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"permissions" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"region" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"afkChannelID" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"afkTimeout" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"embedEnabled" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"embedChannelID" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"verificationLevel" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"defaultMessageNotifications" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"explicitContentFilter" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"roles" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"features" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"mfaLevel" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"applicationID" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n (Maybe b)) =>
n -> o -> o
mergeF @"widgetEnabled" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"widgetChannelID" UpdatedGuild
Updated Guild
n
            Guild -> (Guild -> Guild) -> Guild
forall a b. a -> (a -> b) -> b
& UpdatedGuild -> Guild -> Guild
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"systemChannelID" UpdatedGuild
Updated Guild
n

instance Updateable User where
    update :: Updated User -> User -> User
update Updated User
n User
o =
        User
o
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"username" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) o n b.
(HasField' f o b, HasField' f o b, HasField' f n b) =>
n -> o -> o
setF @"discriminator" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"bot" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"avatar" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"mfaEnabled" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"verified" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"email" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"flags" User
Updated User
n
            User -> (User -> User) -> User
forall a b. a -> (a -> b) -> b
& User -> User -> User
forall (f :: Symbol) old new v.
(HasField' f old (Maybe v), HasField' f new (Maybe v)) =>
new -> old -> old
mergeF' @"premiumType" User
Updated User
n