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

// MessagesGetWebPageRequest represents TL type `messages.getWebPage#8d9692a3`.
// Get instant view¹ page
//
// Links:
//  1. https://instantview.telegram.org
//
// See https://core.telegram.org/method/messages.getWebPage for reference.
type MessagesGetWebPageRequest struct {
	// URL of IV page to fetch
	URL string
	// Hash used for caching, for more info click here¹. Note: the usual hash generation
	// algorithm cannot be used in this case, please re-use the webPage².hash field returned
	// by a previous call to the method, or pass 0 if this is the first call or if the
	// previous call did not return a webPage³.
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	//  2) https://core.telegram.org/constructor/webPage
	//  3) https://core.telegram.org/constructor/webPage
	Hash int
}

// MessagesGetWebPageRequestTypeID is TL type id of MessagesGetWebPageRequest.
const MessagesGetWebPageRequestTypeID = 0x8d9692a3

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

func (g *MessagesGetWebPageRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.URL == "") {
		return false
	}
	if !(g.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetWebPageRequest from given interface.
func (g *MessagesGetWebPageRequest) FillFrom(from interface {
	GetURL() (value string)
	GetHash() (value int)
}) {
	g.URL = from.GetURL()
	g.Hash = from.GetHash()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesGetWebPageRequest) TypeName() string {
	return "messages.getWebPage"
}

// TypeInfo returns info about TL type.
func (g *MessagesGetWebPageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getWebPage",
		ID:   MessagesGetWebPageRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *MessagesGetWebPageRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getWebPage#8d9692a3 as nil")
	}
	b.PutID(MessagesGetWebPageRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetWebPageRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getWebPage#8d9692a3 as nil")
	}
	b.PutString(g.URL)
	b.PutInt(g.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (g *MessagesGetWebPageRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getWebPage#8d9692a3 to nil")
	}
	if err := b.ConsumeID(MessagesGetWebPageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.getWebPage#8d9692a3: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetWebPageRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getWebPage#8d9692a3 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getWebPage#8d9692a3: field url: %w", err)
		}
		g.URL = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getWebPage#8d9692a3: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

// GetURL returns value of URL field.
func (g *MessagesGetWebPageRequest) GetURL() (value string) {
	if g == nil {
		return
	}
	return g.URL
}

// GetHash returns value of Hash field.
func (g *MessagesGetWebPageRequest) GetHash() (value int) {
	if g == nil {
		return
	}
	return g.Hash
}

// MessagesGetWebPage invokes method messages.getWebPage#8d9692a3 returning error if any.
// Get instant view¹ page
//
// Links:
//  1. https://instantview.telegram.org
//
// Possible errors:
//
//	400 WC_CONVERT_URL_INVALID: WC convert URL invalid.
//
// See https://core.telegram.org/method/messages.getWebPage for reference.
func (c *Client) MessagesGetWebPage(ctx context.Context, request *MessagesGetWebPageRequest) (*MessagesWebPage, error) {
	var result MessagesWebPage

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