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

// SetBusinessMessageIsPinnedRequest represents TL type `setBusinessMessageIsPinned#ff14f5f0`.
type SetBusinessMessageIsPinnedRequest struct {
	// Unique identifier of business connection on behalf of which the message was sent
	BusinessConnectionID string
	// The chat the message belongs to
	ChatID int64
	// Identifier of the message
	MessageID int64
	// Pass true to pin the message, pass false to unpin it
	IsPinned bool
}

// SetBusinessMessageIsPinnedRequestTypeID is TL type id of SetBusinessMessageIsPinnedRequest.
const SetBusinessMessageIsPinnedRequestTypeID = 0xff14f5f0

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

func (s *SetBusinessMessageIsPinnedRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.BusinessConnectionID == "") {
		return false
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.MessageID == 0) {
		return false
	}
	if !(s.IsPinned == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetBusinessMessageIsPinnedRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setBusinessMessageIsPinned",
		ID:   SetBusinessMessageIsPinnedRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "IsPinned",
			SchemaName: "is_pinned",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetBusinessMessageIsPinnedRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessMessageIsPinned#ff14f5f0 as nil")
	}
	b.PutID(SetBusinessMessageIsPinnedRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetBusinessMessageIsPinnedRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessMessageIsPinned#ff14f5f0 as nil")
	}
	b.PutString(s.BusinessConnectionID)
	b.PutInt53(s.ChatID)
	b.PutInt53(s.MessageID)
	b.PutBool(s.IsPinned)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetBusinessMessageIsPinnedRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setBusinessMessageIsPinned#ff14f5f0 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field business_connection_id: %w", err)
		}
		s.BusinessConnectionID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field message_id: %w", err)
		}
		s.MessageID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field is_pinned: %w", err)
		}
		s.IsPinned = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetBusinessMessageIsPinnedRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessMessageIsPinned#ff14f5f0 as nil")
	}
	b.ObjStart()
	b.PutID("setBusinessMessageIsPinned")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(s.BusinessConnectionID)
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(s.MessageID)
	b.Comma()
	b.FieldStart("is_pinned")
	b.PutBool(s.IsPinned)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setBusinessMessageIsPinned"); err != nil {
				return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field business_connection_id: %w", err)
			}
			s.BusinessConnectionID = value
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field chat_id: %w", err)
			}
			s.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field message_id: %w", err)
			}
			s.MessageID = value
		case "is_pinned":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode setBusinessMessageIsPinned#ff14f5f0: field is_pinned: %w", err)
			}
			s.IsPinned = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBusinessConnectionID returns value of BusinessConnectionID field.
func (s *SetBusinessMessageIsPinnedRequest) GetBusinessConnectionID() (value string) {
	if s == nil {
		return
	}
	return s.BusinessConnectionID
}

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

// GetMessageID returns value of MessageID field.
func (s *SetBusinessMessageIsPinnedRequest) GetMessageID() (value int64) {
	if s == nil {
		return
	}
	return s.MessageID
}

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

// SetBusinessMessageIsPinned invokes method setBusinessMessageIsPinned#ff14f5f0 returning error if any.
func (c *Client) SetBusinessMessageIsPinned(ctx context.Context, request *SetBusinessMessageIsPinnedRequest) error {
	var ok Ok

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