// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// BotVerifierSettings represents TL type `botVerifierSettings#b0cd6617`.
// Info about the current verifier bot »¹.
//
// Links:
//  1. https://core.telegram.org/api/bots/verification
//
// See https://core.telegram.org/constructor/botVerifierSettings for reference.
type BotVerifierSettings struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Indicates whether the bot is allowed to set a custom description field for individual
	// verified peers, different from the custom_description provided here.
	CanModifyCustomDescription bool
	// Verification icon
	Icon int64
	// The name of the organization that provides the verification
	Company string
	// An optional default description for the verification
	//
	// Use SetCustomDescription and GetCustomDescription helpers.
	CustomDescription string
}

// BotVerifierSettingsTypeID is TL type id of BotVerifierSettings.
const BotVerifierSettingsTypeID = 0xb0cd6617

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

func (b *BotVerifierSettings) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.CanModifyCustomDescription == false) {
		return false
	}
	if !(b.Icon == 0) {
		return false
	}
	if !(b.Company == "") {
		return false
	}
	if !(b.CustomDescription == "") {
		return false
	}

	return true
}

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

// FillFrom fills BotVerifierSettings from given interface.
func (b *BotVerifierSettings) FillFrom(from interface {
	GetCanModifyCustomDescription() (value bool)
	GetIcon() (value int64)
	GetCompany() (value string)
	GetCustomDescription() (value string, ok bool)
}) {
	b.CanModifyCustomDescription = from.GetCanModifyCustomDescription()
	b.Icon = from.GetIcon()
	b.Company = from.GetCompany()
	if val, ok := from.GetCustomDescription(); ok {
		b.CustomDescription = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (b *BotVerifierSettings) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botVerifierSettings",
		ID:   BotVerifierSettingsTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CanModifyCustomDescription",
			SchemaName: "can_modify_custom_description",
			Null:       !b.Flags.Has(1),
		},
		{
			Name:       "Icon",
			SchemaName: "icon",
		},
		{
			Name:       "Company",
			SchemaName: "company",
		},
		{
			Name:       "CustomDescription",
			SchemaName: "custom_description",
			Null:       !b.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *BotVerifierSettings) SetFlags() {
	if !(b.CanModifyCustomDescription == false) {
		b.Flags.Set(1)
	}
	if !(b.CustomDescription == "") {
		b.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (b *BotVerifierSettings) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botVerifierSettings#b0cd6617 as nil")
	}
	buf.PutID(BotVerifierSettingsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotVerifierSettings) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botVerifierSettings#b0cd6617 as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode botVerifierSettings#b0cd6617: field flags: %w", err)
	}
	buf.PutLong(b.Icon)
	buf.PutString(b.Company)
	if b.Flags.Has(0) {
		buf.PutString(b.CustomDescription)
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BotVerifierSettings) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botVerifierSettings#b0cd6617 to nil")
	}
	if err := buf.ConsumeID(BotVerifierSettingsTypeID); err != nil {
		return fmt.Errorf("unable to decode botVerifierSettings#b0cd6617: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotVerifierSettings) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botVerifierSettings#b0cd6617 to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode botVerifierSettings#b0cd6617: field flags: %w", err)
		}
	}
	b.CanModifyCustomDescription = b.Flags.Has(1)
	{
		value, err := buf.Long()
		if err != nil {
			return fmt.Errorf("unable to decode botVerifierSettings#b0cd6617: field icon: %w", err)
		}
		b.Icon = value
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode botVerifierSettings#b0cd6617: field company: %w", err)
		}
		b.Company = value
	}
	if b.Flags.Has(0) {
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode botVerifierSettings#b0cd6617: field custom_description: %w", err)
		}
		b.CustomDescription = value
	}
	return nil
}

// SetCanModifyCustomDescription sets value of CanModifyCustomDescription conditional field.
func (b *BotVerifierSettings) SetCanModifyCustomDescription(value bool) {
	if value {
		b.Flags.Set(1)
		b.CanModifyCustomDescription = true
	} else {
		b.Flags.Unset(1)
		b.CanModifyCustomDescription = false
	}
}

// GetCanModifyCustomDescription returns value of CanModifyCustomDescription conditional field.
func (b *BotVerifierSettings) GetCanModifyCustomDescription() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(1)
}

// GetIcon returns value of Icon field.
func (b *BotVerifierSettings) GetIcon() (value int64) {
	if b == nil {
		return
	}
	return b.Icon
}

// GetCompany returns value of Company field.
func (b *BotVerifierSettings) GetCompany() (value string) {
	if b == nil {
		return
	}
	return b.Company
}

// SetCustomDescription sets value of CustomDescription conditional field.
func (b *BotVerifierSettings) SetCustomDescription(value string) {
	b.Flags.Set(0)
	b.CustomDescription = value
}

// GetCustomDescription returns value of CustomDescription conditional field and
// boolean which is true if field was set.
func (b *BotVerifierSettings) GetCustomDescription() (value string, ok bool) {
	if b == nil {
		return
	}
	if !b.Flags.Has(0) {
		return value, false
	}
	return b.CustomDescription, true
}
