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

// AicomposeUpdateToneRequest represents TL type `aicompose.updateTone#903bcf59`.
//
// See https://core.telegram.org/method/aicompose.updateTone for reference.
type AicomposeUpdateToneRequest struct {
	// Flags field of AicomposeUpdateToneRequest.
	Flags bin.Fields
	// Tone field of AicomposeUpdateToneRequest.
	Tone InputAiComposeToneClass
	// DisplayAuthor field of AicomposeUpdateToneRequest.
	//
	// Use SetDisplayAuthor and GetDisplayAuthor helpers.
	DisplayAuthor bool
	// EmojiID field of AicomposeUpdateToneRequest.
	//
	// Use SetEmojiID and GetEmojiID helpers.
	EmojiID int64
	// Title field of AicomposeUpdateToneRequest.
	//
	// Use SetTitle and GetTitle helpers.
	Title string
	// Prompt field of AicomposeUpdateToneRequest.
	//
	// Use SetPrompt and GetPrompt helpers.
	Prompt string
}

// AicomposeUpdateToneRequestTypeID is TL type id of AicomposeUpdateToneRequest.
const AicomposeUpdateToneRequestTypeID = 0x903bcf59

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

func (u *AicomposeUpdateToneRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.Tone == nil) {
		return false
	}
	if !(u.DisplayAuthor == false) {
		return false
	}
	if !(u.EmojiID == 0) {
		return false
	}
	if !(u.Title == "") {
		return false
	}
	if !(u.Prompt == "") {
		return false
	}

	return true
}

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

// FillFrom fills AicomposeUpdateToneRequest from given interface.
func (u *AicomposeUpdateToneRequest) FillFrom(from interface {
	GetTone() (value InputAiComposeToneClass)
	GetDisplayAuthor() (value bool, ok bool)
	GetEmojiID() (value int64, ok bool)
	GetTitle() (value string, ok bool)
	GetPrompt() (value string, ok bool)
}) {
	u.Tone = from.GetTone()
	if val, ok := from.GetDisplayAuthor(); ok {
		u.DisplayAuthor = val
	}

	if val, ok := from.GetEmojiID(); ok {
		u.EmojiID = val
	}

	if val, ok := from.GetTitle(); ok {
		u.Title = val
	}

	if val, ok := from.GetPrompt(); ok {
		u.Prompt = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*AicomposeUpdateToneRequest) TypeName() string {
	return "aicompose.updateTone"
}

// TypeInfo returns info about TL type.
func (u *AicomposeUpdateToneRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "aicompose.updateTone",
		ID:   AicomposeUpdateToneRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Tone",
			SchemaName: "tone",
		},
		{
			Name:       "DisplayAuthor",
			SchemaName: "display_author",
			Null:       !u.Flags.Has(0),
		},
		{
			Name:       "EmojiID",
			SchemaName: "emoji_id",
			Null:       !u.Flags.Has(1),
		},
		{
			Name:       "Title",
			SchemaName: "title",
			Null:       !u.Flags.Has(2),
		},
		{
			Name:       "Prompt",
			SchemaName: "prompt",
			Null:       !u.Flags.Has(3),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *AicomposeUpdateToneRequest) SetFlags() {
	if !(u.DisplayAuthor == false) {
		u.Flags.Set(0)
	}
	if !(u.EmojiID == 0) {
		u.Flags.Set(1)
	}
	if !(u.Title == "") {
		u.Flags.Set(2)
	}
	if !(u.Prompt == "") {
		u.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (u *AicomposeUpdateToneRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode aicompose.updateTone#903bcf59 as nil")
	}
	b.PutID(AicomposeUpdateToneRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *AicomposeUpdateToneRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode aicompose.updateTone#903bcf59 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode aicompose.updateTone#903bcf59: field flags: %w", err)
	}
	if u.Tone == nil {
		return fmt.Errorf("unable to encode aicompose.updateTone#903bcf59: field tone is nil")
	}
	if err := u.Tone.Encode(b); err != nil {
		return fmt.Errorf("unable to encode aicompose.updateTone#903bcf59: field tone: %w", err)
	}
	if u.Flags.Has(0) {
		b.PutBool(u.DisplayAuthor)
	}
	if u.Flags.Has(1) {
		b.PutLong(u.EmojiID)
	}
	if u.Flags.Has(2) {
		b.PutString(u.Title)
	}
	if u.Flags.Has(3) {
		b.PutString(u.Prompt)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *AicomposeUpdateToneRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode aicompose.updateTone#903bcf59 to nil")
	}
	if err := b.ConsumeID(AicomposeUpdateToneRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode aicompose.updateTone#903bcf59: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *AicomposeUpdateToneRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode aicompose.updateTone#903bcf59 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode aicompose.updateTone#903bcf59: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputAiComposeTone(b)
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.updateTone#903bcf59: field tone: %w", err)
		}
		u.Tone = value
	}
	if u.Flags.Has(0) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.updateTone#903bcf59: field display_author: %w", err)
		}
		u.DisplayAuthor = value
	}
	if u.Flags.Has(1) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.updateTone#903bcf59: field emoji_id: %w", err)
		}
		u.EmojiID = value
	}
	if u.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.updateTone#903bcf59: field title: %w", err)
		}
		u.Title = value
	}
	if u.Flags.Has(3) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.updateTone#903bcf59: field prompt: %w", err)
		}
		u.Prompt = value
	}
	return nil
}

// GetTone returns value of Tone field.
func (u *AicomposeUpdateToneRequest) GetTone() (value InputAiComposeToneClass) {
	if u == nil {
		return
	}
	return u.Tone
}

// SetDisplayAuthor sets value of DisplayAuthor conditional field.
func (u *AicomposeUpdateToneRequest) SetDisplayAuthor(value bool) {
	u.Flags.Set(0)
	u.DisplayAuthor = value
}

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

// SetEmojiID sets value of EmojiID conditional field.
func (u *AicomposeUpdateToneRequest) SetEmojiID(value int64) {
	u.Flags.Set(1)
	u.EmojiID = value
}

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

// SetTitle sets value of Title conditional field.
func (u *AicomposeUpdateToneRequest) SetTitle(value string) {
	u.Flags.Set(2)
	u.Title = value
}

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

// SetPrompt sets value of Prompt conditional field.
func (u *AicomposeUpdateToneRequest) SetPrompt(value string) {
	u.Flags.Set(3)
	u.Prompt = value
}

// GetPrompt returns value of Prompt conditional field and
// boolean which is true if field was set.
func (u *AicomposeUpdateToneRequest) GetPrompt() (value string, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(3) {
		return value, false
	}
	return u.Prompt, true
}

// AicomposeUpdateTone invokes method aicompose.updateTone#903bcf59 returning error if any.
//
// See https://core.telegram.org/method/aicompose.updateTone for reference.
func (c *Client) AicomposeUpdateTone(ctx context.Context, request *AicomposeUpdateToneRequest) (AiComposeToneClass, error) {
	var result AiComposeToneBox

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