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

// ToggleSupergroupIsBroadcastGroupRequest represents TL type `toggleSupergroupIsBroadcastGroup#34b22215`.
type ToggleSupergroupIsBroadcastGroupRequest struct {
	// Identifier of the supergroup
	SupergroupID int64
}

// ToggleSupergroupIsBroadcastGroupRequestTypeID is TL type id of ToggleSupergroupIsBroadcastGroupRequest.
const ToggleSupergroupIsBroadcastGroupRequestTypeID = 0x34b22215

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

func (t *ToggleSupergroupIsBroadcastGroupRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.SupergroupID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *ToggleSupergroupIsBroadcastGroupRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "toggleSupergroupIsBroadcastGroup",
		ID:   ToggleSupergroupIsBroadcastGroupRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SupergroupID",
			SchemaName: "supergroup_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ToggleSupergroupIsBroadcastGroupRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupIsBroadcastGroup#34b22215 as nil")
	}
	b.PutID(ToggleSupergroupIsBroadcastGroupRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleSupergroupIsBroadcastGroupRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupIsBroadcastGroup#34b22215 as nil")
	}
	b.PutInt53(t.SupergroupID)
	return nil
}

// Decode implements bin.Decoder.
func (t *ToggleSupergroupIsBroadcastGroupRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupIsBroadcastGroup#34b22215 to nil")
	}
	if err := b.ConsumeID(ToggleSupergroupIsBroadcastGroupRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode toggleSupergroupIsBroadcastGroup#34b22215: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ToggleSupergroupIsBroadcastGroupRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupIsBroadcastGroup#34b22215 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupIsBroadcastGroup#34b22215: field supergroup_id: %w", err)
		}
		t.SupergroupID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleSupergroupIsBroadcastGroupRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupIsBroadcastGroup#34b22215 as nil")
	}
	b.ObjStart()
	b.PutID("toggleSupergroupIsBroadcastGroup")
	b.Comma()
	b.FieldStart("supergroup_id")
	b.PutInt53(t.SupergroupID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *ToggleSupergroupIsBroadcastGroupRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupIsBroadcastGroup#34b22215 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("toggleSupergroupIsBroadcastGroup"); err != nil {
				return fmt.Errorf("unable to decode toggleSupergroupIsBroadcastGroup#34b22215: %w", err)
			}
		case "supergroup_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode toggleSupergroupIsBroadcastGroup#34b22215: field supergroup_id: %w", err)
			}
			t.SupergroupID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSupergroupID returns value of SupergroupID field.
func (t *ToggleSupergroupIsBroadcastGroupRequest) GetSupergroupID() (value int64) {
	if t == nil {
		return
	}
	return t.SupergroupID
}

// ToggleSupergroupIsBroadcastGroup invokes method toggleSupergroupIsBroadcastGroup#34b22215 returning error if any.
func (c *Client) ToggleSupergroupIsBroadcastGroup(ctx context.Context, supergroupid int64) error {
	var ok Ok

	request := &ToggleSupergroupIsBroadcastGroupRequest{
		SupergroupID: supergroupid,
	}
	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
