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

// MessagesComposedMessageWithAI represents TL type `messages.composedMessageWithAI#90d7adfa`.
//
// See https://core.telegram.org/constructor/messages.composedMessageWithAI for reference.
type MessagesComposedMessageWithAI struct {
	// Flags field of MessagesComposedMessageWithAI.
	Flags bin.Fields
	// ResultText field of MessagesComposedMessageWithAI.
	ResultText TextWithEntities
	// DiffText field of MessagesComposedMessageWithAI.
	//
	// Use SetDiffText and GetDiffText helpers.
	DiffText TextWithEntities
}

// MessagesComposedMessageWithAITypeID is TL type id of MessagesComposedMessageWithAI.
const MessagesComposedMessageWithAITypeID = 0x90d7adfa

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

func (c *MessagesComposedMessageWithAI) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.ResultText.Zero()) {
		return false
	}
	if !(c.DiffText.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesComposedMessageWithAI from given interface.
func (c *MessagesComposedMessageWithAI) FillFrom(from interface {
	GetResultText() (value TextWithEntities)
	GetDiffText() (value TextWithEntities, ok bool)
}) {
	c.ResultText = from.GetResultText()
	if val, ok := from.GetDiffText(); ok {
		c.DiffText = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (c *MessagesComposedMessageWithAI) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.composedMessageWithAI",
		ID:   MessagesComposedMessageWithAITypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ResultText",
			SchemaName: "result_text",
		},
		{
			Name:       "DiffText",
			SchemaName: "diff_text",
			Null:       !c.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *MessagesComposedMessageWithAI) SetFlags() {
	if !(c.DiffText.Zero()) {
		c.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (c *MessagesComposedMessageWithAI) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.composedMessageWithAI#90d7adfa as nil")
	}
	b.PutID(MessagesComposedMessageWithAITypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *MessagesComposedMessageWithAI) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.composedMessageWithAI#90d7adfa as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.composedMessageWithAI#90d7adfa: field flags: %w", err)
	}
	if err := c.ResultText.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.composedMessageWithAI#90d7adfa: field result_text: %w", err)
	}
	if c.Flags.Has(0) {
		if err := c.DiffText.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.composedMessageWithAI#90d7adfa: field diff_text: %w", err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *MessagesComposedMessageWithAI) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode messages.composedMessageWithAI#90d7adfa to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.composedMessageWithAI#90d7adfa: field flags: %w", err)
		}
	}
	{
		if err := c.ResultText.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.composedMessageWithAI#90d7adfa: field result_text: %w", err)
		}
	}
	if c.Flags.Has(0) {
		if err := c.DiffText.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.composedMessageWithAI#90d7adfa: field diff_text: %w", err)
		}
	}
	return nil
}

// GetResultText returns value of ResultText field.
func (c *MessagesComposedMessageWithAI) GetResultText() (value TextWithEntities) {
	if c == nil {
		return
	}
	return c.ResultText
}

// SetDiffText sets value of DiffText conditional field.
func (c *MessagesComposedMessageWithAI) SetDiffText(value TextWithEntities) {
	c.Flags.Set(0)
	c.DiffText = value
}

// GetDiffText returns value of DiffText conditional field and
// boolean which is true if field was set.
func (c *MessagesComposedMessageWithAI) GetDiffText() (value TextWithEntities, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.DiffText, true
}
