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

// PhoneToggleGroupCallSettingsRequest represents TL type `phone.toggleGroupCallSettings#974392f2`.
// Change group call settings
//
// See https://core.telegram.org/method/phone.toggleGroupCallSettings for reference.
type PhoneToggleGroupCallSettingsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Invalidate existing invite links
	ResetInviteHash bool
	// Group call
	Call InputGroupCallClass
	// Whether all users will that join this group call are muted by default upon joining the
	// group call
	//
	// Use SetJoinMuted and GetJoinMuted helpers.
	JoinMuted bool
	// MessagesEnabled field of PhoneToggleGroupCallSettingsRequest.
	//
	// Use SetMessagesEnabled and GetMessagesEnabled helpers.
	MessagesEnabled bool
	// SendPaidMessagesStars field of PhoneToggleGroupCallSettingsRequest.
	//
	// Use SetSendPaidMessagesStars and GetSendPaidMessagesStars helpers.
	SendPaidMessagesStars int64
}

// PhoneToggleGroupCallSettingsRequestTypeID is TL type id of PhoneToggleGroupCallSettingsRequest.
const PhoneToggleGroupCallSettingsRequestTypeID = 0x974392f2

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

func (t *PhoneToggleGroupCallSettingsRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Flags.Zero()) {
		return false
	}
	if !(t.ResetInviteHash == false) {
		return false
	}
	if !(t.Call == nil) {
		return false
	}
	if !(t.JoinMuted == false) {
		return false
	}
	if !(t.MessagesEnabled == false) {
		return false
	}
	if !(t.SendPaidMessagesStars == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneToggleGroupCallSettingsRequest from given interface.
func (t *PhoneToggleGroupCallSettingsRequest) FillFrom(from interface {
	GetResetInviteHash() (value bool)
	GetCall() (value InputGroupCallClass)
	GetJoinMuted() (value bool, ok bool)
	GetMessagesEnabled() (value bool, ok bool)
	GetSendPaidMessagesStars() (value int64, ok bool)
}) {
	t.ResetInviteHash = from.GetResetInviteHash()
	t.Call = from.GetCall()
	if val, ok := from.GetJoinMuted(); ok {
		t.JoinMuted = val
	}

	if val, ok := from.GetMessagesEnabled(); ok {
		t.MessagesEnabled = val
	}

	if val, ok := from.GetSendPaidMessagesStars(); ok {
		t.SendPaidMessagesStars = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*PhoneToggleGroupCallSettingsRequest) TypeName() string {
	return "phone.toggleGroupCallSettings"
}

// TypeInfo returns info about TL type.
func (t *PhoneToggleGroupCallSettingsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.toggleGroupCallSettings",
		ID:   PhoneToggleGroupCallSettingsRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ResetInviteHash",
			SchemaName: "reset_invite_hash",
			Null:       !t.Flags.Has(1),
		},
		{
			Name:       "Call",
			SchemaName: "call",
		},
		{
			Name:       "JoinMuted",
			SchemaName: "join_muted",
			Null:       !t.Flags.Has(0),
		},
		{
			Name:       "MessagesEnabled",
			SchemaName: "messages_enabled",
			Null:       !t.Flags.Has(2),
		},
		{
			Name:       "SendPaidMessagesStars",
			SchemaName: "send_paid_messages_stars",
			Null:       !t.Flags.Has(3),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (t *PhoneToggleGroupCallSettingsRequest) SetFlags() {
	if !(t.ResetInviteHash == false) {
		t.Flags.Set(1)
	}
	if !(t.JoinMuted == false) {
		t.Flags.Set(0)
	}
	if !(t.MessagesEnabled == false) {
		t.Flags.Set(2)
	}
	if !(t.SendPaidMessagesStars == 0) {
		t.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (t *PhoneToggleGroupCallSettingsRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode phone.toggleGroupCallSettings#974392f2 as nil")
	}
	b.PutID(PhoneToggleGroupCallSettingsRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *PhoneToggleGroupCallSettingsRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode phone.toggleGroupCallSettings#974392f2 as nil")
	}
	t.SetFlags()
	if err := t.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.toggleGroupCallSettings#974392f2: field flags: %w", err)
	}
	if t.Call == nil {
		return fmt.Errorf("unable to encode phone.toggleGroupCallSettings#974392f2: field call is nil")
	}
	if err := t.Call.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.toggleGroupCallSettings#974392f2: field call: %w", err)
	}
	if t.Flags.Has(0) {
		b.PutBool(t.JoinMuted)
	}
	if t.Flags.Has(2) {
		b.PutBool(t.MessagesEnabled)
	}
	if t.Flags.Has(3) {
		b.PutLong(t.SendPaidMessagesStars)
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *PhoneToggleGroupCallSettingsRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode phone.toggleGroupCallSettings#974392f2 to nil")
	}
	if err := b.ConsumeID(PhoneToggleGroupCallSettingsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode phone.toggleGroupCallSettings#974392f2: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *PhoneToggleGroupCallSettingsRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode phone.toggleGroupCallSettings#974392f2 to nil")
	}
	{
		if err := t.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode phone.toggleGroupCallSettings#974392f2: field flags: %w", err)
		}
	}
	t.ResetInviteHash = t.Flags.Has(1)
	{
		value, err := DecodeInputGroupCall(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.toggleGroupCallSettings#974392f2: field call: %w", err)
		}
		t.Call = value
	}
	if t.Flags.Has(0) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode phone.toggleGroupCallSettings#974392f2: field join_muted: %w", err)
		}
		t.JoinMuted = value
	}
	if t.Flags.Has(2) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode phone.toggleGroupCallSettings#974392f2: field messages_enabled: %w", err)
		}
		t.MessagesEnabled = value
	}
	if t.Flags.Has(3) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode phone.toggleGroupCallSettings#974392f2: field send_paid_messages_stars: %w", err)
		}
		t.SendPaidMessagesStars = value
	}
	return nil
}

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

