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

// SetPassportElementErrorsRequest represents TL type `setPassportElementErrors#80fa80bf`.
type SetPassportElementErrorsRequest struct {
	// User identifier
	UserID int64
	// The errors
	Errors []InputPassportElementError
}

// SetPassportElementErrorsRequestTypeID is TL type id of SetPassportElementErrorsRequest.
const SetPassportElementErrorsRequestTypeID = 0x80fa80bf

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

func (s *SetPassportElementErrorsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.UserID == 0) {
		return false
	}
	if !(s.Errors == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetPassportElementErrorsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setPassportElementErrors",
		ID:   SetPassportElementErrorsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "Errors",
			SchemaName: "errors",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetPassportElementErrorsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPassportElementErrors#80fa80bf as nil")
	}
	b.PutID(SetPassportElementErrorsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetPassportElementErrorsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPassportElementErrors#80fa80bf as nil")
	}
	b.PutInt53(s.UserID)
	b.PutInt(len(s.Errors))
	for idx, v := range s.Errors {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare setPassportElementErrors#80fa80bf: field errors element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetPassportElementErrorsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPassportElementErrors#80fa80bf to nil")
	}
	if err := b.ConsumeID(SetPassportElementErrorsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setPassportElementErrors#80fa80bf: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetPassportElementErrorsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPassportElementErrors#80fa80bf to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setPassportElementErrors#80fa80bf: field user_id: %w", err)
		}
		s.UserID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode setPassportElementErrors#80fa80bf: field errors: %w", err)
		}

		if headerLen > 0 {
			s.Errors = make([]InputPassportElementError, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value InputPassportElementError
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare setPassportElementErrors#80fa80bf: field errors: %w", err)
			}
			s.Errors = append(s.Errors, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetPassportElementErrorsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setPassportElementErrors#80fa80bf as nil")
	}
	b.ObjStart()
	b.PutID("setPassportElementErrors")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(s.UserID)
	b.Comma()
	b.FieldStart("errors")
	b.ArrStart()
	for idx, v := range s.Errors {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode setPassportElementErrors#80fa80bf: field errors element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetPassportElementErrorsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setPassportElementErrors#80fa80bf to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setPassportElementErrors"); err != nil {
				return fmt.Errorf("unable to decode setPassportElementErrors#80fa80bf: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setPassportElementErrors#80fa80bf: field user_id: %w", err)
			}
			s.UserID = value
		case "errors":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value InputPassportElementError
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode setPassportElementErrors#80fa80bf: field errors: %w", err)
				}
				s.Errors = append(s.Errors, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode setPassportElementErrors#80fa80bf: field errors: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (s *SetPassportElementErrorsRequest) GetUserID() (value int64) {
	if s == nil {
		return
	}
	return s.UserID
}

// GetErrors returns value of Errors field.
func (s *SetPassportElementErrorsRequest) GetErrors() (value []InputPassportElementError) {
	if s == nil {
		return
	}
	return s.Errors
}

// SetPassportElementErrors invokes method setPassportElementErrors#80fa80bf returning error if any.
func (c *Client) SetPassportElementErrors(ctx context.Context, request *SetPassportElementErrorsRequest) error {
	var ok Ok

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