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

// UnpinAllChatMessagesRequest represents TL type `unpinAllChatMessages#aa4cd4b7`.
type UnpinAllChatMessagesRequest struct {
	// Identifier of the chat
	ChatID int64
}

// UnpinAllChatMessagesRequestTypeID is TL type id of UnpinAllChatMessagesRequest.
const UnpinAllChatMessagesRequestTypeID = 0xaa4cd4b7

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

func (u *UnpinAllChatMessagesRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.ChatID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (u *UnpinAllChatMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "unpinAllChatMessages",
		ID:   UnpinAllChatMessagesRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UnpinAllChatMessagesRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode unpinAllChatMessages#aa4cd4b7 as nil")
	}
	b.PutID(UnpinAllChatMessagesRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UnpinAllChatMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode unpinAllChatMessages#aa4cd4b7 as nil")
	}
	b.PutInt53(u.ChatID)
	return nil
}

// Decode implements bin.Decoder.
func (u *UnpinAllChatMessagesRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode unpinAllChatMessages#aa4cd4b7 to nil")
	}
	if err := b.ConsumeID(UnpinAllChatMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode unpinAllChatMessages#aa4cd4b7: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *UnpinAllChatMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode unpinAllChatMessages#aa4cd4b7 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode unpinAllChatMessages#aa4cd4b7: field chat_id: %w", err)
		}
		u.ChatID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UnpinAllChatMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode unpinAllChatMessages#aa4cd4b7 as nil")
	}
	b.ObjStart()
	b.PutID("unpinAllChatMessages")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(u.ChatID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (u *UnpinAllChatMessagesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode unpinAllChatMessages#aa4cd4b7 to nil")
	}

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

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

// UnpinAllChatMessages invokes method unpinAllChatMessages#aa4cd4b7 returning error if any.
func (c *Client) UnpinAllChatMessages(ctx context.Context, chatid int64) error {
	var ok Ok

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