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

// ToggleSupergroupJoinByRequestRequest represents TL type `toggleSupergroupJoinByRequest#7ddf9fde`.
type ToggleSupergroupJoinByRequestRequest struct {
	// Identifier of the supergroup that isn't a broadcast group
	SupergroupID int64
	// New value of join_by_request
	JoinByRequest bool
}

// ToggleSupergroupJoinByRequestRequestTypeID is TL type id of ToggleSupergroupJoinByRequestRequest.
const ToggleSupergroupJoinByRequestRequestTypeID = 0x7ddf9fde

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (t *ToggleSupergroupJoinByRequestRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupJoinByRequest#7ddf9fde as nil")
	}
	b.PutID(ToggleSupergroupJoinByRequestRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleSupergroupJoinByRequestRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupJoinByRequest#7ddf9fde as nil")
	}
	b.PutInt53(t.SupergroupID)
	b.PutBool(t.JoinByRequest)
	return nil
}

// Decode implements bin.Decoder.
func (t *ToggleSupergroupJoinByRequestRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupJoinByRequest#7ddf9fde to nil")
	}
	if err := b.ConsumeID(ToggleSupergroupJoinByRequestRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode toggleSupergroupJoinByRequest#7ddf9fde: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ToggleSupergroupJoinByRequestRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupJoinByRequest#7ddf9fde to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupJoinByRequest#7ddf9fde: field supergroup_id: %w", err)
		}
		t.SupergroupID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupJoinByRequest#7ddf9fde: field join_by_request: %w", err)
		}
		t.JoinByRequest = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleSupergroupJoinByRequestRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupJoinByRequest#7ddf9fde as nil")
	}
	b.ObjStart()
	b.PutID("toggleSupergroupJoinByRequest")
	b.Comma()
	b.FieldStart("supergroup_id")
	b.PutInt53(t.SupergroupID)
	b.Comma()
	b.FieldStart("join_by_request")
	b.PutBool(t.JoinByRequest)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *ToggleSupergroupJoinByRequestRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupJoinByRequest#7ddf9fde to nil")
	}

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

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

// GetJoinByRequest returns value of JoinByRequest field.
func (t *ToggleSupergroupJoinByRequestRequest) GetJoinByRequest() (value bool) {
	if t == nil {
		return
	}
	return t.JoinByRequest
}

// ToggleSupergroupJoinByRequest invokes method toggleSupergroupJoinByRequest#7ddf9fde returning error if any.
func (c *Client) ToggleSupergroupJoinByRequest(ctx context.Context, request *ToggleSupergroupJoinByRequestRequest) error {
	var ok Ok

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