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

// CloseWebAppRequest represents TL type `closeWebApp#68a124c6`.
type CloseWebAppRequest struct {
	// Identifier of Web App launch, received from openWebApp
	WebAppLaunchID int64
}

// CloseWebAppRequestTypeID is TL type id of CloseWebAppRequest.
const CloseWebAppRequestTypeID = 0x68a124c6

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

func (c *CloseWebAppRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.WebAppLaunchID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CloseWebAppRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "closeWebApp",
		ID:   CloseWebAppRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "WebAppLaunchID",
			SchemaName: "web_app_launch_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CloseWebAppRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode closeWebApp#68a124c6 as nil")
	}
	b.PutID(CloseWebAppRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CloseWebAppRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode closeWebApp#68a124c6 as nil")
	}
	b.PutLong(c.WebAppLaunchID)
	return nil
}

// Decode implements bin.Decoder.
func (c *CloseWebAppRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode closeWebApp#68a124c6 to nil")
	}
	if err := b.ConsumeID(CloseWebAppRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode closeWebApp#68a124c6: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CloseWebAppRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode closeWebApp#68a124c6 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode closeWebApp#68a124c6: field web_app_launch_id: %w", err)
		}
		c.WebAppLaunchID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CloseWebAppRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode closeWebApp#68a124c6 as nil")
	}
	b.ObjStart()
	b.PutID("closeWebApp")
	b.Comma()
	b.FieldStart("web_app_launch_id")
	b.PutLong(c.WebAppLaunchID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CloseWebAppRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode closeWebApp#68a124c6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("closeWebApp"); err != nil {
				return fmt.Errorf("unable to decode closeWebApp#68a124c6: %w", err)
			}
		case "web_app_launch_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode closeWebApp#68a124c6: field web_app_launch_id: %w", err)
			}
			c.WebAppLaunchID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetWebAppLaunchID returns value of WebAppLaunchID field.
func (c *CloseWebAppRequest) GetWebAppLaunchID() (value int64) {
	if c == nil {
		return
	}
	return c.WebAppLaunchID
}

// CloseWebApp invokes method closeWebApp#68a124c6 returning error if any.
func (c *Client) CloseWebApp(ctx context.Context, webapplaunchid int64) error {
	var ok Ok

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