// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// ChannelsToggleSignaturesRequest represents TL type `channels.toggleSignatures#418d549c`.
// Enable/disable message signatures in channels
//
// See https://core.telegram.org/method/channels.toggleSignatures for reference.
type ChannelsToggleSignaturesRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, enables message signatures.
	SignaturesEnabled bool
	// If set, messages from channel admins will link to their profiles, just like for group
	// messages: can only be set if the signatures_enabled flag is set.
	ProfilesEnabled bool
	// Channel
	Channel InputChannelClass
}

// ChannelsToggleSignaturesRequestTypeID is TL type id of ChannelsToggleSignaturesRequest.
const ChannelsToggleSignaturesRequestTypeID = 0x418d549c

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

func (t *ChannelsToggleSignaturesRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Flags.Zero()) {
		return false
	}
	if !(t.SignaturesEnabled == false) {
		return false
	}
	if !(t.ProfilesEnabled == false) {
		return false
	}
	if !(t.Channel == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsToggleSignaturesRequest from given interface.
func (t *ChannelsToggleSignaturesRequest) FillFrom(from interface {
	GetSignaturesEnabled() (value bool)
	GetProfilesEnabled() (value bool)
	GetChannel() (value InputChannelClass)
}) {
	t.SignaturesEnabled = from.GetSignaturesEnabled()
	t.ProfilesEnabled = from.GetProfilesEnabled()
	t.Channel = from.GetChannel()
}

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

// TypeName returns name of type in TL schema.
func (*ChannelsToggleSignaturesRequest) TypeName() string {
	return "channels.toggleSignatures"
}

// TypeInfo returns info about TL type.
func (t *ChannelsToggleSignaturesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.toggleSignatures",
		ID:   ChannelsToggleSignaturesRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SignaturesEnabled",
			SchemaName: "signatures_enabled",
			Null:       !t.Flags.Has(0),
		},
		{
			Name:       "ProfilesEnabled",
			SchemaName: "profiles_enabled",
			Null:       !t.Flags.Has(1),
		},
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (t *ChannelsToggleSignaturesRequest) SetFlags() {
	if !(t.SignaturesEnabled == false) {
		t.Flags.Set(0)
	}
	if !(t.ProfilesEnabled == false) {
		t.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (t *ChannelsToggleSignaturesRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleSignatures#418d549c as nil")
	}
	b.PutID(ChannelsToggleSignaturesRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ChannelsToggleSignaturesRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleSignatures#418d549c as nil")
	}
	t.SetFlags()
	if err := t.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.toggleSignatures#418d549c: field flags: %w", err)
	}
	if t.Channel == nil {
		return fmt.Errorf("unable to encode channels.toggleSignatures#418d549c: field channel is nil")
	}
	if err := t.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.toggleSignatures#418d549c: field channel: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *ChannelsToggleSignaturesRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleSignatures#418d549c to nil")
	}
	if err := b.ConsumeID(ChannelsToggleSignaturesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.toggleSignatures#418d549c: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ChannelsToggleSignaturesRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleSignatures#418d549c to nil")
	}
	{
		if err := t.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode channels.toggleSignatures#418d549c: field flags: %w", err)
		}
	}
	t.SignaturesEnabled = t.Flags.Has(0)
	t.ProfilesEnabled = t.Flags.Has(1)
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleSignatures#418d549c: field channel: %w", err)
		}
		t.Channel = value
	}
	return nil
}

// SetSignaturesEnabled sets value of SignaturesEnabled conditional field.
func (t *ChannelsToggleSignaturesRequest) SetSignaturesEnabled(value bool) {
	if value {
		t.Flags.Set(0)
		t.SignaturesEnabled = true
	} else {
		t.Flags.Unset(0)
		t.SignaturesEnabled = false
	}
}

// GetSignaturesEnabled returns value of SignaturesEnabled conditional field.
func (t *ChannelsToggleSignaturesRequest) GetSignaturesEnabled() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(0)
}

// SetProfilesEnabled sets value of ProfilesEnabled conditional field.
func (t *ChannelsToggleSignaturesRequest) SetProfilesEnabled(value bool) {
	if value {
		t.Flags.Set(1)
		t.ProfilesEnabled = true
	} else {
		t.Flags.Unset(1)
		t.ProfilesEnabled = false
	}
}

// GetProfilesEnabled returns value of ProfilesEnabled conditional field.
func (t *ChannelsToggleSignaturesRequest) GetProfilesEnabled() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(1)
}

// GetChannel returns value of Channel field.
func (t *ChannelsToggleSignaturesRequest) GetChannel() (value InputChannelClass) {
	if t == nil {
		return
	}
	return t.Channel
}

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (t *ChannelsToggleSignaturesRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return t.Channel.AsNotEmpty()
}

// ChannelsToggleSignatures invokes method channels.toggleSignatures#418d549c returning error if any.
// Enable/disable message signatures in channels
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHAT_ADMIN_REQUIRED: You must be an admin in this chat to do this.
//	400 CHAT_ID_INVALID: The provided chat id is invalid.
//	400 CHAT_NOT_MODIFIED: No changes were made to chat information because the new information you passed is identical to the current information.
//
// See https://core.telegram.org/method/channels.toggleSignatures for reference.
func (c *Client) ChannelsToggleSignatures(ctx context.Context, request *ChannelsToggleSignaturesRequest) (UpdatesClass, error) {
	var result UpdatesBox

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