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

// ToggleForumTopicIsPinnedRequest represents TL type `toggleForumTopicIsPinned#466ceab4`.
type ToggleForumTopicIsPinnedRequest struct {
	// Chat identifier
	ChatID int64
	// Message thread identifier of the forum topic
	MessageThreadID int64
	// Pass true to pin the topic; pass false to unpin it
	IsPinned bool
}

// ToggleForumTopicIsPinnedRequestTypeID is TL type id of ToggleForumTopicIsPinnedRequest.
const ToggleForumTopicIsPinnedRequestTypeID = 0x466ceab4

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

func (t *ToggleForumTopicIsPinnedRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.ChatID == 0) {
		return false
	}
	if !(t.MessageThreadID == 0) {
		return false
	}
	if !(t.IsPinned == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *ToggleForumTopicIsPinnedRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "toggleForumTopicIsPinned",
		ID:   ToggleForumTopicIsPinnedRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageThreadID",
			SchemaName: "message_thread_id",
		},
		{
			Name:       "IsPinned",
			SchemaName: "is_pinned",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ToggleForumTopicIsPinnedRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleForumTopicIsPinned#466ceab4 as nil")
	}
	b.PutID(ToggleForumTopicIsPinnedRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleForumTopicIsPinnedRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleForumTopicIsPinned#466ceab4 as nil")
	}
	b.PutInt53(t.ChatID)
	b.PutInt53(t.MessageThreadID)
	b.PutBool(t.IsPinned)
	return nil
}

// Decode implements bin.Decoder.
func (t *ToggleForumTopicIsPinnedRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleForumTopicIsPinned#466ceab4 to nil")
	}
	if err := b.ConsumeID(ToggleForumTopicIsPinnedRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ToggleForumTopicIsPinnedRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleForumTopicIsPinned#466ceab4 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: field chat_id: %w", err)
		}
		t.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: field message_thread_id: %w", err)
		}
		t.MessageThreadID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: field is_pinned: %w", err)
		}
		t.IsPinned = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleForumTopicIsPinnedRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleForumTopicIsPinned#466ceab4 as nil")
	}
	b.ObjStart()
	b.PutID("toggleForumTopicIsPinned")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(t.ChatID)
	b.Comma()
	b.FieldStart("message_thread_id")
	b.PutInt53(t.MessageThreadID)
	b.Comma()
	b.FieldStart("is_pinned")
	b.PutBool(t.IsPinned)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *ToggleForumTopicIsPinnedRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleForumTopicIsPinned#466ceab4 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("toggleForumTopicIsPinned"); err != nil {
				return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: field chat_id: %w", err)
			}
			t.ChatID = value
		case "message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: field message_thread_id: %w", err)
			}
			t.MessageThreadID = value
		case "is_pinned":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode toggleForumTopicIsPinned#466ceab4: field is_pinned: %w", err)
			}
			t.IsPinned = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetMessageThreadID returns value of MessageThreadID field.
func (t *ToggleForumTopicIsPinnedRequest) GetMessageThreadID() (value int64) {
	if t == nil {
		return
	}
	return t.MessageThreadID
}

// GetIsPinned returns value of IsPinned field.
func (t *ToggleForumTopicIsPinnedRequest) GetIsPinned() (value bool) {
	if t == nil {
		return
	}
	return t.IsPinned
}

// ToggleForumTopicIsPinned invokes method toggleForumTopicIsPinned#466ceab4 returning error if any.
func (c *Client) ToggleForumTopicIsPinned(ctx context.Context, request *ToggleForumTopicIsPinnedRequest) error {
	var ok Ok

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