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

// ChatPhotoSticker represents TL type `chatPhotoSticker#a90383a3`.
type ChatPhotoSticker struct {
	// Type of the sticker
	Type ChatPhotoStickerTypeClass
	// The fill to be used as background for the sticker; rotation angle in
	// backgroundFillGradient isn't supported
	BackgroundFill BackgroundFillClass
}

// ChatPhotoStickerTypeID is TL type id of ChatPhotoSticker.
const ChatPhotoStickerTypeID = 0xa90383a3

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

func (c *ChatPhotoSticker) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Type == nil) {
		return false
	}
	if !(c.BackgroundFill == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatPhotoSticker) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatPhotoSticker",
		ID:   ChatPhotoStickerTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "BackgroundFill",
			SchemaName: "background_fill",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatPhotoSticker) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhotoSticker#a90383a3 as nil")
	}
	b.PutID(ChatPhotoStickerTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatPhotoSticker) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhotoSticker#a90383a3 as nil")
	}
	if c.Type == nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field type is nil")
	}
	if err := c.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field type: %w", err)
	}
	if c.BackgroundFill == nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field background_fill is nil")
	}
	if err := c.BackgroundFill.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field background_fill: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatPhotoSticker) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhotoSticker#a90383a3 to nil")
	}
	{
		value, err := DecodeChatPhotoStickerType(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatPhotoSticker#a90383a3: field type: %w", err)
		}
		c.Type = value
	}
	{
		value, err := DecodeBackgroundFill(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatPhotoSticker#a90383a3: field background_fill: %w", err)
		}
		c.BackgroundFill = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatPhotoSticker) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhotoSticker#a90383a3 as nil")
	}
	b.ObjStart()
	b.PutID("chatPhotoSticker")
	b.Comma()
	b.FieldStart("type")
	if c.Type == nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field type is nil")
	}
	if err := c.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("background_fill")
	if c.BackgroundFill == nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field background_fill is nil")
	}
	if err := c.BackgroundFill.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatPhotoSticker#a90383a3: field background_fill: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatPhotoSticker) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhotoSticker#a90383a3 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatPhotoSticker"); err != nil {
				return fmt.Errorf("unable to decode chatPhotoSticker#a90383a3: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONChatPhotoStickerType(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatPhotoSticker#a90383a3: field type: %w", err)
			}
			c.Type = value
		case "background_fill":
			value, err := DecodeTDLibJSONBackgroundFill(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatPhotoSticker#a90383a3: field background_fill: %w", err)
			}
			c.BackgroundFill = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (c *ChatPhotoSticker) GetType() (value ChatPhotoStickerTypeClass) {
	if c == nil {
		return
	}
	return c.Type
}

// GetBackgroundFill returns value of BackgroundFill field.
func (c *ChatPhotoSticker) GetBackgroundFill() (value BackgroundFillClass) {
	if c == nil {
		return
	}
	return c.BackgroundFill
}
