// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// BotsDeletePreviewMediaRequest represents TL type `bots.deletePreviewMedia#2d0135b3`.
// Delete a main mini app preview, see here »¹ for more info.
// Only owners of bots with a configured Main Mini App can use this method, see see here
// »¹ for more info on how to check if you can invoke this method.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//  2. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//
// See https://core.telegram.org/method/bots.deletePreviewMedia for reference.
type BotsDeletePreviewMediaRequest struct {
	// The bot that owns the Main Mini App.
	Bot InputUserClass
	// ISO 639-1 language code, indicating the localization of the preview to delete.
	LangCode string
	// The photo/video preview to delete, previously fetched as specified here »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/webapps#main-mini-app-previews
	Media []InputMediaClass
}

// BotsDeletePreviewMediaRequestTypeID is TL type id of BotsDeletePreviewMediaRequest.
const BotsDeletePreviewMediaRequestTypeID = 0x2d0135b3

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

func (d *BotsDeletePreviewMediaRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Bot == nil) {
		return false
	}
	if !(d.LangCode == "") {
		return false
	}
	if !(d.Media == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotsDeletePreviewMediaRequest from given interface.
func (d *BotsDeletePreviewMediaRequest) FillFrom(from interface {
	GetBot() (value InputUserClass)
	GetLangCode() (value string)
	GetMedia() (value []InputMediaClass)
}) {
	d.Bot = from.GetBot()
	d.LangCode = from.GetLangCode()
	d.Media = from.GetMedia()
}

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

// TypeName returns name of type in TL schema.
func (*BotsDeletePreviewMediaRequest) TypeName() string {
	return "bots.deletePreviewMedia"
}

// TypeInfo returns info about TL type.
func (d *BotsDeletePreviewMediaRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.deletePreviewMedia",
		ID:   BotsDeletePreviewMediaRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "LangCode",
			SchemaName: "lang_code",
		},
		{
			Name:       "Media",
			SchemaName: "media",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *BotsDeletePreviewMediaRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode bots.deletePreviewMedia#2d0135b3 as nil")
	}
	b.PutID(BotsDeletePreviewMediaRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *BotsDeletePreviewMediaRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode bots.deletePreviewMedia#2d0135b3 as nil")
	}
	if d.Bot == nil {
		return fmt.Errorf("unable to encode bots.deletePreviewMedia#2d0135b3: field bot is nil")
	}
	if err := d.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.deletePreviewMedia#2d0135b3: field bot: %w", err)
	}
	b.PutString(d.LangCode)
	b.PutVectorHeader(len(d.Media))
	for idx, v := range d.Media {
		if v == nil {
			return fmt.Errorf("unable to encode bots.deletePreviewMedia#2d0135b3: field media element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode bots.deletePreviewMedia#2d0135b3: field media element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *BotsDeletePreviewMediaRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode bots.deletePreviewMedia#2d0135b3 to nil")
	}
	if err := b.ConsumeID(BotsDeletePreviewMediaRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.deletePreviewMedia#2d0135b3: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *BotsDeletePreviewMediaRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode bots.deletePreviewMedia#2d0135b3 to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.deletePreviewMedia#2d0135b3: field bot: %w", err)
		}
		d.Bot = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.deletePreviewMedia#2d0135b3: field lang_code: %w", err)
		}
		d.LangCode = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode bots.deletePreviewMedia#2d0135b3: field media: %w", err)
		}

		if headerLen > 0 {
			d.Media = make([]InputMediaClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputMedia(b)
			if err != nil {
				return fmt.Errorf("unable to decode bots.deletePreviewMedia#2d0135b3: field media: %w", err)
			}
			d.Media = append(d.Media, value)
		}
	}
	return nil
}

// GetBot returns value of Bot field.
func (d *BotsDeletePreviewMediaRequest) GetBot() (value InputUserClass) {
	if d == nil {
		return
	}
	return d.Bot
}

// GetLangCode returns value of LangCode field.
func (d *BotsDeletePreviewMediaRequest) GetLangCode() (value string) {
	if d == nil {
		return
	}
	return d.LangCode
}

// GetMedia returns value of Media field.
func (d *BotsDeletePreviewMediaRequest) GetMedia() (value []InputMediaClass) {
	if d == nil {
		return
	}
	return d.Media
}

// MapMedia returns field Media wrapped in InputMediaClassArray helper.
func (d *BotsDeletePreviewMediaRequest) MapMedia() (value InputMediaClassArray) {
	return InputMediaClassArray(d.Media)
}

// BotsDeletePreviewMedia invokes method bots.deletePreviewMedia#2d0135b3 returning error if any.
// Delete a main mini app preview, see here »¹ for more info.
// Only owners of bots with a configured Main Mini App can use this method, see see here
// »¹ for more info on how to check if you can invoke this method.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//  2. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//
// Possible errors:
//
//	400 BOT_INVALID: This is not a valid bot.
//
// See https://core.telegram.org/method/bots.deletePreviewMedia for reference.
func (c *Client) BotsDeletePreviewMedia(ctx context.Context, request *BotsDeletePreviewMediaRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
