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

// DisconnectAffiliateProgramRequest represents TL type `disconnectAffiliateProgram#f9b124fc`.
type DisconnectAffiliateProgramRequest struct {
	// The affiliate to which the affiliate program is connected
	Affiliate AffiliateTypeClass
	// The referral link of the affiliate program
	URL string
}

// DisconnectAffiliateProgramRequestTypeID is TL type id of DisconnectAffiliateProgramRequest.
const DisconnectAffiliateProgramRequestTypeID = 0xf9b124fc

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

func (d *DisconnectAffiliateProgramRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Affiliate == nil) {
		return false
	}
	if !(d.URL == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DisconnectAffiliateProgramRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "disconnectAffiliateProgram",
		ID:   DisconnectAffiliateProgramRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Affiliate",
			SchemaName: "affiliate",
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DisconnectAffiliateProgramRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode disconnectAffiliateProgram#f9b124fc as nil")
	}
	b.PutID(DisconnectAffiliateProgramRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DisconnectAffiliateProgramRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode disconnectAffiliateProgram#f9b124fc as nil")
	}
	if d.Affiliate == nil {
		return fmt.Errorf("unable to encode disconnectAffiliateProgram#f9b124fc: field affiliate is nil")
	}
	if err := d.Affiliate.Encode(b); err != nil {
		return fmt.Errorf("unable to encode disconnectAffiliateProgram#f9b124fc: field affiliate: %w", err)
	}
	b.PutString(d.URL)
	return nil
}

// Decode implements bin.Decoder.
func (d *DisconnectAffiliateProgramRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode disconnectAffiliateProgram#f9b124fc to nil")
	}
	if err := b.ConsumeID(DisconnectAffiliateProgramRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode disconnectAffiliateProgram#f9b124fc: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DisconnectAffiliateProgramRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode disconnectAffiliateProgram#f9b124fc to nil")
	}
	{
		value, err := DecodeAffiliateType(b)
		if err != nil {
			return fmt.Errorf("unable to decode disconnectAffiliateProgram#f9b124fc: field affiliate: %w", err)
		}
		d.Affiliate = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode disconnectAffiliateProgram#f9b124fc: field url: %w", err)
		}
		d.URL = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DisconnectAffiliateProgramRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode disconnectAffiliateProgram#f9b124fc as nil")
	}
	b.ObjStart()
	b.PutID("disconnectAffiliateProgram")
	b.Comma()
	b.FieldStart("affiliate")
	if d.Affiliate == nil {
		return fmt.Errorf("unable to encode disconnectAffiliateProgram#f9b124fc: field affiliate is nil")
	}
	if err := d.Affiliate.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode disconnectAffiliateProgram#f9b124fc: field affiliate: %w", err)
	}
	b.Comma()
	b.FieldStart("url")
	b.PutString(d.URL)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DisconnectAffiliateProgramRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode disconnectAffiliateProgram#f9b124fc to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("disconnectAffiliateProgram"); err != nil {
				return fmt.Errorf("unable to decode disconnectAffiliateProgram#f9b124fc: %w", err)
			}
		case "affiliate":
			value, err := DecodeTDLibJSONAffiliateType(b)
			if err != nil {
				return fmt.Errorf("unable to decode disconnectAffiliateProgram#f9b124fc: field affiliate: %w", err)
			}
			d.Affiliate = value
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode disconnectAffiliateProgram#f9b124fc: field url: %w", err)
			}
			d.URL = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetURL returns value of URL field.
func (d *DisconnectAffiliateProgramRequest) GetURL() (value string) {
	if d == nil {
		return
	}
	return d.URL
}

// DisconnectAffiliateProgram invokes method disconnectAffiliateProgram#f9b124fc returning error if any.
func (c *Client) DisconnectAffiliateProgram(ctx context.Context, request *DisconnectAffiliateProgramRequest) (*ConnectedAffiliateProgram, error) {
	var result ConnectedAffiliateProgram

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