package tg

import 

// AnyMessageHandler is a handler of a message from any chat: private chat,
// basic group or channel (supergroup).
type AnyMessageHandler func(ctx context.Context, e Entities, msg MessageClass) error

// OnAnyNewMessage sets handler of new messages from any chat, i.e. both
// UpdateNewMessage and UpdateNewChannelMessage.
//
// It replaces handlers set by OnNewMessage and OnNewChannelMessage, and is
// replaced by them.
func ( UpdateDispatcher) ( AnyMessageHandler) {
	.OnNewMessage(func( context.Context,  Entities,  *UpdateNewMessage) error {
		return (, , .Message)
	})
	.OnNewChannelMessage(func( context.Context,  Entities,  *UpdateNewChannelMessage) error {
		return (, , .Message)
	})
}

// OnAnyEditMessage sets handler of edited messages from any chat, i.e. both
// UpdateEditMessage and UpdateEditChannelMessage.
//
// It replaces handlers set by OnEditMessage and OnEditChannelMessage, and is
// replaced by them.
func ( UpdateDispatcher) ( AnyMessageHandler) {
	.OnEditMessage(func( context.Context,  Entities,  *UpdateEditMessage) error {
		return (, , .Message)
	})
	.OnEditChannelMessage(func( context.Context,  Entities,  *UpdateEditChannelMessage) error {
		return (, , .Message)
	})
}

// AnyDeleteMessagesHandler is a handler of deleted messages.
//
// The channelID is zero for private chats and basic groups: message IDs are
// common for them, and the update does not contain the peer.
type AnyDeleteMessagesHandler func(ctx context.Context, e Entities, channelID int64, ids []int) error

// OnAnyDeleteMessages sets handler of deleted messages from any chat, i.e.
// both UpdateDeleteMessages and UpdateDeleteChannelMessages.
//
// It replaces handlers set by OnDeleteMessages and OnDeleteChannelMessages,
// and is replaced by them.
func ( UpdateDispatcher) ( AnyDeleteMessagesHandler) {
	.OnDeleteMessages(func( context.Context,  Entities,  *UpdateDeleteMessages) error {
		return (, , 0, .Messages)
	})
	.OnDeleteChannelMessages(func( context.Context,  Entities,  *UpdateDeleteChannelMessages) error {
		return (, , .ChannelID, .Messages)
	})
}

// AnyRead is read history event: messages up to and including MaxID in Peer
// (and topic, if TopMsgID or SavedPeer is set) are read.
type AnyRead struct {
	// Peer is the chat.
	Peer PeerClass
	// MaxID is the ID of the last read message.
	MaxID int
	// TopMsgID is the forum topic or discussion thread ID, zero if none.
	TopMsgID int
	// SavedPeer is the monoforum (channel direct messages) topic, nil if none.
	SavedPeer PeerClass
	// Update is the source update, for fields not mapped here.
	Update UpdateClass
}

// AnyReadHandler is a handler of read history.
type AnyReadHandler func(ctx context.Context, e Entities, read AnyRead) error

// OnAnyReadInbox sets handler of read incoming messages in any chat or topic,
// i.e. UpdateReadHistoryInbox, UpdateReadChannelInbox,
// UpdateReadChannelDiscussionInbox and UpdateReadMonoForumInbox.
//
// It replaces handlers set by OnReadHistoryInbox, OnReadChannelInbox,
// OnReadChannelDiscussionInbox and OnReadMonoForumInbox, and is replaced by
// them.
func ( UpdateDispatcher) ( AnyReadHandler) {
	.OnReadHistoryInbox(func( context.Context,  Entities,  *UpdateReadHistoryInbox) error {
		return (, , AnyRead{
			Peer:     .Peer,
			MaxID:    .MaxID,
			TopMsgID: .TopMsgID,
			Update:   ,
		})
	})
	.OnReadChannelInbox(func( context.Context,  Entities,  *UpdateReadChannelInbox) error {
		return (, , AnyRead{
			Peer:   &PeerChannel{ChannelID: .ChannelID},
			MaxID:  .MaxID,
			Update: ,
		})
	})
	.OnReadChannelDiscussionInbox(func( context.Context,  Entities,  *UpdateReadChannelDiscussionInbox) error {
		return (, , AnyRead{
			Peer:     &PeerChannel{ChannelID: .ChannelID},
			MaxID:    .ReadMaxID,
			TopMsgID: .TopMsgID,
			Update:   ,
		})
	})
	.OnReadMonoForumInbox(func( context.Context,  Entities,  *UpdateReadMonoForumInbox) error {
		return (, , AnyRead{
			Peer:      &PeerChannel{ChannelID: .ChannelID},
			MaxID:     .ReadMaxID,
			SavedPeer: .SavedPeerID,
			Update:    ,
		})
	})
}

