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

// EditStoryRequest represents TL type `editStory#152d6614`.
type EditStoryRequest struct {
	// Identifier of the chat that posted the story
	StoryPosterChatID int64
	// Identifier of the story to edit
	StoryID int32
	// New content of the story; pass null to keep the current content
	Content InputStoryContentClass
	// New clickable rectangle areas to be shown on the story media; pass null to keep the
	// current areas. Areas can't be edited if story content isn't changed
	Areas InputStoryAreas
	// New story caption; pass null to keep the current caption
	Caption FormattedText
}

// EditStoryRequestTypeID is TL type id of EditStoryRequest.
const EditStoryRequestTypeID = 0x152d6614

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

func (e *EditStoryRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.StoryPosterChatID == 0) {
		return false
	}
	if !(e.StoryID == 0) {
		return false
	}
	if !(e.Content == nil) {
		return false
	}
	if !(e.Areas.Zero()) {
		return false
	}
	if !(e.Caption.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EditStoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "editStory",
		ID:   EditStoryRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryPosterChatID",
			SchemaName: "story_poster_chat_id",
		},
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
		{
			Name:       "Content",
			SchemaName: "content",
		},
		{
			Name:       "Areas",
			SchemaName: "areas",
		},
		{
			Name:       "Caption",
			SchemaName: "caption",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EditStoryRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editStory#152d6614 as nil")
	}
	b.PutID(EditStoryRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EditStoryRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editStory#152d6614 as nil")
	}
	b.PutInt53(e.StoryPosterChatID)
	b.PutInt32(e.StoryID)
	if e.Content == nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field content is nil")
	}
	if err := e.Content.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field content: %w", err)
	}
	if err := e.Areas.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field areas: %w", err)
	}
	if err := e.Caption.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field caption: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *EditStoryRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editStory#152d6614 to nil")
	}
	if err := b.ConsumeID(EditStoryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode editStory#152d6614: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EditStoryRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editStory#152d6614 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode editStory#152d6614: field story_poster_chat_id: %w", err)
		}
		e.StoryPosterChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode editStory#152d6614: field story_id: %w", err)
		}
		e.StoryID = value
	}
	{
		value, err := DecodeInputStoryContent(b)
		if err != nil {
			return fmt.Errorf("unable to decode editStory#152d6614: field content: %w", err)
		}
		e.Content = value
	}
	{
		if err := e.Areas.Decode(b); err != nil {
			return fmt.Errorf("unable to decode editStory#152d6614: field areas: %w", err)
		}
	}
	{
		if err := e.Caption.Decode(b); err != nil {
			return fmt.Errorf("unable to decode editStory#152d6614: field caption: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EditStoryRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode editStory#152d6614 as nil")
	}
	b.ObjStart()
	b.PutID("editStory")
	b.Comma()
	b.FieldStart("story_poster_chat_id")
	b.PutInt53(e.StoryPosterChatID)
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(e.StoryID)
	b.Comma()
	b.FieldStart("content")
	if e.Content == nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field content is nil")
	}
	if err := e.Content.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field content: %w", err)
	}
	b.Comma()
	b.FieldStart("areas")
	if err := e.Areas.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field areas: %w", err)
	}
	b.Comma()
	b.FieldStart("caption")
	if err := e.Caption.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editStory#152d6614: field caption: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EditStoryRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode editStory#152d6614 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("editStory"); err != nil {
				return fmt.Errorf("unable to decode editStory#152d6614: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode editStory#152d6614: field story_poster_chat_id: %w", err)
			}
			e.StoryPosterChatID = value
		case "story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode editStory#152d6614: field story_id: %w", err)
			}
			e.StoryID = value
		case "content":
			value, err := DecodeTDLibJSONInputStoryContent(b)
			if err != nil {
				return fmt.Errorf("unable to decode editStory#152d6614: field content: %w", err)
			}
			e.Content = value
		case "areas":
			if err := e.Areas.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode editStory#152d6614: field areas: %w", err)
			}
		case "caption":
			if err := e.Caption.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode editStory#152d6614: field caption: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

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

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

// EditStory invokes method editStory#152d6614 returning error if any.
func (c *Client) EditStory(ctx context.Context, request *EditStoryRequest) error {
	var ok Ok

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