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

// SendResoldGiftRequest represents TL type `sendResoldGift#b36b4f6c`.
type SendResoldGiftRequest struct {
	// Name of the upgraded gift to send
	GiftName string
	// Identifier of the user or the channel chat that will receive the gift
	OwnerID MessageSenderClass
	// The amount of Telegram Stars required to pay for the gift
	StarCount int64
}

// SendResoldGiftRequestTypeID is TL type id of SendResoldGiftRequest.
const SendResoldGiftRequestTypeID = 0xb36b4f6c

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

func (s *SendResoldGiftRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.GiftName == "") {
		return false
	}
	if !(s.OwnerID == nil) {
		return false
	}
	if !(s.StarCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SendResoldGiftRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sendResoldGift",
		ID:   SendResoldGiftRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GiftName",
			SchemaName: "gift_name",
		},
		{
			Name:       "OwnerID",
			SchemaName: "owner_id",
		},
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SendResoldGiftRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendResoldGift#b36b4f6c as nil")
	}
	b.PutID(SendResoldGiftRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SendResoldGiftRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendResoldGift#b36b4f6c as nil")
	}
	b.PutString(s.GiftName)
	if s.OwnerID == nil {
		return fmt.Errorf("unable to encode sendResoldGift#b36b4f6c: field owner_id is nil")
	}
	if err := s.OwnerID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendResoldGift#b36b4f6c: field owner_id: %w", err)
	}
	b.PutInt53(s.StarCount)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SendResoldGiftRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendResoldGift#b36b4f6c to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendResoldGift#b36b4f6c: field gift_name: %w", err)
		}
		s.GiftName = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendResoldGift#b36b4f6c: field owner_id: %w", err)
		}
		s.OwnerID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode sendResoldGift#b36b4f6c: field star_count: %w", err)
		}
		s.StarCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SendResoldGiftRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sendResoldGift#b36b4f6c as nil")
	}
	b.ObjStart()
	b.PutID("sendResoldGift")
	b.Comma()
	b.FieldStart("gift_name")
	b.PutString(s.GiftName)
	b.Comma()
	b.FieldStart("owner_id")
	if s.OwnerID == nil {
		return fmt.Errorf("unable to encode sendResoldGift#b36b4f6c: field owner_id is nil")
	}
	if err := s.OwnerID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode sendResoldGift#b36b4f6c: field owner_id: %w", err)
	}
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(s.StarCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sendResoldGift"); err != nil {
				return fmt.Errorf("unable to decode sendResoldGift#b36b4f6c: %w", err)
			}
		case "gift_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sendResoldGift#b36b4f6c: field gift_name: %w", err)
			}
			s.GiftName = value
		case "owner_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendResoldGift#b36b4f6c: field owner_id: %w", err)
			}
			s.OwnerID = value
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode sendResoldGift#b36b4f6c: field star_count: %w", err)
			}
			s.StarCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGiftName returns value of GiftName field.
func (s *SendResoldGiftRequest) GetGiftName() (value string) {
	if s == nil {
		return
	}
	return s.GiftName
}

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

// GetStarCount returns value of StarCount field.
func (s *SendResoldGiftRequest) GetStarCount() (value int64) {
	if s == nil {
		return
	}
	return s.StarCount
}

// SendResoldGift invokes method sendResoldGift#b36b4f6c returning error if any.
func (c *Client) SendResoldGift(ctx context.Context, request *SendResoldGiftRequest) error {
	var ok Ok

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