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

// EditBusinessStoryRequest represents TL type `editBusinessStory#1c2a5f3c`.
type EditBusinessStoryRequest struct {
	// Identifier of the chat that posted the story
	StoryPosterChatID int64
	// Identifier of the story to edit
	StoryID int32
	// New content of the story
	Content InputStoryContentClass
	// New clickable rectangle areas to be shown on the story media
	Areas InputStoryAreas
	// New story caption
	Caption FormattedText
	// The new privacy settings for the story
	PrivacySettings StoryPrivacySettingsClass
}

// EditBusinessStoryRequestTypeID is TL type id of EditBusinessStoryRequest.
const EditBusinessStoryRequestTypeID = 0x1c2a5f3c

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

func (e *EditBusinessStoryRequest) 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
	}
	if !(e.PrivacySettings == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EditBusinessStoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "editBusinessStory",
		ID:   EditBusinessStoryRequestTypeID,
	}
	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",
		},
		{
			Name:       "PrivacySettings",
			SchemaName: "privacy_settings",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EditBusinessStoryRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editBusinessStory#1c2a5f3c as nil")
	}
	b.PutID(EditBusinessStoryRequestTypeID)
	return e.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (e *EditBusinessStoryRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editBusinessStory#1c2a5f3c to nil")
	}
	if err := b.ConsumeID(EditBusinessStoryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode editBusinessStory#1c2a5f3c: %w", err)
	}
	return e.DecodeBare(b)
}

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EditBusinessStoryRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode editBusinessStory#1c2a5f3c as nil")
	}
	b.ObjStart()
	b.PutID("editBusinessStory")
	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 editBusinessStory#1c2a5f3c: field content is nil")
	}
	if err := e.Content.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessStory#1c2a5f3c: field content: %w", err)
	}
	b.Comma()
	b.FieldStart("areas")
	if err := e.Areas.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessStory#1c2a5f3c: field areas: %w", err)
	}
	b.Comma()
	b.FieldStart("caption")
	if err := e.Caption.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessStory#1c2a5f3c: field caption: %w", err)
	}
	b.Comma()
	b.FieldStart("privacy_settings")
	if e.PrivacySettings == nil {
		return fmt.Errorf("unable to encode editBusinessStory#1c2a5f3c: field privacy_settings is nil")
	}
	if err := e.PrivacySettings.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessStory#1c2a5f3c: field privacy_settings: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EditBusinessStoryRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode editBusinessStory#1c2a5f3c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("editBusinessStory"); err != nil {
				return fmt.Errorf("unable to decode editBusinessStory#1c2a5f3c: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessStory#1c2a5f3c: 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 editBusinessStory#1c2a5f3c: field story_id: %w", err)
			}
			e.StoryID = value
		case "content":
			value, err := DecodeTDLibJSONInputStoryContent(b)
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessStory#1c2a5f3c: field content: %w", err)
			}
			e.Content = value
		case "areas":
			if err := e.Areas.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode editBusinessStory#1c2a5f3c: field areas: %w", err)
			}
		case "caption":
			if err := e.Caption.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode editBusinessStory#1c2a5f3c: field caption: %w", err)
			}
		case "privacy_settings":
			value, err := DecodeTDLibJSONStoryPrivacySettings(b)
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessStory#1c2a5f3c: field privacy_settings: %w", err)
			}
			e.PrivacySettings = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

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

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

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

// EditBusinessStory invokes method editBusinessStory#1c2a5f3c returning error if any.
func (c *Client) EditBusinessStory(ctx context.Context, request *EditBusinessStoryRequest) (*Story, error) {
	var result Story

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