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

// ImportContactsRequest represents TL type `importContacts#77bdb718`.
type ImportContactsRequest struct {
	// The list of contacts to import or edit; contacts' vCard are ignored and are not
	// imported
	Contacts []Contact
}

// ImportContactsRequestTypeID is TL type id of ImportContactsRequest.
const ImportContactsRequestTypeID = 0x77bdb718

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

func (i *ImportContactsRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Contacts == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *ImportContactsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "importContacts",
		ID:   ImportContactsRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Contacts",
			SchemaName: "contacts",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *ImportContactsRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode importContacts#77bdb718 as nil")
	}
	b.PutID(ImportContactsRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *ImportContactsRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode importContacts#77bdb718 as nil")
	}
	b.PutInt(len(i.Contacts))
	for idx, v := range i.Contacts {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare importContacts#77bdb718: field contacts element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *ImportContactsRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode importContacts#77bdb718 to nil")
	}
	if err := b.ConsumeID(ImportContactsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode importContacts#77bdb718: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *ImportContactsRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode importContacts#77bdb718 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode importContacts#77bdb718: field contacts: %w", err)
		}

		if headerLen > 0 {
			i.Contacts = make([]Contact, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Contact
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare importContacts#77bdb718: field contacts: %w", err)
			}
			i.Contacts = append(i.Contacts, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *ImportContactsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode importContacts#77bdb718 as nil")
	}
	b.ObjStart()
	b.PutID("importContacts")
	b.Comma()
	b.FieldStart("contacts")
	b.ArrStart()
	for idx, v := range i.Contacts {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode importContacts#77bdb718: field contacts 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 (i *ImportContactsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode importContacts#77bdb718 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("importContacts"); err != nil {
				return fmt.Errorf("unable to decode importContacts#77bdb718: %w", err)
			}
		case "contacts":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value Contact
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode importContacts#77bdb718: field contacts: %w", err)
				}
				i.Contacts = append(i.Contacts, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode importContacts#77bdb718: field contacts: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetContacts returns value of Contacts field.
func (i *ImportContactsRequest) GetContacts() (value []Contact) {
	if i == nil {
		return
	}
	return i.Contacts
}

// ImportContacts invokes method importContacts#77bdb718 returning error if any.
func (c *Client) ImportContacts(ctx context.Context, contacts []Contact) (*ImportedContacts, error) {
	var result ImportedContacts

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