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

// AccountUpdateColorRequest represents TL type `account.updateColor#7cefa15d`.
// Update the accent color and background custom emoji »¹ of the current account.
//
// Links:
//  1. https://core.telegram.org/api/colors
//
// See https://core.telegram.org/method/account.updateColor for reference.
type AccountUpdateColorRequest 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.
	ForProfile bool
	// ID of the accent color palette »¹ to use (not RGB24, see here »² for more info).
	//
	// 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
}

// AccountUpdateColorRequestTypeID is TL type id of AccountUpdateColorRequest.
const AccountUpdateColorRequestTypeID = 0x7cefa15d

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

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

	return true
}

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

// FillFrom fills AccountUpdateColorRequest from given interface.
func (u *AccountUpdateColorRequest) FillFrom(from interface {
	GetForProfile() (value bool)
	GetColor() (value int, ok bool)
	GetBackgroundEmojiID() (value int64, ok bool)
}) {
	u.ForProfile = from.GetForProfile()
	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 (*AccountUpdateColorRequest) TypeID() uint32 {
	return AccountUpdateColorRequestTypeID
}

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

// TypeInfo returns info about TL type.
func (u *AccountUpdateColorRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.updateColor",
		ID:   AccountUpdateColorRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ForProfile",
			SchemaName: "for_profile",
			Null:       !u.Flags.Has(1),
		},
		{
			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 *AccountUpdateColorRequest) 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 *AccountUpdateColorRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateColor#7cefa15d as nil")
	}
	b.PutID(AccountUpdateColorRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *AccountUpdateColorRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateColor#7cefa15d as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.updateColor#7cefa15d: field flags: %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 *AccountUpdateColorRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateColor#7cefa15d to nil")
	}
	if err := b.ConsumeID(AccountUpdateColorRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.updateColor#7cefa15d: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *AccountUpdateColorRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateColor#7cefa15d to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateColor#7cefa15d: field flags: %w", err)
		}
	}
	u.ForProfile = u.Flags.Has(1)
	if u.Flags.Has(2) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode account.updateColor#7cefa15d: 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 account.updateColor#7cefa15d: field background_emoji_id: %w", err)
		}
		u.BackgroundEmojiID = value
	}
	return nil
}

// SetForProfile sets value of ForProfile conditional field.
func (u *AccountUpdateColorRequest) 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 *AccountUpdateColorRequest) GetForProfile() (value bool) {
	if u == nil {
		return
	}
	return u.Flags.Has(1)
}

// SetColor sets value of Color conditional field.
func (u *AccountUpdateColorRequest) 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 *AccountUpdateColorRequest) 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 *AccountUpdateColorRequest) 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 *AccountUpdateColorRequest) GetBackgroundEmojiID() (value int64, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(0) {
		return value, false
	}
	return u.BackgroundEmojiID, true
}

// AccountUpdateColor invokes method account.updateColor#7cefa15d returning error if any.
// Update the accent color and background custom emoji »¹ of the current account.
//
// Links:
//  1. https://core.telegram.org/api/colors
//
// Possible errors:
//
//	400 COLOR_INVALID: The specified color palette ID was invalid.
//	403 PREMIUM_ACCOUNT_REQUIRED: A premium account is required to execute this action.
//
// See https://core.telegram.org/method/account.updateColor for reference.
func (c *Client) AccountUpdateColor(ctx context.Context, request *AccountUpdateColorRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
