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

// ToggleHasSponsoredMessagesEnabledRequest represents TL type `toggleHasSponsoredMessagesEnabled#75055cec`.
type ToggleHasSponsoredMessagesEnabledRequest struct {
	// Pass true to enable sponsored messages for the current user; false to disable them
	HasSponsoredMessagesEnabled bool
}

// ToggleHasSponsoredMessagesEnabledRequestTypeID is TL type id of ToggleHasSponsoredMessagesEnabledRequest.
const ToggleHasSponsoredMessagesEnabledRequestTypeID = 0x75055cec

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

func (t *ToggleHasSponsoredMessagesEnabledRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.HasSponsoredMessagesEnabled == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *ToggleHasSponsoredMessagesEnabledRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "toggleHasSponsoredMessagesEnabled",
		ID:   ToggleHasSponsoredMessagesEnabledRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "HasSponsoredMessagesEnabled",
			SchemaName: "has_sponsored_messages_enabled",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ToggleHasSponsoredMessagesEnabledRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleHasSponsoredMessagesEnabled#75055cec as nil")
	}
	b.PutID(ToggleHasSponsoredMessagesEnabledRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleHasSponsoredMessagesEnabledRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleHasSponsoredMessagesEnabled#75055cec as nil")
	}
	b.PutBool(t.HasSponsoredMessagesEnabled)
	return nil
}

// Decode implements bin.Decoder.
func (t *ToggleHasSponsoredMessagesEnabledRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleHasSponsoredMessagesEnabled#75055cec to nil")
	}
	if err := b.ConsumeID(ToggleHasSponsoredMessagesEnabledRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode toggleHasSponsoredMessagesEnabled#75055cec: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ToggleHasSponsoredMessagesEnabledRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleHasSponsoredMessagesEnabled#75055cec to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode toggleHasSponsoredMessagesEnabled#75055cec: field has_sponsored_messages_enabled: %w", err)
		}
		t.HasSponsoredMessagesEnabled = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleHasSponsoredMessagesEnabledRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleHasSponsoredMessagesEnabled#75055cec as nil")
	}
	b.ObjStart()
	b.PutID("toggleHasSponsoredMessagesEnabled")
	b.Comma()
	b.FieldStart("has_sponsored_messages_enabled")
	b.PutBool(t.HasSponsoredMessagesEnabled)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *ToggleHasSponsoredMessagesEnabledRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleHasSponsoredMessagesEnabled#75055cec to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("toggleHasSponsoredMessagesEnabled"); err != nil {
				return fmt.Errorf("unable to decode toggleHasSponsoredMessagesEnabled#75055cec: %w", err)
			}
		case "has_sponsored_messages_enabled":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode toggleHasSponsoredMessagesEnabled#75055cec: field has_sponsored_messages_enabled: %w", err)
			}
			t.HasSponsoredMessagesEnabled = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetHasSponsoredMessagesEnabled returns value of HasSponsoredMessagesEnabled field.
func (t *ToggleHasSponsoredMessagesEnabledRequest) GetHasSponsoredMessagesEnabled() (value bool) {
	if t == nil {
		return
	}
	return t.HasSponsoredMessagesEnabled
}

// ToggleHasSponsoredMessagesEnabled invokes method toggleHasSponsoredMessagesEnabled#75055cec returning error if any.
func (c *Client) ToggleHasSponsoredMessagesEnabled(ctx context.Context, hassponsoredmessagesenabled bool) error {
	var ok Ok

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