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

// CanPostStoryRequest represents TL type `canPostStory#27da5ace`.
type CanPostStoryRequest struct {
	// Chat identifier. Pass Saved Messages chat identifier when posting a story on behalf of
	// the current user
	ChatID int64
}

// CanPostStoryRequestTypeID is TL type id of CanPostStoryRequest.
const CanPostStoryRequestTypeID = 0x27da5ace

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *CanPostStoryRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode canPostStory#27da5ace as nil")
	}
	b.PutID(CanPostStoryRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CanPostStoryRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode canPostStory#27da5ace as nil")
	}
	b.PutInt53(c.ChatID)
	return nil
}

// Decode implements bin.Decoder.
func (c *CanPostStoryRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode canPostStory#27da5ace to nil")
	}
	if err := b.ConsumeID(CanPostStoryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode canPostStory#27da5ace: %w", err)
	}
	return c.DecodeBare(b)
}

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

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CanPostStoryRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode canPostStory#27da5ace to nil")
	}

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

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

// CanPostStory invokes method canPostStory#27da5ace returning error if any.
func (c *Client) CanPostStory(ctx context.Context, chatid int64) (CanPostStoryResultClass, error) {
	var result CanPostStoryResultBox

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