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

// ToggleStoryIsPostedToChatPageRequest represents TL type `toggleStoryIsPostedToChatPage#8056a16c`.
type ToggleStoryIsPostedToChatPageRequest struct {
	// Identifier of the chat that posted the story
	StoryPosterChatID int64
	// Identifier of the story
	StoryID int32
	// Pass true to make the story accessible after expiration; pass false to make it private
	IsPostedToChatPage bool
}

// ToggleStoryIsPostedToChatPageRequestTypeID is TL type id of ToggleStoryIsPostedToChatPageRequest.
const ToggleStoryIsPostedToChatPageRequestTypeID = 0x8056a16c

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

func (t *ToggleStoryIsPostedToChatPageRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.StoryPosterChatID == 0) {
		return false
	}
	if !(t.StoryID == 0) {
		return false
	}
	if !(t.IsPostedToChatPage == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *ToggleStoryIsPostedToChatPageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "toggleStoryIsPostedToChatPage",
		ID:   ToggleStoryIsPostedToChatPageRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryPosterChatID",
			SchemaName: "story_poster_chat_id",
		},
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
		{
			Name:       "IsPostedToChatPage",
			SchemaName: "is_posted_to_chat_page",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ToggleStoryIsPostedToChatPageRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleStoryIsPostedToChatPage#8056a16c as nil")
	}
	b.PutID(ToggleStoryIsPostedToChatPageRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleStoryIsPostedToChatPageRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleStoryIsPostedToChatPage#8056a16c as nil")
	}
	b.PutInt53(t.StoryPosterChatID)
	b.PutInt32(t.StoryID)
	b.PutBool(t.IsPostedToChatPage)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *ToggleStoryIsPostedToChatPageRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleStoryIsPostedToChatPage#8056a16c to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleStoryIsPostedToChatPage#8056a16c: field story_poster_chat_id: %w", err)
		}
		t.StoryPosterChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode toggleStoryIsPostedToChatPage#8056a16c: field story_id: %w", err)
		}
		t.StoryID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode toggleStoryIsPostedToChatPage#8056a16c: field is_posted_to_chat_page: %w", err)
		}
		t.IsPostedToChatPage = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleStoryIsPostedToChatPageRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleStoryIsPostedToChatPage#8056a16c as nil")
	}
	b.ObjStart()
	b.PutID("toggleStoryIsPostedToChatPage")
	b.Comma()
	b.FieldStart("story_poster_chat_id")
	b.PutInt53(t.StoryPosterChatID)
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(t.StoryID)
	b.Comma()
	b.FieldStart("is_posted_to_chat_page")
	b.PutBool(t.IsPostedToChatPage)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("toggleStoryIsPostedToChatPage"); err != nil {
				return fmt.Errorf("unable to decode toggleStoryIsPostedToChatPage#8056a16c: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode toggleStoryIsPostedToChatPage#8056a16c: field story_poster_chat_id: %w", err)
			}
			t.StoryPosterChatID = value
		case "story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode toggleStoryIsPostedToChatPage#8056a16c: field story_id: %w", err)
			}
			t.StoryID = value
		case "is_posted_to_chat_page":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode toggleStoryIsPostedToChatPage#8056a16c: field is_posted_to_chat_page: %w", err)
			}
			t.IsPostedToChatPage = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStoryPosterChatID returns value of StoryPosterChatID field.
func (t *ToggleStoryIsPostedToChatPageRequest) GetStoryPosterChatID() (value int64) {
	if t == nil {
		return
	}
	return t.StoryPosterChatID
}

// GetStoryID returns value of StoryID field.
func (t *ToggleStoryIsPostedToChatPageRequest) GetStoryID() (value int32) {
	if t == nil {
		return
	}
	return t.StoryID
}

// GetIsPostedToChatPage returns value of IsPostedToChatPage field.
func (t *ToggleStoryIsPostedToChatPageRequest) GetIsPostedToChatPage() (value bool) {
	if t == nil {
		return
	}
	return t.IsPostedToChatPage
}

// ToggleStoryIsPostedToChatPage invokes method toggleStoryIsPostedToChatPage#8056a16c returning error if any.
func (c *Client) ToggleStoryIsPostedToChatPage(ctx context.Context, request *ToggleStoryIsPostedToChatPageRequest) error {
	var ok Ok

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