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

// PaymentsResolveStarGiftOfferRequest represents TL type `payments.resolveStarGiftOffer#e9ce781c`.
//
// See https://core.telegram.org/method/payments.resolveStarGiftOffer for reference.
type PaymentsResolveStarGiftOfferRequest struct {
	// Flags field of PaymentsResolveStarGiftOfferRequest.
	Flags bin.Fields
	// Decline field of PaymentsResolveStarGiftOfferRequest.
	Decline bool
	// OfferMsgID field of PaymentsResolveStarGiftOfferRequest.
	OfferMsgID int
}

// PaymentsResolveStarGiftOfferRequestTypeID is TL type id of PaymentsResolveStarGiftOfferRequest.
const PaymentsResolveStarGiftOfferRequestTypeID = 0xe9ce781c

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

func (r *PaymentsResolveStarGiftOfferRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Decline == false) {
		return false
	}
	if !(r.OfferMsgID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsResolveStarGiftOfferRequest from given interface.
func (r *PaymentsResolveStarGiftOfferRequest) FillFrom(from interface {
	GetDecline() (value bool)
	GetOfferMsgID() (value int)
}) {
	r.Decline = from.GetDecline()
	r.OfferMsgID = from.GetOfferMsgID()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsResolveStarGiftOfferRequest) TypeName() string {
	return "payments.resolveStarGiftOffer"
}

// TypeInfo returns info about TL type.
func (r *PaymentsResolveStarGiftOfferRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.resolveStarGiftOffer",
		ID:   PaymentsResolveStarGiftOfferRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Decline",
			SchemaName: "decline",
			Null:       !r.Flags.Has(0),
		},
		{
			Name:       "OfferMsgID",
			SchemaName: "offer_msg_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *PaymentsResolveStarGiftOfferRequest) SetFlags() {
	if !(r.Decline == false) {
		r.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (r *PaymentsResolveStarGiftOfferRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode payments.resolveStarGiftOffer#e9ce781c as nil")
	}
	b.PutID(PaymentsResolveStarGiftOfferRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *PaymentsResolveStarGiftOfferRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode payments.resolveStarGiftOffer#e9ce781c as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.resolveStarGiftOffer#e9ce781c: field flags: %w", err)
	}
	b.PutInt(r.OfferMsgID)
	return nil
}

// Decode implements bin.Decoder.
func (r *PaymentsResolveStarGiftOfferRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode payments.resolveStarGiftOffer#e9ce781c to nil")
	}
	if err := b.ConsumeID(PaymentsResolveStarGiftOfferRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.resolveStarGiftOffer#e9ce781c: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *PaymentsResolveStarGiftOfferRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode payments.resolveStarGiftOffer#e9ce781c to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.resolveStarGiftOffer#e9ce781c: field flags: %w", err)
		}
	}
	r.Decline = r.Flags.Has(0)
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.resolveStarGiftOffer#e9ce781c: field offer_msg_id: %w", err)
		}
		r.OfferMsgID = value
	}
	return nil
}

// SetDecline sets value of Decline conditional field.
func (r *PaymentsResolveStarGiftOfferRequest) SetDecline(value bool) {
	if value {
		r.Flags.Set(0)
		r.Decline = true
	} else {
		r.Flags.Unset(0)
		r.Decline = false
	}
}

// GetDecline returns value of Decline conditional field.
func (r *PaymentsResolveStarGiftOfferRequest) GetDecline() (value bool) {
	if r == nil {
		return
	}
	return r.Flags.Has(0)
}

// GetOfferMsgID returns value of OfferMsgID field.
func (r *PaymentsResolveStarGiftOfferRequest) GetOfferMsgID() (value int) {
	if r == nil {
		return
	}
	return r.OfferMsgID
}

// PaymentsResolveStarGiftOffer invokes method payments.resolveStarGiftOffer#e9ce781c returning error if any.
//
// See https://core.telegram.org/method/payments.resolveStarGiftOffer for reference.
func (c *Client) PaymentsResolveStarGiftOffer(ctx context.Context, request *PaymentsResolveStarGiftOfferRequest) (UpdatesClass, error) {
	var result UpdatesBox

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