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

// AddLocalMessageRequest represents TL type `addLocalMessage#f617b7a1`.
type AddLocalMessageRequest struct {
	// Target chat; channel direct messages chats aren't supported
	ChatID int64
	// Identifier of the sender of the message
	SenderID MessageSenderClass
	// Information about the message or story to be replied; pass null if none
	ReplyTo InputMessageReplyToClass
	// Pass true to disable notification for the message
	DisableNotification bool
	// The content of the message to be added
	InputMessageContent InputMessageContentClass
}

// AddLocalMessageRequestTypeID is TL type id of AddLocalMessageRequest.
const AddLocalMessageRequestTypeID = 0xf617b7a1

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

func (a *AddLocalMessageRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.ChatID == 0) {
		return false
	}
	if !(a.SenderID == nil) {
		return false
	}
	if !(a.ReplyTo == nil) {
		return false
	}
	if !(a.DisableNotification == false) {
		return false
	}
	if !(a.InputMessageContent == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AddLocalMessageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "addLocalMessage",
		ID:   AddLocalMessageRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
		{
			Name:       "ReplyTo",
			SchemaName: "reply_to",
		},
		{
			Name:       "DisableNotification",
			SchemaName: "disable_notification",
		},
		{
			Name:       "InputMessageContent",
			SchemaName: "input_message_content",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AddLocalMessageRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addLocalMessage#f617b7a1 as nil")
	}
	b.PutID(AddLocalMessageRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AddLocalMessageRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addLocalMessage#f617b7a1 as nil")
	}
	b.PutInt53(a.ChatID)
	if a.SenderID == nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field sender_id is nil")
	}
	if err := a.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field sender_id: %w", err)
	}
	if a.ReplyTo == nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field reply_to is nil")
	}
	if err := a.ReplyTo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field reply_to: %w", err)
	}
	b.PutBool(a.DisableNotification)
	if a.InputMessageContent == nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field input_message_content is nil")
	}
	if err := a.InputMessageContent.Encode(b); err != nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field input_message_content: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AddLocalMessageRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addLocalMessage#f617b7a1 to nil")
	}
	if err := b.ConsumeID(AddLocalMessageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AddLocalMessageRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addLocalMessage#f617b7a1 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field chat_id: %w", err)
		}
		a.ChatID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field sender_id: %w", err)
		}
		a.SenderID = value
	}
	{
		value, err := DecodeInputMessageReplyTo(b)
		if err != nil {
			return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field reply_to: %w", err)
		}
		a.ReplyTo = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field disable_notification: %w", err)
		}
		a.DisableNotification = value
	}
	{
		value, err := DecodeInputMessageContent(b)
		if err != nil {
			return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field input_message_content: %w", err)
		}
		a.InputMessageContent = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AddLocalMessageRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode addLocalMessage#f617b7a1 as nil")
	}
	b.ObjStart()
	b.PutID("addLocalMessage")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(a.ChatID)
	b.Comma()
	b.FieldStart("sender_id")
	if a.SenderID == nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field sender_id is nil")
	}
	if err := a.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field sender_id: %w", err)
	}
	b.Comma()
	b.FieldStart("reply_to")
	if a.ReplyTo == nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field reply_to is nil")
	}
	if err := a.ReplyTo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field reply_to: %w", err)
	}
	b.Comma()
	b.FieldStart("disable_notification")
	b.PutBool(a.DisableNotification)
	b.Comma()
	b.FieldStart("input_message_content")
	if a.InputMessageContent == nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field input_message_content is nil")
	}
	if err := a.InputMessageContent.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode addLocalMessage#f617b7a1: field input_message_content: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AddLocalMessageRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode addLocalMessage#f617b7a1 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("addLocalMessage"); err != nil {
				return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field chat_id: %w", err)
			}
			a.ChatID = value
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field sender_id: %w", err)
			}
			a.SenderID = value
		case "reply_to":
			value, err := DecodeTDLibJSONInputMessageReplyTo(b)
			if err != nil {
				return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field reply_to: %w", err)
			}
			a.ReplyTo = value
		case "disable_notification":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field disable_notification: %w", err)
			}
			a.DisableNotification = value
		case "input_message_content":
			value, err := DecodeTDLibJSONInputMessageContent(b)
			if err != nil {
				return fmt.Errorf("unable to decode addLocalMessage#f617b7a1: field input_message_content: %w", err)
			}
			a.InputMessageContent = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetSenderID returns value of SenderID field.
func (a *AddLocalMessageRequest) GetSenderID() (value MessageSenderClass) {
	if a == nil {
		return
	}
	return a.SenderID
}

// GetReplyTo returns value of ReplyTo field.
func (a *AddLocalMessageRequest) GetReplyTo() (value InputMessageReplyToClass) {
	if a == nil {
		return
	}
	return a.ReplyTo
}

// GetDisableNotification returns value of DisableNotification field.
func (a *AddLocalMessageRequest) GetDisableNotification() (value bool) {
	if a == nil {
		return
	}
	return a.DisableNotification
}

// GetInputMessageContent returns value of InputMessageContent field.
func (a *AddLocalMessageRequest) GetInputMessageContent() (value InputMessageContentClass) {
	if a == nil {
		return
	}
	return a.InputMessageContent
}

// AddLocalMessage invokes method addLocalMessage#f617b7a1 returning error if any.
func (c *Client) AddLocalMessage(ctx context.Context, request *AddLocalMessageRequest) (*Message, error) {
	var result Message

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