// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// MessagesRequestURLAuthRequest represents TL type `messages.requestUrlAuth#198fb446`.
// Get more info about a Seamless Telegram Login authorization request, for more info
// click here »¹
//
// Links:
//  1. https://core.telegram.org/api/url-authorization
//
// See https://core.telegram.org/method/messages.requestUrlAuth for reference.
type MessagesRequestURLAuthRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Peer where the message is located
	//
	// Use SetPeer and GetPeer helpers.
	Peer InputPeerClass
	// The message
	//
	// Use SetMsgID and GetMsgID helpers.
	MsgID int
	// The ID of the button with the authorization request
	//
	// Use SetButtonID and GetButtonID helpers.
	ButtonID int
	// URL used for link URL authorization, click here for more info »¹
	//
	// Links:
	//  1) https://core.telegram.org/api/url-authorization#link-url-authorization
	//
	// Use SetURL and GetURL helpers.
	URL string
}

// MessagesRequestURLAuthRequestTypeID is TL type id of MessagesRequestURLAuthRequest.
const MessagesRequestURLAuthRequestTypeID = 0x198fb446

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

func (r *MessagesRequestURLAuthRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.MsgID == 0) {
		return false
	}
	if !(r.ButtonID == 0) {
		return false
	}
	if !(r.URL == "") {
		return false
	}

	return true
}

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

// FillFrom fills MessagesRequestURLAuthRequest from given interface.
func (r *MessagesRequestURLAuthRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass, ok bool)
	GetMsgID() (value int, ok bool)
	GetButtonID() (value int, ok bool)
	GetURL() (value string, ok bool)
}) {
	if val, ok := from.GetPeer(); ok {
		r.Peer = val
	}

	if val, ok := from.GetMsgID(); ok {
		r.MsgID = val
	}

	if val, ok := from.GetButtonID(); ok {
		r.ButtonID = val
	}

	if val, ok := from.GetURL(); ok {
		r.URL = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (r *MessagesRequestURLAuthRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.requestUrlAuth",
		ID:   MessagesRequestURLAuthRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
			Null:       !r.Flags.Has(1),
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
			Null:       !r.Flags.Has(1),
		},
		{
			Name:       "ButtonID",
			SchemaName: "button_id",
			Null:       !r.Flags.Has(1),
		},
		{
			Name:       "URL",
			SchemaName: "url",
			Null:       !r.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *MessagesRequestURLAuthRequest) SetFlags() {
	if !(r.Peer == nil) {
		r.Flags.Set(1)
	}
	if !(r.MsgID == 0) {
		r.Flags.Set(1)
	}
	if !(r.ButtonID == 0) {
		r.Flags.Set(1)
	}
	if !(r.URL == "") {
		r.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (r *MessagesRequestURLAuthRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.requestUrlAuth#198fb446 as nil")
	}
	b.PutID(MessagesRequestURLAuthRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesRequestURLAuthRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.requestUrlAuth#198fb446 as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.requestUrlAuth#198fb446: field flags: %w", err)
	}
	if r.Flags.Has(1) {
		if r.Peer == nil {
			return fmt.Errorf("unable to encode messages.requestUrlAuth#198fb446: field peer is nil")
		}
		if err := r.Peer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.requestUrlAuth#198fb446: field peer: %w", err)
		}
	}
	if r.Flags.Has(1) {
		b.PutInt(r.MsgID)
	}
	if r.Flags.Has(1) {
		b.PutInt(r.ButtonID)
	}
	if r.Flags.Has(2) {
		b.PutString(r.URL)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *MessagesRequestURLAuthRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.requestUrlAuth#198fb446 to nil")
	}
	if err := b.ConsumeID(MessagesRequestURLAuthRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.requestUrlAuth#198fb446: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *MessagesRequestURLAuthRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.requestUrlAuth#198fb446 to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.requestUrlAuth#198fb446: field flags: %w", err)
		}
	}
	if r.Flags.Has(1) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.requestUrlAuth#198fb446: field peer: %w", err)
		}
		r.Peer = value
	}
	if r.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.requestUrlAuth#198fb446: field msg_id: %w", err)
		}
		r.MsgID = value
	}
	if r.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.requestUrlAuth#198fb446: field button_id: %w", err)
		}
		r.ButtonID = value
	}
	if r.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.requestUrlAuth#198fb446: field url: %w", err)
		}
		r.URL = value
	}
	return nil
}

// SetPeer sets value of Peer conditional field.
func (r *MessagesRequestURLAuthRequest) SetPeer(value InputPeerClass) {
	r.Flags.Set(1)
	r.Peer = value
}

// GetPeer returns value of Peer conditional field and
// boolean which is true if field was set.
func (r *MessagesRequestURLAuthRequest) GetPeer() (value InputPeerClass, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(1) {
		return value, false
	}
	return r.Peer, true
}

// SetMsgID sets value of MsgID conditional field.
func (r *MessagesRequestURLAuthRequest) SetMsgID(value int) {
	r.Flags.Set(1)
	r.MsgID = value
}

// GetMsgID returns value of MsgID conditional field and
// boolean which is true if field was set.
func (r *MessagesRequestURLAuthRequest) GetMsgID() (value int, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(1) {
		return value, false
	}
	return r.MsgID, true
}

// SetButtonID sets value of ButtonID conditional field.
func (r *MessagesRequestURLAuthRequest) SetButtonID(value int) {
	r.Flags.Set(1)
	r.ButtonID = value
}

// GetButtonID returns value of ButtonID conditional field and
// boolean which is true if field was set.
func (r *MessagesRequestURLAuthRequest) GetButtonID() (value int, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(1) {
		return value, false
	}
	return r.ButtonID, true
}

// SetURL sets value of URL conditional field.
func (r *MessagesRequestURLAuthRequest) SetURL(value string) {
	r.Flags.Set(2)
	r.URL = value
}

// GetURL returns value of URL conditional field and
// boolean which is true if field was set.
func (r *MessagesRequestURLAuthRequest) GetURL() (value string, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(2) {
		return value, false
	}
	return r.URL, true
}

// MessagesRequestURLAuth invokes method messages.requestUrlAuth#198fb446 returning error if any.
// Get more info about a Seamless Telegram Login authorization request, for more info
// click here »¹
//
// Links:
//  1. https://core.telegram.org/api/url-authorization
//
// See https://core.telegram.org/method/messages.requestUrlAuth for reference.
func (c *Client) MessagesRequestURLAuth(ctx context.Context, request *MessagesRequestURLAuthRequest) (URLAuthResultClass, error) {
	var result URLAuthResultBox

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