// 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#684d214e`.
// 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 PeerColorClass
}

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

// 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 == nil) {
		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 PeerColorClass, ok bool)
}) {
	u.ForProfile = from.GetForProfile()
	if val, ok := from.GetColor(); ok {
		u.Color = 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),
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (u *AccountUpdateColorRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateColor#684d214e 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#684d214e as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.updateColor#684d214e: field flags: %w", err)
	}
	if u.Flags.Has(2) {
		if u.Color == nil {
			return fmt.Errorf("unable to encode account.updateColor#684d214e: field color is nil")
		}
		if err := u.Color.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.updateColor#684d214e: field color: %w", err)
		}
	}
	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#684d214e to nil")
	}
	if err := b.ConsumeID(AccountUpdateColorRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.updateColor#684d214e: %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#684d214e to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateColor#684d214e: field flags: %w", err)
		}
	}
	u.ForProfile = u.Flags.Has(1)
	if u.Flags.Has(2) {
		value, err := DecodePeerColor(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.updateColor#684d214e: field color: %w", err)
		}
		u.Color = 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 PeerColorClass) {
	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 PeerColorClass, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(2) {
		return value, false
	}
	return u.Color, true
}

// AccountUpdateColor invokes method account.updateColor#684d214e 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.
//	400 DOCUMENT_INVALID: The specified document is 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
}
