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

// ConnectedBotStarRef represents TL type `connectedBotStarRef#19a13f71`.
// Info about an active affiliate program we have with a Mini App¹
//
// Links:
//  1. https://core.telegram.org/api/bots/referrals#becoming-an-affiliate
//
// See https://core.telegram.org/constructor/connectedBotStarRef for reference.
type ConnectedBotStarRef struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, this affiliation was revoked by the affiliate using payments
	// editConnectedStarRefBot¹, or by the affiliation program owner using bots
	// updateStarRefProgram²
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.editConnectedStarRefBot
	//  2) https://core.telegram.org/method/bots.updateStarRefProgram
	Revoked bool
	// Referral link¹ to be shared
	//
	// Links:
	//  1) https://core.telegram.org/api/links#referral-links
	URL string
	// When did we affiliate with bot_id
	Date int
	// ID of the mini app that created the affiliate program
	BotID int64
	// The number of Telegram Stars received by the affiliate for each 1000 Telegram Stars
	// received by bot_id
	CommissionPermille int
	// Number of months the program will be active; if not set, there is no expiration date.
	//
	// Use SetDurationMonths and GetDurationMonths helpers.
	DurationMonths int
	// The number of users that used the affiliate program
	Participants int64
	// The number of Telegram Stars that were earned by the affiliate program
	Revenue int64
}

// ConnectedBotStarRefTypeID is TL type id of ConnectedBotStarRef.
const ConnectedBotStarRefTypeID = 0x19a13f71

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

func (c *ConnectedBotStarRef) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Revoked == false) {
		return false
	}
	if !(c.URL == "") {
		return false
	}
	if !(c.Date == 0) {
		return false
	}
	if !(c.BotID == 0) {
		return false
	}
	if !(c.CommissionPermille == 0) {
		return false
	}
	if !(c.DurationMonths == 0) {
		return false
	}
	if !(c.Participants == 0) {
		return false
	}
	if !(c.Revenue == 0) {
		return false
	}

	return true
}

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

// FillFrom fills ConnectedBotStarRef from given interface.
func (c *ConnectedBotStarRef) FillFrom(from interface {
	GetRevoked() (value bool)
	GetURL() (value string)
	GetDate() (value int)
	GetBotID() (value int64)
	GetCommissionPermille() (value int)
	GetDurationMonths() (value int, ok bool)
	GetParticipants() (value int64)
	GetRevenue() (value int64)
}) {
	c.Revoked = from.GetRevoked()
	c.URL = from.GetURL()
	c.Date = from.GetDate()
	c.BotID = from.GetBotID()
	c.CommissionPermille = from.GetCommissionPermille()
	if val, ok := from.GetDurationMonths(); ok {
		c.DurationMonths = val
	}

	c.Participants = from.GetParticipants()
	c.Revenue = from.GetRevenue()
}

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

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

// TypeInfo returns info about TL type.
func (c *ConnectedBotStarRef) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "connectedBotStarRef",
		ID:   ConnectedBotStarRefTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Revoked",
			SchemaName: "revoked",
			Null:       !c.Flags.Has(1),
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "BotID",
			SchemaName: "bot_id",
		},
		{
			Name:       "CommissionPermille",
			SchemaName: "commission_permille",
		},
		{
			Name:       "DurationMonths",
			SchemaName: "duration_months",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "Participants",
			SchemaName: "participants",
		},
		{
			Name:       "Revenue",
			SchemaName: "revenue",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *ConnectedBotStarRef) SetFlags() {
	if !(c.Revoked == false) {
		c.Flags.Set(1)
	}
	if !(c.DurationMonths == 0) {
		c.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (c *ConnectedBotStarRef) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedBotStarRef#19a13f71 as nil")
	}
	b.PutID(ConnectedBotStarRefTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ConnectedBotStarRef) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedBotStarRef#19a13f71 as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode connectedBotStarRef#19a13f71: field flags: %w", err)
	}
	b.PutString(c.URL)
	b.PutInt(c.Date)
	b.PutLong(c.BotID)
	b.PutInt(c.CommissionPermille)
	if c.Flags.Has(0) {
		b.PutInt(c.DurationMonths)
	}
	b.PutLong(c.Participants)
	b.PutLong(c.Revenue)
	return nil
}

// Decode implements bin.Decoder.
func (c *ConnectedBotStarRef) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedBotStarRef#19a13f71 to nil")
	}
	if err := b.ConsumeID(ConnectedBotStarRefTypeID); err != nil {
		return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ConnectedBotStarRef) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedBotStarRef#19a13f71 to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field flags: %w", err)
		}
	}
	c.Revoked = c.Flags.Has(1)
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field url: %w", err)
		}
		c.URL = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field date: %w", err)
		}
		c.Date = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field bot_id: %w", err)
		}
		c.BotID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field commission_permille: %w", err)
		}
		c.CommissionPermille = value
	}
	if c.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field duration_months: %w", err)
		}
		c.DurationMonths = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field participants: %w", err)
		}
		c.Participants = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode connectedBotStarRef#19a13f71: field revenue: %w", err)
		}
		c.Revenue = value
	}
	return nil
}

// SetRevoked sets value of Revoked conditional field.
func (c *ConnectedBotStarRef) SetRevoked(value bool) {
	if value {
		c.Flags.Set(1)
		c.Revoked = true
	} else {
		c.Flags.Unset(1)
		c.Revoked = false
	}
}

// GetRevoked returns value of Revoked conditional field.
func (c *ConnectedBotStarRef) GetRevoked() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(1)
}

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

// GetDate returns value of Date field.
func (c *ConnectedBotStarRef) GetDate() (value int) {
	if c == nil {
		return
	}
	return c.Date
}

// GetBotID returns value of BotID field.
func (c *ConnectedBotStarRef) GetBotID() (value int64) {
	if c == nil {
		return
	}
	return c.BotID
}

// GetCommissionPermille returns value of CommissionPermille field.
func (c *ConnectedBotStarRef) GetCommissionPermille() (value int) {
	if c == nil {
		return
	}
	return c.CommissionPermille
}

// SetDurationMonths sets value of DurationMonths conditional field.
func (c *ConnectedBotStarRef) SetDurationMonths(value int) {
	c.Flags.Set(0)
	c.DurationMonths = value
}

// GetDurationMonths returns value of DurationMonths conditional field and
// boolean which is true if field was set.
func (c *ConnectedBotStarRef) GetDurationMonths() (value int, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.DurationMonths, true
}

// GetParticipants returns value of Participants field.
func (c *ConnectedBotStarRef) GetParticipants() (value int64) {
	if c == nil {
		return
	}
	return c.Participants
}

// GetRevenue returns value of Revenue field.
func (c *ConnectedBotStarRef) GetRevenue() (value int64) {
	if c == nil {
		return
	}
	return c.Revenue
}
