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

// TargetChatTypes represents TL type `targetChatTypes#5a300e51`.
type TargetChatTypes struct {
	// True, if private chats with ordinary users are allowed
	AllowUserChats bool
	// True, if private chats with other bots are allowed
	AllowBotChats bool
	// True, if basic group and supergroup chats are allowed
	AllowGroupChats bool
	// True, if channel chats are allowed
	AllowChannelChats bool
}

// TargetChatTypesTypeID is TL type id of TargetChatTypes.
const TargetChatTypesTypeID = 0x5a300e51

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

func (t *TargetChatTypes) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.AllowUserChats == false) {
		return false
	}
	if !(t.AllowBotChats == false) {
		return false
	}
	if !(t.AllowGroupChats == false) {
		return false
	}
	if !(t.AllowChannelChats == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TargetChatTypes) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "targetChatTypes",
		ID:   TargetChatTypesTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "AllowUserChats",
			SchemaName: "allow_user_chats",
		},
		{
			Name:       "AllowBotChats",
			SchemaName: "allow_bot_chats",
		},
		{
			Name:       "AllowGroupChats",
			SchemaName: "allow_group_chats",
		},
		{
			Name:       "AllowChannelChats",
			SchemaName: "allow_channel_chats",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TargetChatTypes) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatTypes#5a300e51 as nil")
	}
	b.PutID(TargetChatTypesTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TargetChatTypes) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatTypes#5a300e51 as nil")
	}
	b.PutBool(t.AllowUserChats)
	b.PutBool(t.AllowBotChats)
	b.PutBool(t.AllowGroupChats)
	b.PutBool(t.AllowChannelChats)
	return nil
}

// Decode implements bin.Decoder.
func (t *TargetChatTypes) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode targetChatTypes#5a300e51 to nil")
	}
	if err := b.ConsumeID(TargetChatTypesTypeID); err != nil {
		return fmt.Errorf("unable to decode targetChatTypes#5a300e51: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TargetChatTypes) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode targetChatTypes#5a300e51 to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_user_chats: %w", err)
		}
		t.AllowUserChats = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_bot_chats: %w", err)
		}
		t.AllowBotChats = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_group_chats: %w", err)
		}
		t.AllowGroupChats = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_channel_chats: %w", err)
		}
		t.AllowChannelChats = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TargetChatTypes) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatTypes#5a300e51 as nil")
	}
	b.ObjStart()
	b.PutID("targetChatTypes")
	b.Comma()
	b.FieldStart("allow_user_chats")
	b.PutBool(t.AllowUserChats)
	b.Comma()
	b.FieldStart("allow_bot_chats")
	b.PutBool(t.AllowBotChats)
	b.Comma()
	b.FieldStart("allow_group_chats")
	b.PutBool(t.AllowGroupChats)
	b.Comma()
	b.FieldStart("allow_channel_chats")
	b.PutBool(t.AllowChannelChats)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TargetChatTypes) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode targetChatTypes#5a300e51 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("targetChatTypes"); err != nil {
				return fmt.Errorf("unable to decode targetChatTypes#5a300e51: %w", err)
			}
		case "allow_user_chats":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_user_chats: %w", err)
			}
			t.AllowUserChats = value
		case "allow_bot_chats":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_bot_chats: %w", err)
			}
			t.AllowBotChats = value
		case "allow_group_chats":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_group_chats: %w", err)
			}
			t.AllowGroupChats = value
		case "allow_channel_chats":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode targetChatTypes#5a300e51: field allow_channel_chats: %w", err)
			}
			t.AllowChannelChats = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetAllowUserChats returns value of AllowUserChats field.
func (t *TargetChatTypes) GetAllowUserChats() (value bool) {
	if t == nil {
		return
	}
	return t.AllowUserChats
}

// GetAllowBotChats returns value of AllowBotChats field.
func (t *TargetChatTypes) GetAllowBotChats() (value bool) {
	if t == nil {
		return
	}
	return t.AllowBotChats
}

// GetAllowGroupChats returns value of AllowGroupChats field.
func (t *TargetChatTypes) GetAllowGroupChats() (value bool) {
	if t == nil {
		return
	}
	return t.AllowGroupChats
}

// GetAllowChannelChats returns value of AllowChannelChats field.
func (t *TargetChatTypes) GetAllowChannelChats() (value bool) {
	if t == nil {
		return
	}
	return t.AllowChannelChats
}
