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

// GiveawayParameters represents TL type `giveawayParameters#16f1de3d`.
type GiveawayParameters struct {
	// Identifier of the supergroup or channel chat, which will be automatically boosted by
	// the winners of the giveaway for duration of the Telegram Premium subscription,
	BoostedChatID int64
	// Identifiers of other supergroup or channel chats that must be subscribed by the users
	// to be eligible for the giveaway. There can be up to
	// getOption("giveaway_additional_chat_count_max") additional chats
	AdditionalChatIDs []int64
	// Point in time (Unix timestamp) when the giveaway is expected to be performed; must be
	// 60-getOption("giveaway_duration_max") seconds in the future in scheduled giveaways
	WinnersSelectionDate int32
	// True, if only new members of the chats will be eligible for the giveaway
	OnlyNewMembers bool
	// True, if the list of winners of the giveaway will be available to everyone
	HasPublicWinners bool
	// The list of two-letter ISO 3166-1 alpha-2 codes of countries, users from which will be
	// eligible for the giveaway. If empty, then all users can participate in the giveaway.
	CountryCodes []string
	// Additional description of the giveaway prize; 0-128 characters
	PrizeDescription string
}

// GiveawayParametersTypeID is TL type id of GiveawayParameters.
const GiveawayParametersTypeID = 0x16f1de3d

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

func (g *GiveawayParameters) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.BoostedChatID == 0) {
		return false
	}
	if !(g.AdditionalChatIDs == nil) {
		return false
	}
	if !(g.WinnersSelectionDate == 0) {
		return false
	}
	if !(g.OnlyNewMembers == false) {
		return false
	}
	if !(g.HasPublicWinners == false) {
		return false
	}
	if !(g.CountryCodes == nil) {
		return false
	}
	if !(g.PrizeDescription == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GiveawayParameters) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "giveawayParameters",
		ID:   GiveawayParametersTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BoostedChatID",
			SchemaName: "boosted_chat_id",
		},
		{
			Name:       "AdditionalChatIDs",
			SchemaName: "additional_chat_ids",
		},
		{
			Name:       "WinnersSelectionDate",
			SchemaName: "winners_selection_date",
		},
		{
			Name:       "OnlyNewMembers",
			SchemaName: "only_new_members",
		},
		{
			Name:       "HasPublicWinners",
			SchemaName: "has_public_winners",
		},
		{
			Name:       "CountryCodes",
			SchemaName: "country_codes",
		},
		{
			Name:       "PrizeDescription",
			SchemaName: "prize_description",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GiveawayParameters) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giveawayParameters#16f1de3d as nil")
	}
	b.PutID(GiveawayParametersTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GiveawayParameters) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giveawayParameters#16f1de3d as nil")
	}
	b.PutInt53(g.BoostedChatID)
	b.PutInt(len(g.AdditionalChatIDs))
	for _, v := range g.AdditionalChatIDs {
		b.PutInt53(v)
	}
	b.PutInt32(g.WinnersSelectionDate)
	b.PutBool(g.OnlyNewMembers)
	b.PutBool(g.HasPublicWinners)
	b.PutInt(len(g.CountryCodes))
	for _, v := range g.CountryCodes {
		b.PutString(v)
	}
	b.PutString(g.PrizeDescription)
	return nil
}

