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

// GetConnectedAffiliateProgramRequest represents TL type `getConnectedAffiliateProgram#9761e770`.
type GetConnectedAffiliateProgramRequest struct {
	// The affiliate to which the affiliate program will be connected
	Affiliate AffiliateTypeClass
	// Identifier of the bot that created the program
	BotUserID int64
}

// GetConnectedAffiliateProgramRequestTypeID is TL type id of GetConnectedAffiliateProgramRequest.
const GetConnectedAffiliateProgramRequestTypeID = 0x9761e770

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

func (g *GetConnectedAffiliateProgramRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Affiliate == nil) {
		return false
	}
	if !(g.BotUserID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetConnectedAffiliateProgramRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getConnectedAffiliateProgram",
		ID:   GetConnectedAffiliateProgramRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Affiliate",
			SchemaName: "affiliate",
		},
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetConnectedAffiliateProgramRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getConnectedAffiliateProgram#9761e770 as nil")
	}
	b.PutID(GetConnectedAffiliateProgramRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetConnectedAffiliateProgramRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getConnectedAffiliateProgram#9761e770 as nil")
	}
	if g.Affiliate == nil {
		return fmt.Errorf("unable to encode getConnectedAffiliateProgram#9761e770: field affiliate is nil")
	}
	if err := g.Affiliate.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getConnectedAffiliateProgram#9761e770: field affiliate: %w", err)
	}
	b.PutInt53(g.BotUserID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetConnectedAffiliateProgramRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getConnectedAffiliateProgram#9761e770 to nil")
	}
	{
		value, err := DecodeAffiliateType(b)
		if err != nil {
			return fmt.Errorf("unable to decode getConnectedAffiliateProgram#9761e770: field affiliate: %w", err)
		}
		g.Affiliate = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getConnectedAffiliateProgram#9761e770: field bot_user_id: %w", err)
		}
		g.BotUserID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetConnectedAffiliateProgramRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getConnectedAffiliateProgram#9761e770 as nil")
	}
	b.ObjStart()
	b.PutID("getConnectedAffiliateProgram")
	b.Comma()
	b.FieldStart("affiliate")
	if g.Affiliate == nil {
		return fmt.Errorf("unable to encode getConnectedAffiliateProgram#9761e770: field affiliate is nil")
	}
	if err := g.Affiliate.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getConnectedAffiliateProgram#9761e770: field affiliate: %w", err)
	}
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getConnectedAffiliateProgram"); err != nil {
				return fmt.Errorf("unable to decode getConnectedAffiliateProgram#9761e770: %w", err)
			}
		case "affiliate":
			value, err := DecodeTDLibJSONAffiliateType(b)
			if err != nil {
				return fmt.Errorf("unable to decode getConnectedAffiliateProgram#9761e770: field affiliate: %w", err)
			}
			g.Affiliate = value
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getConnectedAffiliateProgram#9761e770: field bot_user_id: %w", err)
			}
			g.BotUserID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetAffiliate returns value of Affiliate field.
func (g *GetConnectedAffiliateProgramRequest) GetAffiliate() (value AffiliateTypeClass) {
	if g == nil {
		return
	}
	return g.Affiliate
}

// GetBotUserID returns value of BotUserID field.
func (g *GetConnectedAffiliateProgramRequest) GetBotUserID() (value int64) {
	if g == nil {
		return
	}
	return g.BotUserID
}

// GetConnectedAffiliateProgram invokes method getConnectedAffiliateProgram#9761e770 returning error if any.
func (c *Client) GetConnectedAffiliateProgram(ctx context.Context, request *GetConnectedAffiliateProgramRequest) (*ConnectedAffiliateProgram, error) {
	var result ConnectedAffiliateProgram

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