// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// AttachMenuBotIcon represents TL type `attachMenuBotIcon#b2a7386b`.
// Represents an attachment menu icon for bot web apps »¹
//
// Links:
//  1. https://core.telegram.org/api/bots/attach
//
// See https://core.telegram.org/constructor/attachMenuBotIcon for reference.
type AttachMenuBotIcon struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// One of the following values: note that animated icons must be played when the user
	// clicks on the button, activating the bot web app. default_static - Default attachment
	// menu icon in SVG format placeholder_static - Default placeholder for opened Web Apps
	// in SVG format ios_static - Attachment menu icon in SVG format for the official iOS app
	// ios_animated - Animated attachment menu icon in TGS format for the official iOS app
	// android_animated - Animated attachment menu icon in TGS format for the official
	// Android app macos_animated - Animated attachment menu icon in TGS format for the
	// official native Mac OS app
	Name string
	// The actual icon file.
	Icon DocumentClass
	// Attachment menu icon colors.
	//
	// Use SetColors and GetColors helpers.
	Colors []AttachMenuBotIconColor
}

// AttachMenuBotIconTypeID is TL type id of AttachMenuBotIcon.
const AttachMenuBotIconTypeID = 0xb2a7386b

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

func (a *AttachMenuBotIcon) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Flags.Zero()) {
		return false
	}
	if !(a.Name == "") {
		return false
	}
	if !(a.Icon == nil) {
		return false
	}
	if !(a.Colors == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AttachMenuBotIcon from given interface.
func (a *AttachMenuBotIcon) FillFrom(from interface {
	GetName() (value string)
	GetIcon() (value DocumentClass)
	GetColors() (value []AttachMenuBotIconColor, ok bool)
}) {
	a.Name = from.GetName()
	a.Icon = from.GetIcon()
	if val, ok := from.GetColors(); ok {
		a.Colors = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (a *AttachMenuBotIcon) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "attachMenuBotIcon",
		ID:   AttachMenuBotIconTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Icon",
			SchemaName: "icon",
		},
		{
			Name:       "Colors",
			SchemaName: "colors",
			Null:       !a.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (a *AttachMenuBotIcon) SetFlags() {
	if !(a.Colors == nil) {
		a.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (a *AttachMenuBotIcon) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode attachMenuBotIcon#b2a7386b as nil")
	}
	b.PutID(AttachMenuBotIconTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AttachMenuBotIcon) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode attachMenuBotIcon#b2a7386b as nil")
	}
	a.SetFlags()
	if err := a.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode attachMenuBotIcon#b2a7386b: field flags: %w", err)
	}
	b.PutString(a.Name)
	if a.Icon == nil {
		return fmt.Errorf("unable to encode attachMenuBotIcon#b2a7386b: field icon is nil")
	}
	if err := a.Icon.Encode(b); err != nil {
		return fmt.Errorf("unable to encode attachMenuBotIcon#b2a7386b: field icon: %w", err)
	}
	if a.Flags.Has(0) {
		b.PutVectorHeader(len(a.Colors))
		for idx, v := range a.Colors {
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode attachMenuBotIcon#b2a7386b: field colors element with index %d: %w", idx, err)
			}
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AttachMenuBotIcon) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode attachMenuBotIcon#b2a7386b to nil")
	}
	if err := b.ConsumeID(AttachMenuBotIconTypeID); err != nil {
		return fmt.Errorf("unable to decode attachMenuBotIcon#b2a7386b: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AttachMenuBotIcon) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode attachMenuBotIcon#b2a7386b to nil")
	}
	{
		if err := a.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode attachMenuBotIcon#b2a7386b: field flags: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode attachMenuBotIcon#b2a7386b: field name: %w", err)
		}
		a.Name = value
	}
	{
		value, err := DecodeDocument(b)
		if err != nil {
			return fmt.Errorf("unable to decode attachMenuBotIcon#b2a7386b: field icon: %w", err)
		}
		a.Icon = value
	}
	if a.Flags.Has(0) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode attachMenuBotIcon#b2a7386b: field colors: %w", err)
		}

		if headerLen > 0 {
			a.Colors = make([]AttachMenuBotIconColor, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AttachMenuBotIconColor
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode attachMenuBotIcon#b2a7386b: field colors: %w", err)
			}
			a.Colors = append(a.Colors, value)
		}
	}
	return nil
}

// GetName returns value of Name field.
func (a *AttachMenuBotIcon) GetName() (value string) {
	if a == nil {
		return
	}
	return a.Name
}

// GetIcon returns value of Icon field.
func (a *AttachMenuBotIcon) GetIcon() (value DocumentClass) {
	if a == nil {
		return
	}
	return a.Icon
}

// SetColors sets value of Colors conditional field.
func (a *AttachMenuBotIcon) SetColors(value []AttachMenuBotIconColor) {
	a.Flags.Set(0)
	a.Colors = value
}

// GetColors returns value of Colors conditional field and
// boolean which is true if field was set.
func (a *AttachMenuBotIcon) GetColors() (value []AttachMenuBotIconColor, ok bool) {
	if a == nil {
		return
	}
	if !a.Flags.Has(0) {
		return value, false
	}
	return a.Colors, true
}

// GetIconAsNotEmpty returns mapped value of Icon field.
func (a *AttachMenuBotIcon) GetIconAsNotEmpty() (*Document, bool) {
	return a.Icon.AsNotEmpty()
}
