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

// GetMainWebAppRequest represents TL type `getMainWebApp#23687ca6`.
type GetMainWebAppRequest struct {
	// Identifier of the chat in which the Web App is opened; pass 0 if none
	ChatID int64
	// Identifier of the target bot. If the bot is restricted for the current user, then show
	// an error instead of calling the method
	BotUserID int64
	// Start parameter from internalLinkTypeMainWebApp
	StartParameter string
	// Parameters to use to open the Web App
	Parameters WebAppOpenParameters
}

// GetMainWebAppRequestTypeID is TL type id of GetMainWebAppRequest.
const GetMainWebAppRequestTypeID = 0x23687ca6

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

func (g *GetMainWebAppRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatID == 0) {
		return false
	}
	if !(g.BotUserID == 0) {
		return false
	}
	if !(g.StartParameter == "") {
		return false
	}
	if !(g.Parameters.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetMainWebAppRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getMainWebApp",
		ID:   GetMainWebAppRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "StartParameter",
			SchemaName: "start_parameter",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetMainWebAppRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getMainWebApp#23687ca6 as nil")
	}
	b.PutID(GetMainWebAppRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetMainWebAppRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getMainWebApp#23687ca6 as nil")
	}
	b.PutInt53(g.ChatID)
	b.PutInt53(g.BotUserID)
	b.PutString(g.StartParameter)
	if err := g.Parameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getMainWebApp#23687ca6: field parameters: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GetMainWebAppRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getMainWebApp#23687ca6 to nil")
	}
	if err := b.ConsumeID(GetMainWebAppRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getMainWebApp#23687ca6: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetMainWebAppRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getMainWebApp#23687ca6 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field bot_user_id: %w", err)
		}
		g.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field start_parameter: %w", err)
		}
		g.StartParameter = value
	}
	{
		if err := g.Parameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field parameters: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetMainWebAppRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getMainWebApp#23687ca6 as nil")
	}
	b.ObjStart()
	b.PutID("getMainWebApp")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.FieldStart("start_parameter")
	b.PutString(g.StartParameter)
	b.Comma()
	b.FieldStart("parameters")
	if err := g.Parameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getMainWebApp#23687ca6: field parameters: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetMainWebAppRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getMainWebApp#23687ca6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getMainWebApp"); err != nil {
				return fmt.Errorf("unable to decode getMainWebApp#23687ca6: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field chat_id: %w", err)
			}
			g.ChatID = value
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field bot_user_id: %w", err)
			}
			g.BotUserID = value
		case "start_parameter":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field start_parameter: %w", err)
			}
			g.StartParameter = value
		case "parameters":
			if err := g.Parameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode getMainWebApp#23687ca6: field parameters: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (g *GetMainWebAppRequest) GetChatID() (value int64) {
	if g == nil {
		return
	}
	return g.ChatID
}

// GetBotUserID returns value of BotUserID field.
func (g *GetMainWebAppRequest) GetBotUserID() (value int64) {
	if g == nil {
		return
	}
	return g.BotUserID
}

// GetStartParameter returns value of StartParameter field.
func (g *GetMainWebAppRequest) GetStartParameter() (value string) {
	if g == nil {
		return
	}
	return g.StartParameter
}

// GetParameters returns value of Parameters field.
func (g *GetMainWebAppRequest) GetParameters() (value WebAppOpenParameters) {
	if g == nil {
		return
	}
	return g.Parameters
}

// GetMainWebApp invokes method getMainWebApp#23687ca6 returning error if any.
func (c *Client) GetMainWebApp(ctx context.Context, request *GetMainWebAppRequest) (*MainWebApp, error) {
	var result MainWebApp

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