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

// DeleteChatMessagesBySenderRequest represents TL type `deleteChatMessagesBySender#ba9b2e67`.
type DeleteChatMessagesBySenderRequest struct {
	// Chat identifier
	ChatID int64
	// Identifier of the sender of messages to delete
	SenderID MessageSenderClass
}

// DeleteChatMessagesBySenderRequestTypeID is TL type id of DeleteChatMessagesBySenderRequest.
const DeleteChatMessagesBySenderRequestTypeID = 0xba9b2e67

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

func (d *DeleteChatMessagesBySenderRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.ChatID == 0) {
		return false
	}
	if !(d.SenderID == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DeleteChatMessagesBySenderRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "deleteChatMessagesBySender",
		ID:   DeleteChatMessagesBySenderRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DeleteChatMessagesBySenderRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatMessagesBySender#ba9b2e67 as nil")
	}
	b.PutID(DeleteChatMessagesBySenderRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DeleteChatMessagesBySenderRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatMessagesBySender#ba9b2e67 as nil")
	}
	b.PutInt53(d.ChatID)
	if d.SenderID == nil {
		return fmt.Errorf("unable to encode deleteChatMessagesBySender#ba9b2e67: field sender_id is nil")
	}
	if err := d.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode deleteChatMessagesBySender#ba9b2e67: field sender_id: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *DeleteChatMessagesBySenderRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatMessagesBySender#ba9b2e67 to nil")
	}
	if err := b.ConsumeID(DeleteChatMessagesBySenderRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode deleteChatMessagesBySender#ba9b2e67: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DeleteChatMessagesBySenderRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatMessagesBySender#ba9b2e67 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode deleteChatMessagesBySender#ba9b2e67: field chat_id: %w", err)
		}
		d.ChatID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode deleteChatMessagesBySender#ba9b2e67: field sender_id: %w", err)
		}
		d.SenderID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DeleteChatMessagesBySenderRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatMessagesBySender#ba9b2e67 as nil")
	}
	b.ObjStart()
	b.PutID("deleteChatMessagesBySender")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(d.ChatID)
	b.Comma()
	b.FieldStart("sender_id")
	if d.SenderID == nil {
		return fmt.Errorf("unable to encode deleteChatMessagesBySender#ba9b2e67: field sender_id is nil")
	}
	if err := d.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode deleteChatMessagesBySender#ba9b2e67: field sender_id: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DeleteChatMessagesBySenderRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatMessagesBySender#ba9b2e67 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("deleteChatMessagesBySender"); err != nil {
				return fmt.Errorf("unable to decode deleteChatMessagesBySender#ba9b2e67: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode deleteChatMessagesBySender#ba9b2e67: field chat_id: %w", err)
			}
			d.ChatID = value
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode deleteChatMessagesBySender#ba9b2e67: field sender_id: %w", err)
			}
			d.SenderID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// DeleteChatMessagesBySender invokes method deleteChatMessagesBySender#ba9b2e67 returning error if any.
func (c *Client) DeleteChatMessagesBySender(ctx context.Context, request *DeleteChatMessagesBySenderRequest) error {
	var ok Ok

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