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

// ConfirmSessionRequest represents TL type `confirmSession#d7c9b41f`.
type ConfirmSessionRequest struct {
	// Session identifier
	SessionID int64
}

// ConfirmSessionRequestTypeID is TL type id of ConfirmSessionRequest.
const ConfirmSessionRequestTypeID = 0xd7c9b41f

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ConfirmSessionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "confirmSession",
		ID:   ConfirmSessionRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SessionID",
			SchemaName: "session_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ConfirmSessionRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode confirmSession#d7c9b41f as nil")
	}
	b.PutID(ConfirmSessionRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ConfirmSessionRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode confirmSession#d7c9b41f as nil")
	}
	b.PutLong(c.SessionID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ConfirmSessionRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode confirmSession#d7c9b41f to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode confirmSession#d7c9b41f: field session_id: %w", err)
		}
		c.SessionID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ConfirmSessionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode confirmSession#d7c9b41f as nil")
	}
	b.ObjStart()
	b.PutID("confirmSession")
	b.Comma()
	b.FieldStart("session_id")
	b.PutLong(c.SessionID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetSessionID returns value of SessionID field.
func (c *ConfirmSessionRequest) GetSessionID() (value int64) {
	if c == nil {
		return
	}
	return c.SessionID
}

// ConfirmSession invokes method confirmSession#d7c9b41f returning error if any.
func (c *Client) ConfirmSession(ctx context.Context, sessionid int64) error {
	var ok Ok

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