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

// PaymentsCheckedGiftCode represents TL type `payments.checkedGiftCode#eb983f8f`.
// Contains info about a Telegram Premium giftcode link¹.
//
// Links:
//  1. https://core.telegram.org/api/links#premium-giftcode-links
//
// See https://core.telegram.org/constructor/payments.checkedGiftCode for reference.
type PaymentsCheckedGiftCode struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether this giftcode was created by a giveaway¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/giveaways
	ViaGiveaway bool
	// The peer that created the gift code.
	//
	// Use SetFromID and GetFromID helpers.
	FromID PeerClass
	// Message ID of the giveaway in the channel specified in from_id.
	//
	// Use SetGiveawayMsgID and GetGiveawayMsgID helpers.
	GiveawayMsgID int
	// The destination user of the gift.
	//
	// Use SetToID and GetToID helpers.
	ToID int64
	// Creation date of the gift code.
	Date int
	// Days field of PaymentsCheckedGiftCode.
	Days int
	// When was the giftcode imported, if it was imported.
	//
	// Use SetUsedDate and GetUsedDate helpers.
	UsedDate int
	// Mentioned chats
	Chats []ChatClass
	// Mentioned users
	Users []UserClass
}

// PaymentsCheckedGiftCodeTypeID is TL type id of PaymentsCheckedGiftCode.
const PaymentsCheckedGiftCodeTypeID = 0xeb983f8f

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

func (c *PaymentsCheckedGiftCode) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.ViaGiveaway == false) {
		return false
	}
	if !(c.FromID == nil) {
		return false
	}
	if !(c.GiveawayMsgID == 0) {
		return false
	}
	if !(c.ToID == 0) {
		return false
	}
	if !(c.Date == 0) {
		return false
	}
	if !(c.Days == 0) {
		return false
	}
	if !(c.UsedDate == 0) {
		return false
	}
	if !(c.Chats == nil) {
		return false
	}
	if !(c.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsCheckedGiftCode from given interface.
func (c *PaymentsCheckedGiftCode) FillFrom(from interface {
	GetViaGiveaway() (value bool)
	GetFromID() (value PeerClass, ok bool)
	GetGiveawayMsgID() (value int, ok bool)
	GetToID() (value int64, ok bool)
	GetDate() (value int)
	GetDays() (value int)
	GetUsedDate() (value int, ok bool)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	c.ViaGiveaway = from.GetViaGiveaway()
	if val, ok := from.GetFromID(); ok {
		c.FromID = val
	}

	if val, ok := from.GetGiveawayMsgID(); ok {
		c.GiveawayMsgID = val
	}

	if val, ok := from.GetToID(); ok {
		c.ToID = val
	}

	c.Date = from.GetDate()
	c.Days = from.GetDays()
	if val, ok := from.GetUsedDate(); ok {
		c.UsedDate = val
	}

	c.Chats = from.GetChats()
	c.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsCheckedGiftCode) TypeName() string {
	return "payments.checkedGiftCode"
}

// TypeInfo returns info about TL type.
func (c *PaymentsCheckedGiftCode) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.checkedGiftCode",
		ID:   PaymentsCheckedGiftCodeTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ViaGiveaway",
			SchemaName: "via_giveaway",
			Null:       !c.Flags.Has(2),
		},
		{
			Name:       "FromID",
			SchemaName: "from_id",
			Null:       !c.Flags.Has(4),
		},
		{
			Name:       "GiveawayMsgID",
			SchemaName: "giveaway_msg_id",
			Null:       !c.Flags.Has(3),
		},
		{
			Name:       "ToID",
			SchemaName: "to_id",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Days",
			SchemaName: "days",
		},
		{
			Name:       "UsedDate",
			SchemaName: "used_date",
			Null:       !c.Flags.Has(1),
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *PaymentsCheckedGiftCode) SetFlags() {
	if !(c.ViaGiveaway == false) {
		c.Flags.Set(2)
	}
	if !(c.FromID == nil) {
		c.Flags.Set(4)
	}
	if !(c.GiveawayMsgID == 0) {
		c.Flags.Set(3)
	}
	if !(c.ToID == 0) {
		c.Flags.Set(0)
	}
	if !(c.UsedDate == 0) {
		c.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (c *PaymentsCheckedGiftCode) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.checkedGiftCode#eb983f8f as nil")
	}
	b.PutID(PaymentsCheckedGiftCodeTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *PaymentsCheckedGiftCode) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.checkedGiftCode#eb983f8f as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.checkedGiftCode#eb983f8f: field flags: %w", err)
	}
	if c.Flags.Has(4) {
		if c.FromID == nil {
			return fmt.Errorf("unable to encode payments.checkedGiftCode#eb983f8f: field from_id is nil")
		}
		if err := c.FromID.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.checkedGiftCode#eb983f8f: field from_id: %w", err)
		}
	}
	if c.Flags.Has(3) {
		b.PutInt(c.GiveawayMsgID)
	}
	if c.Flags.Has(0) {
		b.PutLong(c.ToID)
	}
	b.PutInt(c.Date)
	b.PutInt(c.Days)
	if c.Flags.Has(1) {
		b.PutInt(c.UsedDate)
	}
	b.PutVectorHeader(len(c.Chats))
	for idx, v := range c.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode payments.checkedGiftCode#eb983f8f: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.checkedGiftCode#eb983f8f: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(c.Users))
	for idx, v := range c.Users {
		if v == nil {
			return fmt.Errorf("unable to encode payments.checkedGiftCode#eb983f8f: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.checkedGiftCode#eb983f8f: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *PaymentsCheckedGiftCode) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.checkedGiftCode#eb983f8f to nil")
	}
	if err := b.ConsumeID(PaymentsCheckedGiftCodeTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *PaymentsCheckedGiftCode) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.checkedGiftCode#eb983f8f to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field flags: %w", err)
		}
	}
	c.ViaGiveaway = c.Flags.Has(2)
	if c.Flags.Has(4) {
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field from_id: %w", err)
		}
		c.FromID = value
	}
	if c.Flags.Has(3) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field giveaway_msg_id: %w", err)
		}
		c.GiveawayMsgID = value
	}
	if c.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field to_id: %w", err)
		}
		c.ToID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field date: %w", err)
		}
		c.Date = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field days: %w", err)
		}
		c.Days = value
	}
	if c.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field used_date: %w", err)
		}
		c.UsedDate = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field chats: %w", err)
		}

		if headerLen > 0 {
			c.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field chats: %w", err)
			}
			c.Chats = append(c.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field users: %w", err)
		}

		if headerLen > 0 {
			c.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.checkedGiftCode#eb983f8f: field users: %w", err)
			}
			c.Users = append(c.Users, value)
		}
	}
	return nil
}

