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

// ProfileAccentColors represents TL type `profileAccentColors#218e9367`.
type ProfileAccentColors struct {
	// The list of 1-2 colors in RGB format, describing the colors, as expected to be shown
	// in the color palette settings
	PaletteColors []int32
	// The list of 1-2 colors in RGB format, describing the colors, as expected to be used
	// for the profile photo background
	BackgroundColors []int32
	// The list of 2 colors in RGB format, describing the colors of the gradient to be used
	// for the unread active story indicator around profile photo
	StoryColors []int32
}

// ProfileAccentColorsTypeID is TL type id of ProfileAccentColors.
const ProfileAccentColorsTypeID = 0x218e9367

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

func (p *ProfileAccentColors) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.PaletteColors == nil) {
		return false
	}
	if !(p.BackgroundColors == nil) {
		return false
	}
	if !(p.StoryColors == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *ProfileAccentColors) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "profileAccentColors",
		ID:   ProfileAccentColorsTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PaletteColors",
			SchemaName: "palette_colors",
		},
		{
			Name:       "BackgroundColors",
			SchemaName: "background_colors",
		},
		{
			Name:       "StoryColors",
			SchemaName: "story_colors",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *ProfileAccentColors) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode profileAccentColors#218e9367 as nil")
	}
	b.PutID(ProfileAccentColorsTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *ProfileAccentColors) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode profileAccentColors#218e9367 as nil")
	}
	b.PutInt(len(p.PaletteColors))
	for _, v := range p.PaletteColors {
		b.PutInt32(v)
	}
	b.PutInt(len(p.BackgroundColors))
	for _, v := range p.BackgroundColors {
		b.PutInt32(v)
	}
	b.PutInt(len(p.StoryColors))
	for _, v := range p.StoryColors {
		b.PutInt32(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *ProfileAccentColors) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode profileAccentColors#218e9367 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode profileAccentColors#218e9367: field palette_colors: %w", err)
		}

		if headerLen > 0 {
			p.PaletteColors = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode profileAccentColors#218e9367: field palette_colors: %w", err)
			}
			p.PaletteColors = append(p.PaletteColors, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode profileAccentColors#218e9367: field background_colors: %w", err)
		}

		if headerLen > 0 {
			p.BackgroundColors = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode profileAccentColors#218e9367: field background_colors: %w", err)
			}
			p.BackgroundColors = append(p.BackgroundColors, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode profileAccentColors#218e9367: field story_colors: %w", err)
		}

		if headerLen > 0 {
			p.StoryColors = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode profileAccentColors#218e9367: field story_colors: %w", err)
			}
			p.StoryColors = append(p.StoryColors, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *ProfileAccentColors) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode profileAccentColors#218e9367 as nil")
	}
	b.ObjStart()
	b.PutID("profileAccentColors")
	b.Comma()
	b.FieldStart("palette_colors")
	b.ArrStart()
	for _, v := range p.PaletteColors {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("background_colors")
	b.ArrStart()
	for _, v := range p.BackgroundColors {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("story_colors")
	b.ArrStart()
	for _, v := range p.StoryColors {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *ProfileAccentColors) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode profileAccentColors#218e9367 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("profileAccentColors"); err != nil {
				return fmt.Errorf("unable to decode profileAccentColors#218e9367: %w", err)
			}
		case "palette_colors":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode profileAccentColors#218e9367: field palette_colors: %w", err)
				}
				p.PaletteColors = append(p.PaletteColors, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode profileAccentColors#218e9367: field palette_colors: %w", err)
			}
		case "background_colors":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode profileAccentColors#218e9367: field background_colors: %w", err)
				}
				p.BackgroundColors = append(p.BackgroundColors, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode profileAccentColors#218e9367: field background_colors: %w", err)
			}
		case "story_colors":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode profileAccentColors#218e9367: field story_colors: %w", err)
				}
				p.StoryColors = append(p.StoryColors, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode profileAccentColors#218e9367: field story_colors: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPaletteColors returns value of PaletteColors field.
func (p *ProfileAccentColors) GetPaletteColors() (value []int32) {
	if p == nil {
		return
	}
	return p.PaletteColors
}

// GetBackgroundColors returns value of BackgroundColors field.
func (p *ProfileAccentColors) GetBackgroundColors() (value []int32) {
	if p == nil {
		return
	}
	return p.BackgroundColors
}

// GetStoryColors returns value of StoryColors field.
func (p *ProfileAccentColors) GetStoryColors() (value []int32) {
	if p == nil {
		return
	}
	return p.StoryColors
}
