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

// ConnectedAffiliateProgram represents TL type `connectedAffiliateProgram#58bf7415`.
type ConnectedAffiliateProgram struct {
	// The link that can be used to refer users if the program is still active
	URL string
	// User identifier of the bot created the program
	BotUserID int64
	// The parameters of the affiliate program
	Parameters AffiliateProgramParameters
	// Point in time (Unix timestamp) when the affiliate program was connected
	ConnectionDate int32
	// True, if the program was canceled by the bot, or disconnected by the chat owner and
	// isn't available anymore
	IsDisconnected bool
	// The number of users that used the affiliate program
	UserCount int64
	// The number of Telegram Stars that were earned by the affiliate program
	RevenueStarCount int64
}

// ConnectedAffiliateProgramTypeID is TL type id of ConnectedAffiliateProgram.
const ConnectedAffiliateProgramTypeID = 0x58bf7415

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

func (c *ConnectedAffiliateProgram) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.URL == "") {
		return false
	}
	if !(c.BotUserID == 0) {
		return false
	}
	if !(c.Parameters.Zero()) {
		return false
	}
	if !(c.ConnectionDate == 0) {
		return false
	}
	if !(c.IsDisconnected == false) {
		return false
	}
	if !(c.UserCount == 0) {
		return false
	}
	if !(c.RevenueStarCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ConnectedAffiliateProgram) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "connectedAffiliateProgram",
		ID:   ConnectedAffiliateProgramTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
		{
			Name:       "ConnectionDate",
			SchemaName: "connection_date",
		},
		{
			Name:       "IsDisconnected",
			SchemaName: "is_disconnected",
		},
		{
			Name:       "UserCount",
			SchemaName: "user_count",
		},
		{
			Name:       "RevenueStarCount",
			SchemaName: "revenue_star_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ConnectedAffiliateProgram) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedAffiliateProgram#58bf7415 as nil")
	}
	b.PutID(ConnectedAffiliateProgramTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ConnectedAffiliateProgram) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedAffiliateProgram#58bf7415 as nil")
	}
	b.PutString(c.URL)
	b.PutInt53(c.BotUserID)
	if err := c.Parameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode connectedAffiliateProgram#58bf7415: field parameters: %w", err)
	}
	b.PutInt32(c.ConnectionDate)
	b.PutBool(c.IsDisconnected)
	b.PutLong(c.UserCount)
	b.PutLong(c.RevenueStarCount)
	return nil
}

// Decode implements bin.Decoder.
func (c *ConnectedAffiliateProgram) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedAffiliateProgram#58bf7415 to nil")
	}
	if err := b.ConsumeID(ConnectedAffiliateProgramTypeID); err != nil {
		return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ConnectedAffiliateProgram) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedAffiliateProgram#58bf7415 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field url: %w", err)
		}
		c.URL = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field bot_user_id: %w", err)
		}
		c.BotUserID = value
	}
	{
		if err := c.Parameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field parameters: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field connection_date: %w", err)
		}
		c.ConnectionDate = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field is_disconnected: %w", err)
		}
		c.IsDisconnected = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field user_count: %w", err)
		}
		c.UserCount = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field revenue_star_count: %w", err)
		}
		c.RevenueStarCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ConnectedAffiliateProgram) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedAffiliateProgram#58bf7415 as nil")
	}
	b.ObjStart()
	b.PutID("connectedAffiliateProgram")
	b.Comma()
	b.FieldStart("url")
	b.PutString(c.URL)
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(c.BotUserID)
	b.Comma()
	b.FieldStart("parameters")
	if err := c.Parameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode connectedAffiliateProgram#58bf7415: field parameters: %w", err)
	}
	b.Comma()
	b.FieldStart("connection_date")
	b.PutInt32(c.ConnectionDate)
	b.Comma()
	b.FieldStart("is_disconnected")
	b.PutBool(c.IsDisconnected)
	b.Comma()
	b.FieldStart("user_count")
	b.PutLong(c.UserCount)
	b.Comma()
	b.FieldStart("revenue_star_count")
	b.PutLong(c.RevenueStarCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ConnectedAffiliateProgram) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedAffiliateProgram#58bf7415 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("connectedAffiliateProgram"); err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: %w", err)
			}
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field url: %w", err)
			}
			c.URL = value
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field bot_user_id: %w", err)
			}
			c.BotUserID = value
		case "parameters":
			if err := c.Parameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field parameters: %w", err)
			}
		case "connection_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field connection_date: %w", err)
			}
			c.ConnectionDate = value
		case "is_disconnected":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field is_disconnected: %w", err)
			}
			c.IsDisconnected = value
		case "user_count":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field user_count: %w", err)
			}
			c.UserCount = value
		case "revenue_star_count":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliateProgram#58bf7415: field revenue_star_count: %w", err)
			}
			c.RevenueStarCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetParameters returns value of Parameters field.
func (c *ConnectedAffiliateProgram) GetParameters() (value AffiliateProgramParameters) {
	if c == nil {
		return
	}
	return c.Parameters
}

// GetConnectionDate returns value of ConnectionDate field.
func (c *ConnectedAffiliateProgram) GetConnectionDate() (value int32) {
	if c == nil {
		return
	}
	return c.ConnectionDate
}

// GetIsDisconnected returns value of IsDisconnected field.
func (c *ConnectedAffiliateProgram) GetIsDisconnected() (value bool) {
	if c == nil {
		return
	}
	return c.IsDisconnected
}

// GetUserCount returns value of UserCount field.
func (c *ConnectedAffiliateProgram) GetUserCount() (value int64) {
	if c == nil {
		return
	}
	return c.UserCount
}

// GetRevenueStarCount returns value of RevenueStarCount field.
func (c *ConnectedAffiliateProgram) GetRevenueStarCount() (value int64) {
	if c == nil {
		return
	}
	return c.RevenueStarCount
}
