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

// MessagesComposeMessageWithAIRequest represents TL type `messages.composeMessageWithAI#fd426afe`.
//
// See https://core.telegram.org/method/messages.composeMessageWithAI for reference.
type MessagesComposeMessageWithAIRequest struct {
	// Flags field of MessagesComposeMessageWithAIRequest.
	Flags bin.Fields
	// Proofread field of MessagesComposeMessageWithAIRequest.
	Proofread bool
	// Emojify field of MessagesComposeMessageWithAIRequest.
	Emojify bool
	// Text field of MessagesComposeMessageWithAIRequest.
	Text TextWithEntities
	// TranslateToLang field of MessagesComposeMessageWithAIRequest.
	//
	// Use SetTranslateToLang and GetTranslateToLang helpers.
	TranslateToLang string
	// ChangeTone field of MessagesComposeMessageWithAIRequest.
	//
	// Use SetChangeTone and GetChangeTone helpers.
	ChangeTone string
}

// MessagesComposeMessageWithAIRequestTypeID is TL type id of MessagesComposeMessageWithAIRequest.
const MessagesComposeMessageWithAIRequestTypeID = 0xfd426afe

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

func (c *MessagesComposeMessageWithAIRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Proofread == false) {
		return false
	}
	if !(c.Emojify == false) {
		return false
	}
	if !(c.Text.Zero()) {
		return false
	}
	if !(c.TranslateToLang == "") {
		return false
	}
	if !(c.ChangeTone == "") {
		return false
	}

	return true
}

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

// FillFrom fills MessagesComposeMessageWithAIRequest from given interface.
func (c *MessagesComposeMessageWithAIRequest) FillFrom(from interface {
	GetProofread() (value bool)
	GetEmojify() (value bool)
	GetText() (value TextWithEntities)
	GetTranslateToLang() (value string, ok bool)
	GetChangeTone() (value string, ok bool)
}) {
	c.Proofread = from.GetProofread()
	c.Emojify = from.GetEmojify()
	c.Text = from.GetText()
	if val, ok := from.GetTranslateToLang(); ok {
		c.TranslateToLang = val
	}

	if val, ok := from.GetChangeTone(); ok {
		c.ChangeTone = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*MessagesComposeMessageWithAIRequest) TypeName() string {
	return "messages.composeMessageWithAI"
}

// TypeInfo returns info about TL type.
func (c *MessagesComposeMessageWithAIRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.composeMessageWithAI",
		ID:   MessagesComposeMessageWithAIRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Proofread",
			SchemaName: "proofread",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "Emojify",
			SchemaName: "emojify",
			Null:       !c.Flags.Has(3),
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "TranslateToLang",
			SchemaName: "translate_to_lang",
			Null:       !c.Flags.Has(1),
		},
		{
			Name:       "ChangeTone",
			SchemaName: "change_tone",
			Null:       !c.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *MessagesComposeMessageWithAIRequest) SetFlags() {
	if !(c.Proofread == false) {
		c.Flags.Set(0)
	}
	if !(c.Emojify == false) {
		c.Flags.Set(3)
	}
	if !(c.TranslateToLang == "") {
		c.Flags.Set(1)
	}
	if !(c.ChangeTone == "") {
		c.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (c *MessagesComposeMessageWithAIRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.composeMessageWithAI#fd426afe as nil")
	}
	b.PutID(MessagesComposeMessageWithAIRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *MessagesComposeMessageWithAIRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.composeMessageWithAI#fd426afe as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.composeMessageWithAI#fd426afe: field flags: %w", err)
	}
	if err := c.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.composeMessageWithAI#fd426afe: field text: %w", err)
	}
	if c.Flags.Has(1) {
		b.PutString(c.TranslateToLang)
	}
	if c.Flags.Has(2) {
		b.PutString(c.ChangeTone)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *MessagesComposeMessageWithAIRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode messages.composeMessageWithAI#fd426afe to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.composeMessageWithAI#fd426afe: field flags: %w", err)
		}
	}
	c.Proofread = c.Flags.Has(0)
	c.Emojify = c.Flags.Has(3)
	{
		if err := c.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.composeMessageWithAI#fd426afe: field text: %w", err)
		}
	}
	if c.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.composeMessageWithAI#fd426afe: field translate_to_lang: %w", err)
		}
		c.TranslateToLang = value
	}
	if c.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.composeMessageWithAI#fd426afe: field change_tone: %w", err)
		}
		c.ChangeTone = value
	}
	return nil
}

// SetProofread sets value of Proofread conditional field.
func (c *MessagesComposeMessageWithAIRequest) SetProofread(value bool) {
	if value {
		c.Flags.Set(0)
		c.Proofread = true
	} else {
		c.Flags.Unset(0)
		c.Proofread = false
	}
}

// GetProofread returns value of Proofread conditional field.
func (c *MessagesComposeMessageWithAIRequest) GetProofread() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(0)
}

// SetEmojify sets value of Emojify conditional field.
func (c *MessagesComposeMessageWithAIRequest) SetEmojify(value bool) {
	if value {
		c.Flags.Set(3)
		c.Emojify = true
	} else {
		c.Flags.Unset(3)
		c.Emojify = false
	}
}

// GetEmojify returns value of Emojify conditional field.
func (c *MessagesComposeMessageWithAIRequest) GetEmojify() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(3)
}

// GetText returns value of Text field.
func (c *MessagesComposeMessageWithAIRequest) GetText() (value TextWithEntities) {
	if c == nil {
		return
	}
	return c.Text
}

// SetTranslateToLang sets value of TranslateToLang conditional field.
func (c *MessagesComposeMessageWithAIRequest) SetTranslateToLang(value string) {
	c.Flags.Set(1)
	c.TranslateToLang = value
}

// GetTranslateToLang returns value of TranslateToLang conditional field and
// boolean which is true if field was set.
func (c *MessagesComposeMessageWithAIRequest) GetTranslateToLang() (value string, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(1) {
		return value, false
	}
	return c.TranslateToLang, true
}

// SetChangeTone sets value of ChangeTone conditional field.
func (c *MessagesComposeMessageWithAIRequest) SetChangeTone(value string) {
	c.Flags.Set(2)
	c.ChangeTone = value
}

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

// MessagesComposeMessageWithAI invokes method messages.composeMessageWithAI#fd426afe returning error if any.
//
// See https://core.telegram.org/method/messages.composeMessageWithAI for reference.
func (c *Client) MessagesComposeMessageWithAI(ctx context.Context, request *MessagesComposeMessageWithAIRequest) (*MessagesComposedMessageWithAI, error) {
	var result MessagesComposedMessageWithAI

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