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

// ChannelsUpdateColorRequest represents TL type `channels.updateColor#d8aa3671`.
// Update the accent color and background custom emoji »¹ of a channel.
//
// Links:
//  1. https://core.telegram.org/api/colors
//
// See https://core.telegram.org/method/channels.updateColor for reference.
type ChannelsUpdateColorRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to change the accent color emoji pattern of the profile page; otherwise, the
	// accent color and emoji pattern of messages will be changed. Channels can change both
	// message and profile palettes; supergroups can only change the profile palette, of
	// course after reaching the appropriate boost level¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/colors
	ForProfile bool
	// Channel whose accent color should be changed.
	Channel InputChannelClass
	// ID of the accent color palette »¹ to use (not RGB24, see here »² for more info);
	// if not set, the default palette is used.
	//
	// Links:
	//  1) https://core.telegram.org/api/colors
	//  2) https://core.telegram.org/api/colors
	//
	// Use SetColor and GetColor helpers.
	Color int
	// Custom emoji ID used in the accent color pattern.
	//
	// Use SetBackgroundEmojiID and GetBackgroundEmojiID helpers.
	BackgroundEmojiID int64
}

// ChannelsUpdateColorRequestTypeID is TL type id of ChannelsUpdateColorRequest.
const ChannelsUpdateColorRequestTypeID = 0xd8aa3671

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

func (u *ChannelsUpdateColorRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.ForProfile == false) {
		return false
	}
	if !(u.Channel == nil) {
		return false
	}
	if !(u.Color == 0) {
		return false
	}
	if !(u.BackgroundEmojiID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsUpdateColorRequest from given interface.
func (u *ChannelsUpdateColorRequest) FillFrom(from interface {
	GetForProfile() (value bool)
	GetChannel() (value InputChannelClass)
	GetColor() (value int, ok bool)
	GetBackgroundEmojiID() (value int64, ok bool)
}) {
	u.ForProfile = from.GetForProfile()
	u.Channel = from.GetChannel()
	if val, ok := from.GetColor(); ok {
		u.Color = val
	}

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

}

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

// TypeName returns name of type in TL schema.
func (*ChannelsUpdateColorRequest) TypeName() string {
	return "channels.updateColor"
}

// TypeInfo returns info about TL type.
func (u *ChannelsUpdateColorRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.updateColor",
		ID:   ChannelsUpdateColorRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ForProfile",
			SchemaName: "for_profile",
			Null:       !u.Flags.Has(1),
		},
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
		{
			Name:       "Color",
			SchemaName: "color",
			Null:       !u.Flags.Has(2),
		},
		{
			Name:       "BackgroundEmojiID",
			SchemaName: "background_emoji_id",
			Null:       !u.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *ChannelsUpdateColorRequest) SetFlags() {
	if !(u.ForProfile == false) {
		u.Flags.Set(1)
	}
	if !(u.Color == 0) {
		u.Flags.Set(2)
	}
	if !(u.BackgroundEmojiID == 0) {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *ChannelsUpdateColorRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode channels.updateColor#d8aa3671 as nil")
	}
	b.PutID(ChannelsUpdateColorRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *ChannelsUpdateColorRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode channels.updateColor#d8aa3671 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.updateColor#d8aa3671: field flags: %w", err)
	}
	if u.Channel == nil {
		return fmt.Errorf("unable to encode channels.updateColor#d8aa3671: field channel is nil")
	}
	if err := u.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.updateColor#d8aa3671: field channel: %w", err)
	}
	if u.Flags.Has(2) {
		b.PutInt(u.Color)
	}
	if u.Flags.Has(0) {
		b.PutLong(u.BackgroundEmojiID)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *ChannelsUpdateColorRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode channels.updateColor#d8aa3671 to nil")
	}
	if err := b.ConsumeID(ChannelsUpdateColorRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.updateColor#d8aa3671: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *ChannelsUpdateColorRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode channels.updateColor#d8aa3671 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode channels.updateColor#d8aa3671: field flags: %w", err)
		}
	}
	u.ForProfile = u.Flags.Has(1)
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.updateColor#d8aa3671: field channel: %w", err)
		}
		u.Channel = value
	}
	if u.Flags.Has(2) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.updateColor#d8aa3671: field color: %w", err)
		}
		u.Color = value
	}
	if u.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode channels.updateColor#d8aa3671: field background_emoji_id: %w", err)
		}
		u.BackgroundEmojiID = value
	}
	return nil
}

// SetForProfile sets value of ForProfile conditional field.
func (u *ChannelsUpdateColorRequest) SetForProfile(value bool) {
	if value {
		u.Flags.Set(1)
		u.ForProfile = true
	} else {
		u.Flags.Unset(1)
		u.ForProfile = false
	}
}

// GetForProfile returns value of ForProfile conditional field.
func (u *ChannelsUpdateColorRequest) GetForProfile() (value bool) {
	if u == nil {
		return
	}
	return u.Flags.Has(1)
}

// GetChannel returns value of Channel field.
func (u *ChannelsUpdateColorRequest) GetChannel() (value InputChannelClass) {
	if u == nil {
		return
	}
	return u.Channel
}

// SetColor sets value of Color conditional field.
func (u *ChannelsUpdateColorRequest) SetColor(value int) {
	u.Flags.Set(2)
	u.Color = value
}

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

// SetBackgroundEmojiID sets value of BackgroundEmojiID conditional field.
func (u *ChannelsUpdateColorRequest) SetBackgroundEmojiID(value int64) {
	u.Flags.Set(0)
	u.BackgroundEmojiID = value
}

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

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (u *ChannelsUpdateColorRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return u.Channel.AsNotEmpty()
}

// ChannelsUpdateColor invokes method channels.updateColor#d8aa3671 returning error if any.
// Update the accent color and background custom emoji »¹ of a channel.
//
// Links:
//  1. https://core.telegram.org/api/colors
//
// Possible errors:
//
//	400 BOOSTS_REQUIRED: The specified channel must first be boosted by its users in order to perform this action.
//	400 CHANNEL_INVALID: The provided channel is invalid.
//
// See https://core.telegram.org/method/channels.updateColor for reference.
func (c *Client) ChannelsUpdateColor(ctx context.Context, request *ChannelsUpdateColorRequest) (UpdatesClass, error) {
	var result UpdatesBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return result.Updates, nil
}
