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

// SellGiftRequest represents TL type `sellGift#ee3853f9`.
type SellGiftRequest struct {
	// Unique identifier of business connection on behalf of which to send the request; for
	// bots only
	BusinessConnectionID string
	// Identifier of the gift
	ReceivedGiftID string
}

// SellGiftRequestTypeID is TL type id of SellGiftRequest.
const SellGiftRequestTypeID = 0xee3853f9

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

func (s *SellGiftRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.BusinessConnectionID == "") {
		return false
	}
	if !(s.ReceivedGiftID == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SellGiftRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sellGift",
		ID:   SellGiftRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "ReceivedGiftID",
			SchemaName: "received_gift_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SellGiftRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sellGift#ee3853f9 as nil")
	}
	b.PutID(SellGiftRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SellGiftRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sellGift#ee3853f9 as nil")
	}
	b.PutString(s.BusinessConnectionID)
	b.PutString(s.ReceivedGiftID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SellGiftRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sellGift#ee3853f9 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sellGift#ee3853f9: field business_connection_id: %w", err)
		}
		s.BusinessConnectionID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sellGift#ee3853f9: field received_gift_id: %w", err)
		}
		s.ReceivedGiftID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SellGiftRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sellGift#ee3853f9 as nil")
	}
	b.ObjStart()
	b.PutID("sellGift")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(s.BusinessConnectionID)
	b.Comma()
	b.FieldStart("received_gift_id")
	b.PutString(s.ReceivedGiftID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sellGift"); err != nil {
				return fmt.Errorf("unable to decode sellGift#ee3853f9: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sellGift#ee3853f9: field business_connection_id: %w", err)
			}
			s.BusinessConnectionID = value
		case "received_gift_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sellGift#ee3853f9: field received_gift_id: %w", err)
			}
			s.ReceivedGiftID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBusinessConnectionID returns value of BusinessConnectionID field.
func (s *SellGiftRequest) GetBusinessConnectionID() (value string) {
	if s == nil {
		return
	}
	return s.BusinessConnectionID
}

// GetReceivedGiftID returns value of ReceivedGiftID field.
func (s *SellGiftRequest) GetReceivedGiftID() (value string) {
	if s == nil {
		return
	}
	return s.ReceivedGiftID
}

// SellGift invokes method sellGift#ee3853f9 returning error if any.
func (c *Client) SellGift(ctx context.Context, request *SellGiftRequest) error {
	var ok Ok

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