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

// PostStoryRequest represents TL type `postStory#f0a54077`.
type PostStoryRequest struct {
	// Identifier of the chat that will post the story. Pass Saved Messages chat identifier
	// when posting a story on behalf of the current user
	ChatID int64
	// Content of the story
	Content InputStoryContentClass
	// Clickable rectangle areas to be shown on the story media; pass null if none
	Areas InputStoryAreas
	// Story caption; pass null to use an empty caption;
	// 0-getOption("story_caption_length_max") characters; can have entities only if
	// getOption("can_use_text_entities_in_story_caption")
	Caption FormattedText
	// The privacy settings for the story; ignored for stories posted on behalf of supergroup
	// and channel chats
	PrivacySettings StoryPrivacySettingsClass
	// Period after which the story is moved to archive, in seconds; must be one of 6 * 3600,
	// 12 * 3600, 86400, or 2 * 86400 for Telegram Premium users, and 86400 otherwise
	ActivePeriod int32
	// Full identifier of the original story, which content was used to create the story;
	// pass null if the story isn't repost of another story
	FromStoryFullID StoryFullID
	// Pass true to keep the story accessible after expiration
	IsPostedToChatPage bool
	// Pass true if the content of the story must be protected from forwarding and
	// screenshotting
	ProtectContent bool
}

// PostStoryRequestTypeID is TL type id of PostStoryRequest.
const PostStoryRequestTypeID = 0xf0a54077

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

func (p *PostStoryRequest) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.ChatID == 0) {
		return false
	}
	if !(p.Content == nil) {
		return false
	}
	if !(p.Areas.Zero()) {
		return false
	}
	if !(p.Caption.Zero()) {
		return false
	}
	if !(p.PrivacySettings == nil) {
		return false
	}
	if !(p.ActivePeriod == 0) {
		return false
	}
	if !(p.FromStoryFullID.Zero()) {
		return false
	}
	if !(p.IsPostedToChatPage == false) {
		return false
	}
	if !(p.ProtectContent == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PostStoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "postStory",
		ID:   PostStoryRequestTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Content",
			SchemaName: "content",
		},
		{
			Name:       "Areas",
			SchemaName: "areas",
		},
		{
			Name:       "Caption",
			SchemaName: "caption",
		},
		{
			Name:       "PrivacySettings",
			SchemaName: "privacy_settings",
		},
		{
			Name:       "ActivePeriod",
			SchemaName: "active_period",
		},
		{
			Name:       "FromStoryFullID",
			SchemaName: "from_story_full_id",
		},
		{
			Name:       "IsPostedToChatPage",
			SchemaName: "is_posted_to_chat_page",
		},
		{
			Name:       "ProtectContent",
			SchemaName: "protect_content",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PostStoryRequest) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode postStory#f0a54077 as nil")
	}
	b.PutID(PostStoryRequestTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PostStoryRequest) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode postStory#f0a54077 as nil")
	}
	b.PutInt53(p.ChatID)
	if p.Content == nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field content is nil")
	}
	if err := p.Content.Encode(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field content: %w", err)
	}
	if err := p.Areas.Encode(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field areas: %w", err)
	}
	if err := p.Caption.Encode(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field caption: %w", err)
	}
	if p.PrivacySettings == nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field privacy_settings is nil")
	}
	if err := p.PrivacySettings.Encode(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field privacy_settings: %w", err)
	}
	b.PutInt32(p.ActivePeriod)
	if err := p.FromStoryFullID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field from_story_full_id: %w", err)
	}
	b.PutBool(p.IsPostedToChatPage)
	b.PutBool(p.ProtectContent)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PostStoryRequest) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode postStory#f0a54077 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field chat_id: %w", err)
		}
		p.ChatID = value
	}
	{
		value, err := DecodeInputStoryContent(b)
		if err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field content: %w", err)
		}
		p.Content = value
	}
	{
		if err := p.Areas.Decode(b); err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field areas: %w", err)
		}
	}
	{
		if err := p.Caption.Decode(b); err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field caption: %w", err)
		}
	}
	{
		value, err := DecodeStoryPrivacySettings(b)
		if err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field privacy_settings: %w", err)
		}
		p.PrivacySettings = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field active_period: %w", err)
		}
		p.ActivePeriod = value
	}
	{
		if err := p.FromStoryFullID.Decode(b); err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field from_story_full_id: %w", err)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field is_posted_to_chat_page: %w", err)
		}
		p.IsPostedToChatPage = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode postStory#f0a54077: field protect_content: %w", err)
		}
		p.ProtectContent = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PostStoryRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode postStory#f0a54077 as nil")
	}
	b.ObjStart()
	b.PutID("postStory")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(p.ChatID)
	b.Comma()
	b.FieldStart("content")
	if p.Content == nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field content is nil")
	}
	if err := p.Content.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field content: %w", err)
	}
	b.Comma()
	b.FieldStart("areas")
	if err := p.Areas.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field areas: %w", err)
	}
	b.Comma()
	b.FieldStart("caption")
	if err := p.Caption.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field caption: %w", err)
	}
	b.Comma()
	b.FieldStart("privacy_settings")
	if p.PrivacySettings == nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field privacy_settings is nil")
	}
	if err := p.PrivacySettings.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field privacy_settings: %w", err)
	}
	b.Comma()
	b.FieldStart("active_period")
	b.PutInt32(p.ActivePeriod)
	b.Comma()
	b.FieldStart("from_story_full_id")
	if err := p.FromStoryFullID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode postStory#f0a54077: field from_story_full_id: %w", err)
	}
	b.Comma()
	b.FieldStart("is_posted_to_chat_page")
	b.PutBool(p.IsPostedToChatPage)
	b.Comma()
	b.FieldStart("protect_content")
	b.PutBool(p.ProtectContent)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("postStory"); err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field chat_id: %w", err)
			}
			p.ChatID = value
		case "content":
			value, err := DecodeTDLibJSONInputStoryContent(b)
			if err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field content: %w", err)
			}
			p.Content = value
		case "areas":
			if err := p.Areas.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field areas: %w", err)
			}
		case "caption":
			if err := p.Caption.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field caption: %w", err)
			}
		case "privacy_settings":
			value, err := DecodeTDLibJSONStoryPrivacySettings(b)
			if err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field privacy_settings: %w", err)
			}
			p.PrivacySettings = value
		case "active_period":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field active_period: %w", err)
			}
			p.ActivePeriod = value
		case "from_story_full_id":
			if err := p.FromStoryFullID.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field from_story_full_id: %w", err)
			}
		case "is_posted_to_chat_page":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field is_posted_to_chat_page: %w", err)
			}
			p.IsPostedToChatPage = value
		case "protect_content":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode postStory#f0a54077: field protect_content: %w", err)
			}
			p.ProtectContent = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetContent returns value of Content field.
