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

// GetWebPageInstantViewRequest represents TL type `getWebPageInstantView#67cb94fd`.
type GetWebPageInstantViewRequest struct {
	// The web page URL
	URL string
	// Pass true to get only locally available information without sending network requests
	OnlyLocal bool
}

// GetWebPageInstantViewRequestTypeID is TL type id of GetWebPageInstantViewRequest.
const GetWebPageInstantViewRequestTypeID = 0x67cb94fd

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

func (g *GetWebPageInstantViewRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.URL == "") {
		return false
	}
	if !(g.OnlyLocal == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetWebPageInstantViewRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getWebPageInstantView",
		ID:   GetWebPageInstantViewRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "OnlyLocal",
			SchemaName: "only_local",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetWebPageInstantViewRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebPageInstantView#67cb94fd as nil")
	}
	b.PutID(GetWebPageInstantViewRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetWebPageInstantViewRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebPageInstantView#67cb94fd as nil")
	}
	b.PutString(g.URL)
	b.PutBool(g.OnlyLocal)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetWebPageInstantViewRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getWebPageInstantView#67cb94fd to nil")
	}
	if err := b.ConsumeID(GetWebPageInstantViewRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getWebPageInstantView#67cb94fd: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetWebPageInstantViewRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getWebPageInstantView#67cb94fd to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getWebPageInstantView#67cb94fd: field url: %w", err)
		}
		g.URL = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getWebPageInstantView#67cb94fd: field only_local: %w", err)
		}
		g.OnlyLocal = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetWebPageInstantViewRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebPageInstantView#67cb94fd as nil")
	}
	b.ObjStart()
	b.PutID("getWebPageInstantView")
	b.Comma()
	b.FieldStart("url")
	b.PutString(g.URL)
	b.Comma()
	b.FieldStart("only_local")
	b.PutBool(g.OnlyLocal)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetWebPageInstantViewRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getWebPageInstantView#67cb94fd to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getWebPageInstantView"); err != nil {
				return fmt.Errorf("unable to decode getWebPageInstantView#67cb94fd: %w", err)
			}
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getWebPageInstantView#67cb94fd: field url: %w", err)
			}
			g.URL = value
		case "only_local":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getWebPageInstantView#67cb94fd: field only_local: %w", err)
			}
			g.OnlyLocal = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetOnlyLocal returns value of OnlyLocal field.
func (g *GetWebPageInstantViewRequest) GetOnlyLocal() (value bool) {
	if g == nil {
		return
	}
	return g.OnlyLocal
}

// GetWebPageInstantView invokes method getWebPageInstantView#67cb94fd returning error if any.
func (c *Client) GetWebPageInstantView(ctx context.Context, request *GetWebPageInstantViewRequest) (*WebPageInstantView, error) {
	var result WebPageInstantView

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