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

// CheckPhoneNumberCodeRequest represents TL type `checkPhoneNumberCode#dc0565a1`.
type CheckPhoneNumberCodeRequest struct {
	// Authentication code to check
	Code string
}

// CheckPhoneNumberCodeRequestTypeID is TL type id of CheckPhoneNumberCodeRequest.
const CheckPhoneNumberCodeRequestTypeID = 0xdc0565a1

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

func (c *CheckPhoneNumberCodeRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Code == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CheckPhoneNumberCodeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "checkPhoneNumberCode",
		ID:   CheckPhoneNumberCodeRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Code",
			SchemaName: "code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CheckPhoneNumberCodeRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkPhoneNumberCode#dc0565a1 as nil")
	}
	b.PutID(CheckPhoneNumberCodeRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CheckPhoneNumberCodeRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkPhoneNumberCode#dc0565a1 as nil")
	}
	b.PutString(c.Code)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CheckPhoneNumberCodeRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkPhoneNumberCode#dc0565a1 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode checkPhoneNumberCode#dc0565a1: field code: %w", err)
		}
		c.Code = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CheckPhoneNumberCodeRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode checkPhoneNumberCode#dc0565a1 as nil")
	}
	b.ObjStart()
	b.PutID("checkPhoneNumberCode")
	b.Comma()
	b.FieldStart("code")
	b.PutString(c.Code)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetCode returns value of Code field.
func (c *CheckPhoneNumberCodeRequest) GetCode() (value string) {
	if c == nil {
		return
	}
	return c.Code
}

// CheckPhoneNumberCode invokes method checkPhoneNumberCode#dc0565a1 returning error if any.
func (c *Client) CheckPhoneNumberCode(ctx context.Context, code string) error {
	var ok Ok

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