func (p *PostStoryRequest) GetContent() (value InputStoryContentClass) {
	if p == nil {
		return
	}
	return p.Content
}

// GetAreas returns value of Areas field.
func (p *PostStoryRequest) GetAreas() (value InputStoryAreas) {
	if p == nil {
		return
	}
	return p.Areas
}

// GetCaption returns value of Caption field.
func (p *PostStoryRequest) GetCaption() (value FormattedText) {
	if p == nil {
		return
	}
	return p.Caption
}

// GetPrivacySettings returns value of PrivacySettings field.
func (p *PostStoryRequest) GetPrivacySettings() (value StoryPrivacySettingsClass) {
	if p == nil {
		return
	}
	return p.PrivacySettings
}

// GetActivePeriod returns value of ActivePeriod field.
func (p *PostStoryRequest) GetActivePeriod() (value int32) {
	if p == nil {
		return
	}
	return p.ActivePeriod
}

// GetFromStoryFullID returns value of FromStoryFullID field.
func (p *PostStoryRequest) GetFromStoryFullID() (value StoryFullID) {
	if p == nil {
		return
	}
	return p.FromStoryFullID
}

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

// GetProtectContent returns value of ProtectContent field.
func (p *PostStoryRequest) GetProtectContent() (value bool) {
	if p == nil {
		return
	}
	return p.ProtectContent
}

// PostStory invokes method postStory#f0a54077 returning error if any.
func (c *Client) PostStory(ctx context.Context, request *PostStoryRequest) (*Story, error) {
	var result Story

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