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

// PhoneInviteConferenceCallParticipantRequest represents TL type `phone.inviteConferenceCallParticipant#bcf22685`.
// Invite a user to a conference call.
//
// See https://core.telegram.org/method/phone.inviteConferenceCallParticipant for reference.
type PhoneInviteConferenceCallParticipantRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Invite the user to also turn on their video feed.
	Video bool
	// The conference call.
	Call InputGroupCallClass
	// The user to invite.
	UserID InputUserClass
}

// PhoneInviteConferenceCallParticipantRequestTypeID is TL type id of PhoneInviteConferenceCallParticipantRequest.
const PhoneInviteConferenceCallParticipantRequestTypeID = 0xbcf22685

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

func (i *PhoneInviteConferenceCallParticipantRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Flags.Zero()) {
		return false
	}
	if !(i.Video == false) {
		return false
	}
	if !(i.Call == nil) {
		return false
	}
	if !(i.UserID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneInviteConferenceCallParticipantRequest from given interface.
func (i *PhoneInviteConferenceCallParticipantRequest) FillFrom(from interface {
	GetVideo() (value bool)
	GetCall() (value InputGroupCallClass)
	GetUserID() (value InputUserClass)
}) {
	i.Video = from.GetVideo()
	i.Call = from.GetCall()
	i.UserID = from.GetUserID()
}

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

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

// TypeInfo returns info about TL type.
func (i *PhoneInviteConferenceCallParticipantRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.inviteConferenceCallParticipant",
		ID:   PhoneInviteConferenceCallParticipantRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Video",
			SchemaName: "video",
			Null:       !i.Flags.Has(0),
		},
		{
			Name:       "Call",
			SchemaName: "call",
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (i *PhoneInviteConferenceCallParticipantRequest) SetFlags() {
	if !(i.Video == false) {
		i.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (i *PhoneInviteConferenceCallParticipantRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode phone.inviteConferenceCallParticipant#bcf22685 as nil")
	}
	b.PutID(PhoneInviteConferenceCallParticipantRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *PhoneInviteConferenceCallParticipantRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode phone.inviteConferenceCallParticipant#bcf22685 as nil")
	}
	i.SetFlags()
	if err := i.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.inviteConferenceCallParticipant#bcf22685: field flags: %w", err)
	}
	if i.Call == nil {
		return fmt.Errorf("unable to encode phone.inviteConferenceCallParticipant#bcf22685: field call is nil")
	}
	if err := i.Call.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.inviteConferenceCallParticipant#bcf22685: field call: %w", err)
	}
	if i.UserID == nil {
		return fmt.Errorf("unable to encode phone.inviteConferenceCallParticipant#bcf22685: field user_id is nil")
	}
	if err := i.UserID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.inviteConferenceCallParticipant#bcf22685: field user_id: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *PhoneInviteConferenceCallParticipantRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode phone.inviteConferenceCallParticipant#bcf22685 to nil")
	}
	if err := b.ConsumeID(PhoneInviteConferenceCallParticipantRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode phone.inviteConferenceCallParticipant#bcf22685: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *PhoneInviteConferenceCallParticipantRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode phone.inviteConferenceCallParticipant#bcf22685 to nil")
	}
	{
		if err := i.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode phone.inviteConferenceCallParticipant#bcf22685: field flags: %w", err)
		}
	}
	i.Video = i.Flags.Has(0)
	{
		value, err := DecodeInputGroupCall(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.inviteConferenceCallParticipant#bcf22685: field call: %w", err)
		}
		i.Call = value
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.inviteConferenceCallParticipant#bcf22685: field user_id: %w", err)
		}
		i.UserID = value
	}
	return nil
}

// SetVideo sets value of Video conditional field.
func (i *PhoneInviteConferenceCallParticipantRequest) SetVideo(value bool) {
	if value {
		i.Flags.Set(0)
		i.Video = true
	} else {
		i.Flags.Unset(0)
		i.Video = false
	}
}

// GetVideo returns value of Video conditional field.
func (i *PhoneInviteConferenceCallParticipantRequest) GetVideo() (value bool) {
	if i == nil {
		return
	}
	return i.Flags.Has(0)
}

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

// GetUserID returns value of UserID field.
func (i *PhoneInviteConferenceCallParticipantRequest) GetUserID() (value InputUserClass) {
	if i == nil {
		return
	}
	return i.UserID
}

// PhoneInviteConferenceCallParticipant invokes method phone.inviteConferenceCallParticipant#bcf22685 returning error if any.
// Invite a user to a conference call.
//
// Possible errors:
//
//	400 GROUPCALL_INVALID: The specified group call is invalid.
//
// See https://core.telegram.org/method/phone.inviteConferenceCallParticipant for reference.
func (c *Client) PhoneInviteConferenceCallParticipant(ctx context.Context, request *PhoneInviteConferenceCallParticipantRequest) (UpdatesClass, error) {
	var result UpdatesBox

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