// SetViaGiveaway sets value of ViaGiveaway conditional field.
func (c *PaymentsCheckedGiftCode) SetViaGiveaway(value bool) {
	if value {
		c.Flags.Set(2)
		c.ViaGiveaway = true
	} else {
		c.Flags.Unset(2)
		c.ViaGiveaway = false
	}
}

// GetViaGiveaway returns value of ViaGiveaway conditional field.
func (c *PaymentsCheckedGiftCode) GetViaGiveaway() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(2)
}

// SetFromID sets value of FromID conditional field.
func (c *PaymentsCheckedGiftCode) SetFromID(value PeerClass) {
	c.Flags.Set(4)
	c.FromID = value
}

// GetFromID returns value of FromID conditional field and
// boolean which is true if field was set.
func (c *PaymentsCheckedGiftCode) GetFromID() (value PeerClass, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(4) {
		return value, false
	}
	return c.FromID, true
}

// SetGiveawayMsgID sets value of GiveawayMsgID conditional field.
func (c *PaymentsCheckedGiftCode) SetGiveawayMsgID(value int) {
	c.Flags.Set(3)
	c.GiveawayMsgID = value
}

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

// SetToID sets value of ToID conditional field.
func (c *PaymentsCheckedGiftCode) SetToID(value int64) {
	c.Flags.Set(0)
	c.ToID = value
}

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

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

// GetDays returns value of Days field.
func (c *PaymentsCheckedGiftCode) GetDays() (value int) {
	if c == nil {
		return
	}
	return c.Days
}

// SetUsedDate sets value of UsedDate conditional field.
func (c *PaymentsCheckedGiftCode) SetUsedDate(value int) {
	c.Flags.Set(1)
	c.UsedDate = value
}

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

// GetChats returns value of Chats field.
func (c *PaymentsCheckedGiftCode) GetChats() (value []ChatClass) {
	if c == nil {
		return
	}
	return c.Chats
}

// GetUsers returns value of Users field.
func (c *PaymentsCheckedGiftCode) GetUsers() (value []UserClass) {
	if c == nil {
		return
	}
	return c.Users
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (c *PaymentsCheckedGiftCode) MapChats() (value ChatClassArray) {
	return ChatClassArray(c.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (c *PaymentsCheckedGiftCode) MapUsers() (value UserClassArray) {
	return UserClassArray(c.Users)
}