// GetResetInviteHash returns value of ResetInviteHash conditional field.
func (t *PhoneToggleGroupCallSettingsRequest) GetResetInviteHash() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(1)
}

// GetCall returns value of Call field.
func (t *PhoneToggleGroupCallSettingsRequest) GetCall() (value InputGroupCallClass) {
	if t == nil {
		return
	}
	return t.Call
}

// SetJoinMuted sets value of JoinMuted conditional field.
func (t *PhoneToggleGroupCallSettingsRequest) SetJoinMuted(value bool) {
	t.Flags.Set(0)
	t.JoinMuted = value
}

// GetJoinMuted returns value of JoinMuted conditional field and
// boolean which is true if field was set.
func (t *PhoneToggleGroupCallSettingsRequest) GetJoinMuted() (value bool, ok bool) {
	if t == nil {
		return
	}
	if !t.Flags.Has(0) {
		return value, false
	}
	return t.JoinMuted, true
}

// SetMessagesEnabled sets value of MessagesEnabled conditional field.
func (t *PhoneToggleGroupCallSettingsRequest) SetMessagesEnabled(value bool) {
	t.Flags.Set(2)
	t.MessagesEnabled = value
}

// GetMessagesEnabled returns value of MessagesEnabled conditional field and
// boolean which is true if field was set.
func (t *PhoneToggleGroupCallSettingsRequest) GetMessagesEnabled() (value bool, ok bool) {
	if t == nil {
		return
	}
	if !t.Flags.Has(2) {
		return value, false
	}
	return t.MessagesEnabled, true
}

// SetSendPaidMessagesStars sets value of SendPaidMessagesStars conditional field.
func (t *PhoneToggleGroupCallSettingsRequest) SetSendPaidMessagesStars(value int64) {
	t.Flags.Set(3)
	t.SendPaidMessagesStars = value
}

// GetSendPaidMessagesStars returns value of SendPaidMessagesStars conditional field and
// boolean which is true if field was set.
func (t *PhoneToggleGroupCallSettingsRequest) GetSendPaidMessagesStars() (value int64, ok bool) {
	if t == nil {
		return
	}
	if !t.Flags.Has(3) {
		return value, false
	}
	return t.SendPaidMessagesStars, true
}

// PhoneToggleGroupCallSettings invokes method phone.toggleGroupCallSettings#974392f2 returning error if any.
// Change group call settings
//
// Possible errors:
//
//	400 GROUPCALL_INVALID: The specified group call is invalid.
//	400 GROUPCALL_NOT_MODIFIED: Group call settings weren't modified.
//
// See https://core.telegram.org/method/phone.toggleGroupCallSettings for reference.
func (c *Client) PhoneToggleGroupCallSettings(ctx context.Context, request *PhoneToggleGroupCallSettingsRequest) (UpdatesClass, error) {
	var result UpdatesBox

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