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

// ToggleSavedMessagesTopicIsPinnedRequest represents TL type `toggleSavedMessagesTopicIsPinned#a15345cc`.
type ToggleSavedMessagesTopicIsPinnedRequest struct {
	// Identifier of Saved Messages topic to pin or unpin
	SavedMessagesTopicID int64
	// Pass true to pin the topic; pass false to unpin it
	IsPinned bool
}

// ToggleSavedMessagesTopicIsPinnedRequestTypeID is TL type id of ToggleSavedMessagesTopicIsPinnedRequest.
const ToggleSavedMessagesTopicIsPinnedRequestTypeID = 0xa15345cc

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

func (t *ToggleSavedMessagesTopicIsPinnedRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.SavedMessagesTopicID == 0) {
		return false
	}
	if !(t.IsPinned == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *ToggleSavedMessagesTopicIsPinnedRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "toggleSavedMessagesTopicIsPinned",
		ID:   ToggleSavedMessagesTopicIsPinnedRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SavedMessagesTopicID",
			SchemaName: "saved_messages_topic_id",
		},
		{
			Name:       "IsPinned",
			SchemaName: "is_pinned",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ToggleSavedMessagesTopicIsPinnedRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSavedMessagesTopicIsPinned#a15345cc as nil")
	}
	b.PutID(ToggleSavedMessagesTopicIsPinnedRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleSavedMessagesTopicIsPinnedRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSavedMessagesTopicIsPinned#a15345cc as nil")
	}
	b.PutInt53(t.SavedMessagesTopicID)
	b.PutBool(t.IsPinned)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *ToggleSavedMessagesTopicIsPinnedRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSavedMessagesTopicIsPinned#a15345cc to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSavedMessagesTopicIsPinned#a15345cc: field saved_messages_topic_id: %w", err)
		}
		t.SavedMessagesTopicID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSavedMessagesTopicIsPinned#a15345cc: field is_pinned: %w", err)
		}
		t.IsPinned = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleSavedMessagesTopicIsPinnedRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSavedMessagesTopicIsPinned#a15345cc as nil")
	}
	b.ObjStart()
	b.PutID("toggleSavedMessagesTopicIsPinned")
	b.Comma()
	b.FieldStart("saved_messages_topic_id")
	b.PutInt53(t.SavedMessagesTopicID)
	b.Comma()
	b.FieldStart("is_pinned")
	b.PutBool(t.IsPinned)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetSavedMessagesTopicID returns value of SavedMessagesTopicID field.
func (t *ToggleSavedMessagesTopicIsPinnedRequest) GetSavedMessagesTopicID() (value int64) {
	if t == nil {
		return
	}
	return t.SavedMessagesTopicID
}

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

// ToggleSavedMessagesTopicIsPinned invokes method toggleSavedMessagesTopicIsPinned#a15345cc returning error if any.
func (c *Client) ToggleSavedMessagesTopicIsPinned(ctx context.Context, request *ToggleSavedMessagesTopicIsPinnedRequest) error {
	var ok Ok

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