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

// PeerColor represents TL type `peerColor#b54b5acf`.
// Represents a color palette »¹.
//
// Links:
//  1. https://core.telegram.org/api/colors
//
// See https://core.telegram.org/constructor/peerColor for reference.
type PeerColor struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Color palette ID, see here »¹ for more info; if not set, the default palette should
	// be used.
	//
	// Links:
	//  1) https://core.telegram.org/api/colors
	//
	// Use SetColor and GetColor helpers.
	Color int
	// Optional custom emoji ID¹ used to generate the pattern.
	//
	// Links:
	//  1) https://core.telegram.org/api/custom-emoji
	//
	// Use SetBackgroundEmojiID and GetBackgroundEmojiID helpers.
	BackgroundEmojiID int64
}

// PeerColorTypeID is TL type id of PeerColor.
const PeerColorTypeID = 0xb54b5acf

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

func (p *PeerColor) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Flags.Zero()) {
		return false
	}
	if !(p.Color == 0) {
		return false
	}
	if !(p.BackgroundEmojiID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PeerColor from given interface.
func (p *PeerColor) FillFrom(from interface {
	GetColor() (value int, ok bool)
	GetBackgroundEmojiID() (value int64, ok bool)
}) {
	if val, ok := from.GetColor(); ok {
		p.Color = val
	}

	if val, ok := from.GetBackgroundEmojiID(); ok {
		p.BackgroundEmojiID = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (p *PeerColor) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "peerColor",
		ID:   PeerColorTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Color",
			SchemaName: "color",
			Null:       !p.Flags.Has(0),
		},
		{
			Name:       "BackgroundEmojiID",
			SchemaName: "background_emoji_id",
			Null:       !p.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (p *PeerColor) SetFlags() {
	if !(p.Color == 0) {
		p.Flags.Set(0)
	}
	if !(p.BackgroundEmojiID == 0) {
		p.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (p *PeerColor) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode peerColor#b54b5acf as nil")
	}
	b.PutID(PeerColorTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PeerColor) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode peerColor#b54b5acf as nil")
	}
	p.SetFlags()
	if err := p.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode peerColor#b54b5acf: field flags: %w", err)
	}
	if p.Flags.Has(0) {
		b.PutInt(p.Color)
	}
	if p.Flags.Has(1) {
		b.PutLong(p.BackgroundEmojiID)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PeerColor) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode peerColor#b54b5acf to nil")
	}
	if err := b.ConsumeID(PeerColorTypeID); err != nil {
		return fmt.Errorf("unable to decode peerColor#b54b5acf: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PeerColor) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode peerColor#b54b5acf to nil")
	}
	{
		if err := p.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode peerColor#b54b5acf: field flags: %w", err)
		}
	}
	if p.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode peerColor#b54b5acf: field color: %w", err)
		}
		p.Color = value
	}
	if p.Flags.Has(1) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode peerColor#b54b5acf: field background_emoji_id: %w", err)
		}
		p.BackgroundEmojiID = value
	}
	return nil
}

// SetColor sets value of Color conditional field.
func (p *PeerColor) SetColor(value int) {
	p.Flags.Set(0)
	p.Color = value
}

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

// SetBackgroundEmojiID sets value of BackgroundEmojiID conditional field.
func (p *PeerColor) SetBackgroundEmojiID(value int64) {
	p.Flags.Set(1)
	p.BackgroundEmojiID = value
}

// GetBackgroundEmojiID returns value of BackgroundEmojiID conditional field and
// boolean which is true if field was set.
func (p *PeerColor) GetBackgroundEmojiID() (value int64, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(1) {
		return value, false
	}
	return p.BackgroundEmojiID, true
}
