// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// MessagesSetChatWallPaperRequest represents TL type `messages.setChatWallPaper#8ffacae1`.
// Set a custom wallpaper »¹ in a specific private chat with another user.
//
// Links:
//  1. https://core.telegram.org/api/wallpapers
//
// See https://core.telegram.org/method/messages.setChatWallPaper for reference.
type MessagesSetChatWallPaperRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Only for Premium¹ users, sets the specified wallpaper for both users of the chat,
	// without requiring confirmation from the other user.
	//
	// Links:
	//  1) https://core.telegram.org/api/premium
	ForBoth bool
	// If we don't like the new wallpaper the other user of the chat has chosen for us using
	// the for_both flag, we can re-set our previous wallpaper just on our side using this
	// flag.
	Revert bool
	// The private chat where the wallpaper will be set
	Peer InputPeerClass
	// The wallpaper »¹, obtained as described in the wallpaper documentation »²; must
	// not be provided when installing a wallpaper obtained from a
	// messageActionSetChatWallPaper³ service message (id must be provided, instead).
	//
	// Links:
	//  1) https://core.telegram.org/api/wallpapers
	//  2) https://core.telegram.org/api/wallpapers#uploading-wallpapers
	//  3) https://core.telegram.org/constructor/messageActionSetChatWallPaper
	//
	// Use SetWallpaper and GetWallpaper helpers.
	Wallpaper InputWallPaperClass
	// Wallpaper settings, obtained as described in the wallpaper documentation »¹ or from
	// messageActionSetChatWallPaper².wallpaper.settings.
	//
	// Links:
	//  1) https://core.telegram.org/api/wallpapers#uploading-wallpapers
	//  2) https://core.telegram.org/constructor/messageActionSetChatWallPaper
	//
	// Use SetSettings and GetSettings helpers.
	Settings WallPaperSettings
	// If the wallpaper was obtained from a messageActionSetChatWallPaper¹ service message,
	// must contain the ID of that message.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/messageActionSetChatWallPaper
	//
	// Use SetID and GetID helpers.
	ID int
}

// MessagesSetChatWallPaperRequestTypeID is TL type id of MessagesSetChatWallPaperRequest.
const MessagesSetChatWallPaperRequestTypeID = 0x8ffacae1

// Ensuring interfaces in compile-time for MessagesSetChatWallPaperRequest.
var (
	_ bin.Encoder     = &MessagesSetChatWallPaperRequest{}
	_ bin.Decoder     = &MessagesSetChatWallPaperRequest{}
	_ bin.BareEncoder = &MessagesSetChatWallPaperRequest{}
	_ bin.BareDecoder = &MessagesSetChatWallPaperRequest{}
)

func (s *MessagesSetChatWallPaperRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.ForBoth == false) {
		return false
	}
	if !(s.Revert == false) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.Wallpaper == nil) {
		return false
	}
	if !(s.Settings.Zero()) {
		return false
	}
	if !(s.ID == 0) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (s *MessagesSetChatWallPaperRequest) String() string {
	if s == nil {
		return "MessagesSetChatWallPaperRequest(nil)"
	}
	type Alias MessagesSetChatWallPaperRequest
	return fmt.Sprintf("MessagesSetChatWallPaperRequest%+v", Alias(*s))
}

// FillFrom fills MessagesSetChatWallPaperRequest from given interface.
func (s *MessagesSetChatWallPaperRequest) FillFrom(from interface {
	GetForBoth() (value bool)
	GetRevert() (value bool)
	GetPeer() (value InputPeerClass)
	GetWallpaper() (value InputWallPaperClass, ok bool)
	GetSettings() (value WallPaperSettings, ok bool)
	GetID() (value int, ok bool)
}) {
	s.ForBoth = from.GetForBoth()
	s.Revert = from.GetRevert()
	s.Peer = from.GetPeer()
	if val, ok := from.GetWallpaper(); ok {
		s.Wallpaper = val
	}

	if val, ok := from.GetSettings(); ok {
		s.Settings = val
	}

	if val, ok := from.GetID(); ok {
		s.ID = val
	}

}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*MessagesSetChatWallPaperRequest) TypeID() uint32 {
	return MessagesSetChatWallPaperRequestTypeID
}

// TypeName returns name of type in TL schema.
func (*MessagesSetChatWallPaperRequest) TypeName() string {
	return "messages.setChatWallPaper"
}