// OnAnyReadOutbox sets handler of read outgoing messages in any chat or topic,
// i.e. UpdateReadHistoryOutbox, UpdateReadChannelOutbox,
// UpdateReadChannelDiscussionOutbox and UpdateReadMonoForumOutbox.
//
// It replaces handlers set by OnReadHistoryOutbox, OnReadChannelOutbox,
// OnReadChannelDiscussionOutbox and OnReadMonoForumOutbox, and is replaced by
// them.
func ( UpdateDispatcher) ( AnyReadHandler) {
	.OnReadHistoryOutbox(func( context.Context,  Entities,  *UpdateReadHistoryOutbox) error {
		return (, , AnyRead{
			Peer:   .Peer,
			MaxID:  .MaxID,
			Update: ,
		})
	})
	.OnReadChannelOutbox(func( context.Context,  Entities,  *UpdateReadChannelOutbox) error {
		return (, , AnyRead{
			Peer:   &PeerChannel{ChannelID: .ChannelID},
			MaxID:  .MaxID,
			Update: ,
		})
	})
	.OnReadChannelDiscussionOutbox(func( context.Context,  Entities,  *UpdateReadChannelDiscussionOutbox) error {
		return (, , AnyRead{
			Peer:     &PeerChannel{ChannelID: .ChannelID},
			MaxID:    .ReadMaxID,
			TopMsgID: .TopMsgID,
			Update:   ,
		})
	})
	.OnReadMonoForumOutbox(func( context.Context,  Entities,  *UpdateReadMonoForumOutbox) error {
		return (, , AnyRead{
			Peer:      &PeerChannel{ChannelID: .ChannelID},
			MaxID:     .ReadMaxID,
			SavedPeer: .SavedPeerID,
			Update:    ,
		})
	})
}

// AnyTyping is a chat action (typing, uploading, etc.) event.
type AnyTyping struct {
	// Peer is the chat.
	Peer PeerClass
	// From is the peer performing the action.
	From PeerClass
	// TopMsgID is the forum topic or thread ID, zero if none.
	TopMsgID int
	// Action is the chat action.
	Action SendMessageActionClass
	// Update is the source update, for fields not mapped here.
	Update UpdateClass
}

// AnyTypingHandler is a handler of chat actions.
type AnyTypingHandler func(ctx context.Context, e Entities, typing AnyTyping) error

// OnAnyTyping sets handler of chat actions in any chat, i.e. UpdateUserTyping,
// UpdateChatUserTyping and UpdateChannelUserTyping.
//
// It replaces handlers set by OnUserTyping, OnChatUserTyping and
// OnChannelUserTyping, and is replaced by them.
func ( UpdateDispatcher) ( AnyTypingHandler) {
	.OnUserTyping(func( context.Context,  Entities,  *UpdateUserTyping) error {
		 := &PeerUser{UserID: .UserID}
		return (, , AnyTyping{
			Peer:     ,
			From:     ,
			TopMsgID: .TopMsgID,
			Action:   .Action,
			Update:   ,
		})
	})
	.OnChatUserTyping(func( context.Context,  Entities,  *UpdateChatUserTyping) error {
		return (, , AnyTyping{
			Peer:   &PeerChat{ChatID: .ChatID},
			From:   .FromID,
			Action: .Action,
			Update: ,
		})
	})
	.OnChannelUserTyping(func( context.Context,  Entities,  *UpdateChannelUserTyping) error {
		return (, , AnyTyping{
			Peer:     &PeerChannel{ChannelID: .ChannelID},
			From:     .FromID,
			TopMsgID: .TopMsgID,
			Action:   .Action,
			Update:   ,
		})
	})
}