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

// GetStarTransactionsRequest represents TL type `getStarTransactions#f096f8f1`.
type GetStarTransactionsRequest struct {
	// Identifier of the owner of the Telegram Stars; can be the identifier of the current
	// user, identifier of an owned bot,
	OwnerID MessageSenderClass
	// If non-empty, only transactions related to the Star Subscription will be returned
	SubscriptionID string
	// Direction of the transactions to receive; pass null to get all transactions
	Direction StarTransactionDirectionClass
	// Offset of the first transaction to return as received from the previous request; use
	// empty string to get the first chunk of results
	Offset string
	// The maximum number of transactions to return
	Limit int32
}

// GetStarTransactionsRequestTypeID is TL type id of GetStarTransactionsRequest.
const GetStarTransactionsRequestTypeID = 0xf096f8f1

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

func (g *GetStarTransactionsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.OwnerID == nil) {
		return false
	}
	if !(g.SubscriptionID == "") {
		return false
	}
	if !(g.Direction == nil) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetStarTransactionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getStarTransactions",
		ID:   GetStarTransactionsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OwnerID",
			SchemaName: "owner_id",
		},
		{
			Name:       "SubscriptionID",
			SchemaName: "subscription_id",
		},
		{
			Name:       "Direction",
			SchemaName: "direction",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetStarTransactionsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStarTransactions#f096f8f1 as nil")
	}
	b.PutID(GetStarTransactionsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetStarTransactionsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStarTransactions#f096f8f1 as nil")
	}
	if g.OwnerID == nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field owner_id is nil")
	}
	if err := g.OwnerID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field owner_id: %w", err)
	}
	b.PutString(g.SubscriptionID)
	if g.Direction == nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field direction is nil")
	}
	if err := g.Direction.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field direction: %w", err)
	}
	b.PutString(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetStarTransactionsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStarTransactions#f096f8f1 to nil")
	}
	if err := b.ConsumeID(GetStarTransactionsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetStarTransactionsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStarTransactions#f096f8f1 to nil")
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field owner_id: %w", err)
		}
		g.OwnerID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field subscription_id: %w", err)
		}
		g.SubscriptionID = value
	}
	{
		value, err := DecodeStarTransactionDirection(b)
		if err != nil {
			return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field direction: %w", err)
		}
		g.Direction = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetStarTransactionsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getStarTransactions#f096f8f1 as nil")
	}
	b.ObjStart()
	b.PutID("getStarTransactions")
	b.Comma()
	b.FieldStart("owner_id")
	if g.OwnerID == nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field owner_id is nil")
	}
	if err := g.OwnerID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field owner_id: %w", err)
	}
	b.Comma()
	b.FieldStart("subscription_id")
	b.PutString(g.SubscriptionID)
	b.Comma()
	b.FieldStart("direction")
	if g.Direction == nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field direction is nil")
	}
	if err := g.Direction.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getStarTransactions#f096f8f1: field direction: %w", err)
	}
	b.Comma()
	b.FieldStart("offset")
	b.PutString(g.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetStarTransactionsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getStarTransactions#f096f8f1 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getStarTransactions"); err != nil {
				return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: %w", err)
			}
		case "owner_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field owner_id: %w", err)
			}
			g.OwnerID = value
		case "subscription_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field subscription_id: %w", err)
			}
			g.SubscriptionID = value
		case "direction":
			value, err := DecodeTDLibJSONStarTransactionDirection(b)
			if err != nil {
				return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field direction: %w", err)
			}
			g.Direction = value
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getStarTransactions#f096f8f1: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetSubscriptionID returns value of SubscriptionID field.
func (g *GetStarTransactionsRequest) GetSubscriptionID() (value string) {
	if g == nil {
		return
	}
	return g.SubscriptionID
}

// GetDirection returns value of Direction field.
func (g *GetStarTransactionsRequest) GetDirection() (value StarTransactionDirectionClass) {
	if g == nil {
		return
	}
	return g.Direction
}

// GetOffset returns value of Offset field.
func (g *GetStarTransactionsRequest) GetOffset() (value string) {
	if g == nil {
		return
	}
	return g.Offset
}

// GetLimit returns value of Limit field.
func (g *GetStarTransactionsRequest) GetLimit() (value int32) {
	if g == nil {
		return
	}
	return g.Limit
}

// GetStarTransactions invokes method getStarTransactions#f096f8f1 returning error if any.
func (c *Client) GetStarTransactions(ctx context.Context, request *GetStarTransactionsRequest) (*StarTransactions, error) {
	var result StarTransactions

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