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

// PassportAuthorizationForm represents TL type `passportAuthorizationForm#3fe28cb0`.
type PassportAuthorizationForm struct {
	// Unique identifier of the authorization form
	ID int32
	// Telegram Passport elements that must be provided to complete the form
	RequiredElements []PassportRequiredElement
	// URL for the privacy policy of the service; may be empty
	PrivacyPolicyURL string
}

// PassportAuthorizationFormTypeID is TL type id of PassportAuthorizationForm.
const PassportAuthorizationFormTypeID = 0x3fe28cb0

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

func (p *PassportAuthorizationForm) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.ID == 0) {
		return false
	}
	if !(p.RequiredElements == nil) {
		return false
	}
	if !(p.PrivacyPolicyURL == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PassportAuthorizationForm) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "passportAuthorizationForm",
		ID:   PassportAuthorizationFormTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "RequiredElements",
			SchemaName: "required_elements",
		},
		{
			Name:       "PrivacyPolicyURL",
			SchemaName: "privacy_policy_url",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PassportAuthorizationForm) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode passportAuthorizationForm#3fe28cb0 as nil")
	}
	b.PutID(PassportAuthorizationFormTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PassportAuthorizationForm) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode passportAuthorizationForm#3fe28cb0 as nil")
	}
	b.PutInt32(p.ID)
	b.PutInt(len(p.RequiredElements))
	for idx, v := range p.RequiredElements {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare passportAuthorizationForm#3fe28cb0: field required_elements element with index %d: %w", idx, err)
		}
	}
	b.PutString(p.PrivacyPolicyURL)
	return nil
}

// Decode implements bin.Decoder.
func (p *PassportAuthorizationForm) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode passportAuthorizationForm#3fe28cb0 to nil")
	}
	if err := b.ConsumeID(PassportAuthorizationFormTypeID); err != nil {
		return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PassportAuthorizationForm) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode passportAuthorizationForm#3fe28cb0 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: field id: %w", err)
		}
		p.ID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: field required_elements: %w", err)
		}

		if headerLen > 0 {
			p.RequiredElements = make([]PassportRequiredElement, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value PassportRequiredElement
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare passportAuthorizationForm#3fe28cb0: field required_elements: %w", err)
			}
			p.RequiredElements = append(p.RequiredElements, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: field privacy_policy_url: %w", err)
		}
		p.PrivacyPolicyURL = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PassportAuthorizationForm) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode passportAuthorizationForm#3fe28cb0 as nil")
	}
	b.ObjStart()
	b.PutID("passportAuthorizationForm")
	b.Comma()
	b.FieldStart("id")
	b.PutInt32(p.ID)
	b.Comma()
	b.FieldStart("required_elements")
	b.ArrStart()
	for idx, v := range p.RequiredElements {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode passportAuthorizationForm#3fe28cb0: field required_elements element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("privacy_policy_url")
	b.PutString(p.PrivacyPolicyURL)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PassportAuthorizationForm) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode passportAuthorizationForm#3fe28cb0 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("passportAuthorizationForm"); err != nil {
				return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: %w", err)
			}
		case "id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: field id: %w", err)
			}
			p.ID = value
		case "required_elements":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value PassportRequiredElement
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: field required_elements: %w", err)
				}
				p.RequiredElements = append(p.RequiredElements, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: field required_elements: %w", err)
			}
		case "privacy_policy_url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode passportAuthorizationForm#3fe28cb0: field privacy_policy_url: %w", err)
			}
			p.PrivacyPolicyURL = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (p *PassportAuthorizationForm) GetID() (value int32) {
	if p == nil {
		return
	}
	return p.ID
}

// GetRequiredElements returns value of RequiredElements field.
func (p *PassportAuthorizationForm) GetRequiredElements() (value []PassportRequiredElement) {
	if p == nil {
		return
	}
	return p.RequiredElements
}

// GetPrivacyPolicyURL returns value of PrivacyPolicyURL field.
func (p *PassportAuthorizationForm) GetPrivacyPolicyURL() (value string) {
	if p == nil {
		return
	}
	return p.PrivacyPolicyURL
}
