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

// ToggleSupergroupIsAllHistoryAvailableRequest represents TL type `toggleSupergroupIsAllHistoryAvailable#44d9964e`.
type ToggleSupergroupIsAllHistoryAvailableRequest struct {
	// The identifier of the supergroup
	SupergroupID int64
	// The new value of is_all_history_available
	IsAllHistoryAvailable bool
}

// ToggleSupergroupIsAllHistoryAvailableRequestTypeID is TL type id of ToggleSupergroupIsAllHistoryAvailableRequest.
const ToggleSupergroupIsAllHistoryAvailableRequestTypeID = 0x44d9964e

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (t *ToggleSupergroupIsAllHistoryAvailableRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupIsAllHistoryAvailable#44d9964e as nil")
	}
	b.PutID(ToggleSupergroupIsAllHistoryAvailableRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleSupergroupIsAllHistoryAvailableRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupIsAllHistoryAvailable#44d9964e as nil")
	}
	b.PutInt53(t.SupergroupID)
	b.PutBool(t.IsAllHistoryAvailable)
	return nil
}

// Decode implements bin.Decoder.
func (t *ToggleSupergroupIsAllHistoryAvailableRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupIsAllHistoryAvailable#44d9964e to nil")
	}
	if err := b.ConsumeID(ToggleSupergroupIsAllHistoryAvailableRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode toggleSupergroupIsAllHistoryAvailable#44d9964e: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ToggleSupergroupIsAllHistoryAvailableRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupIsAllHistoryAvailable#44d9964e to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupIsAllHistoryAvailable#44d9964e: field supergroup_id: %w", err)
		}
		t.SupergroupID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupIsAllHistoryAvailable#44d9964e: field is_all_history_available: %w", err)
		}
		t.IsAllHistoryAvailable = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleSupergroupIsAllHistoryAvailableRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupIsAllHistoryAvailable#44d9964e as nil")
	}
	b.ObjStart()
	b.PutID("toggleSupergroupIsAllHistoryAvailable")
	b.Comma()
	b.FieldStart("supergroup_id")
	b.PutInt53(t.SupergroupID)
	b.Comma()
	b.FieldStart("is_all_history_available")
	b.PutBool(t.IsAllHistoryAvailable)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *ToggleSupergroupIsAllHistoryAvailableRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupIsAllHistoryAvailable#44d9964e to nil")
	}

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

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

// GetIsAllHistoryAvailable returns value of IsAllHistoryAvailable field.
func (t *ToggleSupergroupIsAllHistoryAvailableRequest) GetIsAllHistoryAvailable() (value bool) {
	if t == nil {
		return
	}
	return t.IsAllHistoryAvailable
}

// ToggleSupergroupIsAllHistoryAvailable invokes method toggleSupergroupIsAllHistoryAvailable#44d9964e returning error if any.
func (c *Client) ToggleSupergroupIsAllHistoryAvailable(ctx context.Context, request *ToggleSupergroupIsAllHistoryAvailableRequest) error {
	var ok Ok

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