// Decode implements bin.Decoder.
func (g *GiveawayParameters) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giveawayParameters#16f1de3d to nil")
	}
	if err := b.ConsumeID(GiveawayParametersTypeID); err != nil {
		return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GiveawayParameters) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giveawayParameters#16f1de3d to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field boosted_chat_id: %w", err)
		}
		g.BoostedChatID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field additional_chat_ids: %w", err)
		}

		if headerLen > 0 {
			g.AdditionalChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field additional_chat_ids: %w", err)
			}
			g.AdditionalChatIDs = append(g.AdditionalChatIDs, value)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field winners_selection_date: %w", err)
		}
		g.WinnersSelectionDate = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field only_new_members: %w", err)
		}
		g.OnlyNewMembers = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field has_public_winners: %w", err)
		}
		g.HasPublicWinners = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field country_codes: %w", err)
		}

		if headerLen > 0 {
			g.CountryCodes = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field country_codes: %w", err)
			}
			g.CountryCodes = append(g.CountryCodes, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field prize_description: %w", err)
		}
		g.PrizeDescription = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GiveawayParameters) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode giveawayParameters#16f1de3d as nil")
	}
	b.ObjStart()
	b.PutID("giveawayParameters")
	b.Comma()
	b.FieldStart("boosted_chat_id")
	b.PutInt53(g.BoostedChatID)
	b.Comma()
	b.FieldStart("additional_chat_ids")
	b.ArrStart()
	for _, v := range g.AdditionalChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("winners_selection_date")
	b.PutInt32(g.WinnersSelectionDate)
	b.Comma()
	b.FieldStart("only_new_members")
	b.PutBool(g.OnlyNewMembers)
	b.Comma()
	b.FieldStart("has_public_winners")
	b.PutBool(g.HasPublicWinners)
	b.Comma()
	b.FieldStart("country_codes")
	b.ArrStart()
	for _, v := range g.CountryCodes {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("prize_description")
	b.PutString(g.PrizeDescription)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GiveawayParameters) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode giveawayParameters#16f1de3d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("giveawayParameters"); err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: %w", err)
			}
		case "boosted_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field boosted_chat_id: %w", err)
			}
			g.BoostedChatID = value
		case "additional_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field additional_chat_ids: %w", err)
				}
				g.AdditionalChatIDs = append(g.AdditionalChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field additional_chat_ids: %w", err)
			}
		case "winners_selection_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field winners_selection_date: %w", err)
			}
			g.WinnersSelectionDate = value
		case "only_new_members":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field only_new_members: %w", err)
			}
			g.OnlyNewMembers = value
		case "has_public_winners":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field has_public_winners: %w", err)
			}
			g.HasPublicWinners = value
		case "country_codes":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.String()
				if err != nil {
					return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field country_codes: %w", err)
				}
				g.CountryCodes = append(g.CountryCodes, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field country_codes: %w", err)
			}
		case "prize_description":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode giveawayParameters#16f1de3d: field prize_description: %w", err)
			}
			g.PrizeDescription = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBoostedChatID returns value of BoostedChatID field.
func (g *GiveawayParameters) GetBoostedChatID() (value int64) {
	if g == nil {
		return
	}
	return g.BoostedChatID
}

// GetAdditionalChatIDs returns value of AdditionalChatIDs field.
func (g *GiveawayParameters) GetAdditionalChatIDs() (value []int64) {
	if g == nil {
		return
	}
	return g.AdditionalChatIDs
}

// GetWinnersSelectionDate returns value of WinnersSelectionDate field.
func (g *GiveawayParameters) GetWinnersSelectionDate() (value int32) {
	if g == nil {
		return
	}
	return g.WinnersSelectionDate
}

// GetOnlyNewMembers returns value of OnlyNewMembers field.
func (g *GiveawayParameters) GetOnlyNewMembers() (value bool) {
	if g == nil {
		return
	}
	return g.OnlyNewMembers
}

// GetHasPublicWinners returns value of HasPublicWinners field.
func (g *GiveawayParameters) GetHasPublicWinners() (value bool) {
	if g == nil {
		return
	}
	return g.HasPublicWinners
}

// GetCountryCodes returns value of CountryCodes field.
func (g *GiveawayParameters) GetCountryCodes() (value []string) {
	if g == nil {
		return
	}
	return g.CountryCodes
}

// GetPrizeDescription returns value of PrizeDescription field.
func (g *GiveawayParameters) GetPrizeDescription() (value string) {
	if g == nil {
		return
	}
	return g.PrizeDescription
}
