// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// SetMessageFactCheckRequest represents TL type `setMessageFactCheck#ffbe3d08`.
type SetMessageFactCheckRequest struct {
	// The channel chat the message belongs to
	ChatID int64
	// Identifier of the message
	MessageID int64
	// New text of the fact-check; 0-getOption("fact_check_length_max") characters; pass null
	// to remove it. Only Bold, Italic, and TextUrl entities with https://t.me/ links are
	// supported
	Text FormattedText
}

// SetMessageFactCheckRequestTypeID is TL type id of SetMessageFactCheckRequest.
const SetMessageFactCheckRequestTypeID = 0xffbe3d08

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

func (s *SetMessageFactCheckRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.MessageID == 0) {
		return false
	}
	if !(s.Text.Zero()) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*SetMessageFactCheckRequest) TypeName() string {
	return "setMessageFactCheck"
}

// TypeInfo returns info about TL type.
func (s *SetMessageFactCheckRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setMessageFactCheck",
		ID:   SetMessageFactCheckRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetMessageFactCheckRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setMessageFactCheck#ffbe3d08 as nil")
	}
	b.PutID(SetMessageFactCheckRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetMessageFactCheckRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setMessageFactCheck#ffbe3d08 as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutInt53(s.MessageID)
	if err := s.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setMessageFactCheck#ffbe3d08: field text: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetMessageFactCheckRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setMessageFactCheck#ffbe3d08 to nil")
	}
	if err := b.ConsumeID(SetMessageFactCheckRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetMessageFactCheckRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setMessageFactCheck#ffbe3d08 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: field message_id: %w", err)
		}
		s.MessageID = value
	}
	{
		if err := s.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: field text: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetMessageFactCheckRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setMessageFactCheck#ffbe3d08 as nil")
	}
	b.ObjStart()
	b.PutID("setMessageFactCheck")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(s.MessageID)
	b.Comma()
	b.FieldStart("text")
	if err := s.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setMessageFactCheck#ffbe3d08: field text: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetMessageFactCheckRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setMessageFactCheck#ffbe3d08 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setMessageFactCheck"); err != nil {
				return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: field chat_id: %w", err)
			}
			s.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: field message_id: %w", err)
			}
			s.MessageID = value
		case "text":
			if err := s.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setMessageFactCheck#ffbe3d08: field text: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (s *SetMessageFactCheckRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetMessageID returns value of MessageID field.
func (s *SetMessageFactCheckRequest) GetMessageID() (value int64) {
	if s == nil {
		return
	}
	return s.MessageID
}

// GetText returns value of Text field.
func (s *SetMessageFactCheckRequest) GetText() (value FormattedText) {
	if s == nil {
		return
	}
	return s.Text
}

// SetMessageFactCheck invokes method setMessageFactCheck#ffbe3d08 returning error if any.
func (c *Client) SetMessageFactCheck(ctx context.Context, request *SetMessageFactCheckRequest) error {
	var ok Ok

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