// TypeInfo returns info about TL type.
func (s *MessagesSetChatWallPaperRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.setChatWallPaper",
		ID:   MessagesSetChatWallPaperRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ForBoth",
			SchemaName: "for_both",
			Null:       !s.Flags.Has(3),
		},
		{
			Name:       "Revert",
			SchemaName: "revert",
			Null:       !s.Flags.Has(4),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Wallpaper",
			SchemaName: "wallpaper",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Settings",
			SchemaName: "settings",
			Null:       !s.Flags.Has(2),
		},
		{
			Name:       "ID",
			SchemaName: "id",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSetChatWallPaperRequest) SetFlags() {
	if !(s.ForBoth == false) {
		s.Flags.Set(3)
	}
	if !(s.Revert == false) {
		s.Flags.Set(4)
	}
	if !(s.Wallpaper == nil) {
		s.Flags.Set(0)
	}
	if !(s.Settings.Zero()) {
		s.Flags.Set(2)
	}
	if !(s.ID == 0) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *MessagesSetChatWallPaperRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.setChatWallPaper#8ffacae1 as nil")
	}
	b.PutID(MessagesSetChatWallPaperRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSetChatWallPaperRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.setChatWallPaper#8ffacae1 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.setChatWallPaper#8ffacae1: field flags: %w", err)
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode messages.setChatWallPaper#8ffacae1: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.setChatWallPaper#8ffacae1: field peer: %w", err)
	}
	if s.Flags.Has(0) {
		if s.Wallpaper == nil {
			return fmt.Errorf("unable to encode messages.setChatWallPaper#8ffacae1: field wallpaper is nil")
		}
		if err := s.Wallpaper.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.setChatWallPaper#8ffacae1: field wallpaper: %w", err)
		}
	}
	if s.Flags.Has(2) {
		if err := s.Settings.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.setChatWallPaper#8ffacae1: field settings: %w", err)
		}
	}
	if s.Flags.Has(1) {
		b.PutInt(s.ID)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSetChatWallPaperRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.setChatWallPaper#8ffacae1 to nil")
	}
	if err := b.ConsumeID(MessagesSetChatWallPaperRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.setChatWallPaper#8ffacae1: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSetChatWallPaperRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.setChatWallPaper#8ffacae1 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.setChatWallPaper#8ffacae1: field flags: %w", err)
		}
	}
	s.ForBoth = s.Flags.Has(3)
	s.Revert = s.Flags.Has(4)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.setChatWallPaper#8ffacae1: field peer: %w", err)
		}
		s.Peer = value
	}
	if s.Flags.Has(0) {
		value, err := DecodeInputWallPaper(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.setChatWallPaper#8ffacae1: field wallpaper: %w", err)
		}
		s.Wallpaper = value
	}
	if s.Flags.Has(2) {
		if err := s.Settings.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.setChatWallPaper#8ffacae1: field settings: %w", err)
		}
	}
	if s.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.setChatWallPaper#8ffacae1: field id: %w", err)
		}
		s.ID = value
	}
	return nil
}

// SetForBoth sets value of ForBoth conditional field.
func (s *MessagesSetChatWallPaperRequest) SetForBoth(value bool) {
	if value {
		s.Flags.Set(3)
		s.ForBoth = true
	} else {
		s.Flags.Unset(3)
		s.ForBoth = false
	}
}

// GetForBoth returns value of ForBoth conditional field.
func (s *MessagesSetChatWallPaperRequest) GetForBoth() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(3)
}

// SetRevert sets value of Revert conditional field.
func (s *MessagesSetChatWallPaperRequest) SetRevert(value bool) {
	if value {
		s.Flags.Set(4)
		s.Revert = true
	} else {
		s.Flags.Unset(4)
		s.Revert = false
	}
}

// GetRevert returns value of Revert conditional field.
func (s *MessagesSetChatWallPaperRequest) GetRevert() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(4)
}

// GetPeer returns value of Peer field.
func (s *MessagesSetChatWallPaperRequest) GetPeer() (value InputPeerClass) {
	if s == nil {
		return
	}
	return s.Peer
}

// SetWallpaper sets value of Wallpaper conditional field.
func (s *MessagesSetChatWallPaperRequest) SetWallpaper(value InputWallPaperClass) {
	s.Flags.Set(0)
	s.Wallpaper = value
}

// GetWallpaper returns value of Wallpaper conditional field and
// boolean which is true if field was set.
func (s *MessagesSetChatWallPaperRequest) GetWallpaper() (value InputWallPaperClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Wallpaper, true
}

// SetSettings sets value of Settings conditional field.
func (s *MessagesSetChatWallPaperRequest) SetSettings(value WallPaperSettings) {
	s.Flags.Set(2)
	s.Settings = value
}

// GetSettings returns value of Settings conditional field and
// boolean which is true if field was set.
func (s *MessagesSetChatWallPaperRequest) GetSettings() (value WallPaperSettings, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(2) {
		return value, false
	}
	return s.Settings, true
}

// SetID sets value of ID conditional field.
func (s *MessagesSetChatWallPaperRequest) SetID(value int) {
	s.Flags.Set(1)
	s.ID = value
}

// GetID returns value of ID conditional field and
// boolean which is true if field was set.
func (s *MessagesSetChatWallPaperRequest) GetID() (value int, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.ID, true
}

// MessagesSetChatWallPaper invokes method messages.setChatWallPaper#8ffacae1 returning error if any.
// Set a custom wallpaper »¹ in a specific private chat with another user.
//
// Links:
//  1. https://core.telegram.org/api/wallpapers
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 WALLPAPER_INVALID: The specified wallpaper is invalid.
//	400 WALLPAPER_NOT_FOUND: The specified wallpaper could not be found.
//
// See https://core.telegram.org/method/messages.setChatWallPaper for reference.
func (c *Client) MessagesSetChatWallPaper(ctx context.Context, request *MessagesSetChatWallPaperRequest) (UpdatesClass, error) {
	var result UpdatesBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return result.Updates, nil
}
