// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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{}
)

// SetPinnedGiftsRequest represents TL type `setPinnedGifts#4263f7cc`.
type SetPinnedGiftsRequest struct {
	// Identifier of the user or the channel chat that received the gifts
	OwnerID MessageSenderClass
	// New list of pinned gifts. All gifts must be upgraded and saved on the profile page
	// first. There can be up to getOption("pinned_gift_count_max") pinned gifts
	ReceivedGiftIDs []string
}

// SetPinnedGiftsRequestTypeID is TL type id of SetPinnedGiftsRequest.
const SetPinnedGiftsRequestTypeID = 0x4263f7cc

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

func (s *SetPinnedGiftsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.OwnerID == nil) {
		return false
	}
	if !(s.ReceivedGiftIDs == nil) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*SetPinnedGiftsRequest) TypeName() string {
	return "setPinnedGifts"
}

// TypeInfo returns info about TL type.
func (s *SetPinnedGiftsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setPinnedGifts",
		ID:   SetPinnedGiftsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OwnerID",
			SchemaName: "owner_id",
		},
		{
			Name:       "ReceivedGiftIDs",
			SchemaName: "received_gift_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetPinnedGiftsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPinnedGifts#4263f7cc as nil")
	}
	b.PutID(SetPinnedGiftsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetPinnedGiftsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPinnedGifts#4263f7cc as nil")
	}
	if s.OwnerID == nil {
		return fmt.Errorf("unable to encode setPinnedGifts#4263f7cc: field owner_id is nil")
	}
	if err := s.OwnerID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setPinnedGifts#4263f7cc: field owner_id: %w", err)
	}
	b.PutInt(len(s.ReceivedGiftIDs))
	for _, v := range s.ReceivedGiftIDs {
		b.PutString(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetPinnedGiftsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPinnedGifts#4263f7cc to nil")
	}
	if err := b.ConsumeID(SetPinnedGiftsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetPinnedGiftsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPinnedGifts#4263f7cc to nil")
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: field owner_id: %w", err)
		}
		s.OwnerID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: field received_gift_ids: %w", err)
		}

		if headerLen > 0 {
			s.ReceivedGiftIDs = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: field received_gift_ids: %w", err)
			}
			s.ReceivedGiftIDs = append(s.ReceivedGiftIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetPinnedGiftsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setPinnedGifts#4263f7cc as nil")
	}
	b.ObjStart()
	b.PutID("setPinnedGifts")
	b.Comma()
	b.FieldStart("owner_id")
	if s.OwnerID == nil {
		return fmt.Errorf("unable to encode setPinnedGifts#4263f7cc: field owner_id is nil")
	}
	if err := s.OwnerID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setPinnedGifts#4263f7cc: field owner_id: %w", err)
	}
	b.Comma()
	b.FieldStart("received_gift_ids")
	b.ArrStart()
	for _, v := range s.ReceivedGiftIDs {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetPinnedGiftsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setPinnedGifts#4263f7cc to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setPinnedGifts"); err != nil {
				return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: %w", err)
			}
		case "owner_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: field owner_id: %w", err)
			}
			s.OwnerID = value
		case "received_gift_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.String()
				if err != nil {
					return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: field received_gift_ids: %w", err)
				}
				s.ReceivedGiftIDs = append(s.ReceivedGiftIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode setPinnedGifts#4263f7cc: field received_gift_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOwnerID returns value of OwnerID field.
func (s *SetPinnedGiftsRequest) GetOwnerID() (value MessageSenderClass) {
	if s == nil {
		return
	}
	return s.OwnerID
}

// GetReceivedGiftIDs returns value of ReceivedGiftIDs field.
func (s *SetPinnedGiftsRequest) GetReceivedGiftIDs() (value []string) {
	if s == nil {
		return
	}
	return s.ReceivedGiftIDs
}

// SetPinnedGifts invokes method setPinnedGifts#4263f7cc returning error if any.
func (c *Client) SetPinnedGifts(ctx context.Context, request *SetPinnedGiftsRequest) error {
	var ok Ok

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