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

// CountryInfo represents TL type `countryInfo#d9936dff`.
type CountryInfo struct {
	// A two-letter ISO 3166-1 alpha-2 country code
	CountryCode string
	// Native name of the country
	Name string
	// English name of the country
	EnglishName string
	// True, if the country must be hidden from the list of all countries
	IsHidden bool
	// List of country calling codes
	CallingCodes []string
}

// CountryInfoTypeID is TL type id of CountryInfo.
const CountryInfoTypeID = 0xd9936dff

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

func (c *CountryInfo) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.CountryCode == "") {
		return false
	}
	if !(c.Name == "") {
		return false
	}
	if !(c.EnglishName == "") {
		return false
	}
	if !(c.IsHidden == false) {
		return false
	}
	if !(c.CallingCodes == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CountryInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "countryInfo",
		ID:   CountryInfoTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CountryCode",
			SchemaName: "country_code",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "EnglishName",
			SchemaName: "english_name",
		},
		{
			Name:       "IsHidden",
			SchemaName: "is_hidden",
		},
		{
			Name:       "CallingCodes",
			SchemaName: "calling_codes",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CountryInfo) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode countryInfo#d9936dff as nil")
	}
	b.PutID(CountryInfoTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CountryInfo) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode countryInfo#d9936dff as nil")
	}
	b.PutString(c.CountryCode)
	b.PutString(c.Name)
	b.PutString(c.EnglishName)
	b.PutBool(c.IsHidden)
	b.PutInt(len(c.CallingCodes))
	for _, v := range c.CallingCodes {
		b.PutString(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CountryInfo) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode countryInfo#d9936dff to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode countryInfo#d9936dff: field country_code: %w", err)
		}
		c.CountryCode = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode countryInfo#d9936dff: field name: %w", err)
		}
		c.Name = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode countryInfo#d9936dff: field english_name: %w", err)
		}
		c.EnglishName = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode countryInfo#d9936dff: field is_hidden: %w", err)
		}
		c.IsHidden = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode countryInfo#d9936dff: field calling_codes: %w", err)
		}

		if headerLen > 0 {
			c.CallingCodes = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode countryInfo#d9936dff: field calling_codes: %w", err)
			}
			c.CallingCodes = append(c.CallingCodes, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CountryInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode countryInfo#d9936dff as nil")
	}
	b.ObjStart()
	b.PutID("countryInfo")
	b.Comma()
	b.FieldStart("country_code")
	b.PutString(c.CountryCode)
	b.Comma()
	b.FieldStart("name")
	b.PutString(c.Name)
	b.Comma()
	b.FieldStart("english_name")
	b.PutString(c.EnglishName)
	b.Comma()
	b.FieldStart("is_hidden")
	b.PutBool(c.IsHidden)
	b.Comma()
	b.FieldStart("calling_codes")
	b.ArrStart()
	for _, v := range c.CallingCodes {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("countryInfo"); err != nil {
				return fmt.Errorf("unable to decode countryInfo#d9936dff: %w", err)
			}
		case "country_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode countryInfo#d9936dff: field country_code: %w", err)
			}
			c.CountryCode = value
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode countryInfo#d9936dff: field name: %w", err)
			}
			c.Name = value
		case "english_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode countryInfo#d9936dff: field english_name: %w", err)
			}
			c.EnglishName = value
		case "is_hidden":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode countryInfo#d9936dff: field is_hidden: %w", err)
			}
			c.IsHidden = value
		case "calling_codes":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.String()
				if err != nil {
					return fmt.Errorf("unable to decode countryInfo#d9936dff: field calling_codes: %w", err)
				}
				c.CallingCodes = append(c.CallingCodes, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode countryInfo#d9936dff: field calling_codes: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCountryCode returns value of CountryCode field.
func (c *CountryInfo) GetCountryCode() (value string) {
	if c == nil {
		return
	}
	return c.CountryCode
}

// GetName returns value of Name field.
func (c *CountryInfo) GetName() (value string) {
	if c == nil {
		return
	}
	return c.Name
}

// GetEnglishName returns value of EnglishName field.
func (c *CountryInfo) GetEnglishName() (value string) {
	if c == nil {
		return
	}
	return c.EnglishName
}

// GetIsHidden returns value of IsHidden field.
func (c *CountryInfo) GetIsHidden() (value bool) {
	if c == nil {
		return
	}
	return c.IsHidden
}

// GetCallingCodes returns value of CallingCodes field.
func (c *CountryInfo) GetCallingCodes() (value []string) {
	if c == nil {
		return
	}
	return c.CallingCodes
}
