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

// PhoneCreateConferenceCallRequest represents TL type `phone.createConferenceCall#7d0444bb`.
// Create and optionally join a new conference call.
//
// See https://core.telegram.org/method/phone.createConferenceCall for reference.
type PhoneCreateConferenceCallRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, mute our microphone when joining the call (can only be used if join is set).
	Muted bool
	// If set, our video stream is disabled (can only be used if join is set).
	VideoStopped bool
	// If set, also join the call, otherwise just create the call link.
	Join bool
	// Unique client message ID required to prevent creation of duplicate group calls.
	RandomID int
	// Public key (can only be used if join is set).
	//
	// Use SetPublicKey and GetPublicKey helpers.
	PublicKey bin.Int256
	// Initial blockchain block (can only be used if join is set).
	//
	// Use SetBlock and GetBlock helpers.
	Block []byte
	// Parameters from tgcalls (can only be used if join is set).
	//
	// Use SetParams and GetParams helpers.
	Params DataJSON
}

// PhoneCreateConferenceCallRequestTypeID is TL type id of PhoneCreateConferenceCallRequest.
const PhoneCreateConferenceCallRequestTypeID = 0x7d0444bb

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

func (c *PhoneCreateConferenceCallRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Muted == false) {
		return false
	}
	if !(c.VideoStopped == false) {
		return false
	}
	if !(c.Join == false) {
		return false
	}
	if !(c.RandomID == 0) {
		return false
	}
	if !(c.PublicKey == bin.Int256{}) {
		return false
	}
	if !(c.Block == nil) {
		return false
	}
	if !(c.Params.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneCreateConferenceCallRequest from given interface.
func (c *PhoneCreateConferenceCallRequest) FillFrom(from interface {
	GetMuted() (value bool)
	GetVideoStopped() (value bool)
	GetJoin() (value bool)
	GetRandomID() (value int)
	GetPublicKey() (value bin.Int256, ok bool)
	GetBlock() (value []byte, ok bool)
	GetParams() (value DataJSON, ok bool)
}) {
	c.Muted = from.GetMuted()
	c.VideoStopped = from.GetVideoStopped()
	c.Join = from.GetJoin()
	c.RandomID = from.GetRandomID()
	if val, ok := from.GetPublicKey(); ok {
		c.PublicKey = val
	}

	if val, ok := from.GetBlock(); ok {
		c.Block = val
	}

	if val, ok := from.GetParams(); ok {
		c.Params = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (c *PhoneCreateConferenceCallRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.createConferenceCall",
		ID:   PhoneCreateConferenceCallRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Muted",
			SchemaName: "muted",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "VideoStopped",
			SchemaName: "video_stopped",
			Null:       !c.Flags.Has(2),
		},
		{
			Name:       "Join",
			SchemaName: "join",
			Null:       !c.Flags.Has(3),
		},
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "PublicKey",
			SchemaName: "public_key",
			Null:       !c.Flags.Has(3),
		},
		{
			Name:       "Block",
			SchemaName: "block",
			Null:       !c.Flags.Has(3),
		},
		{
			Name:       "Params",
			SchemaName: "params",
			Null:       !c.Flags.Has(3),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *PhoneCreateConferenceCallRequest) SetFlags() {
	if !(c.Muted == false) {
		c.Flags.Set(0)
	}
	if !(c.VideoStopped == false) {
		c.Flags.Set(2)
	}
	if !(c.Join == false) {
		c.Flags.Set(3)
	}
	if !(c.PublicKey == bin.Int256{}) {
		c.Flags.Set(3)
	}
	if !(c.Block == nil) {
		c.Flags.Set(3)
	}
	if !(c.Params.Zero()) {
		c.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (c *PhoneCreateConferenceCallRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode phone.createConferenceCall#7d0444bb as nil")
	}
	b.PutID(PhoneCreateConferenceCallRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *PhoneCreateConferenceCallRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode phone.createConferenceCall#7d0444bb as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.createConferenceCall#7d0444bb: field flags: %w", err)
	}
	b.PutInt(c.RandomID)
	if c.Flags.Has(3) {
		b.PutInt256(c.PublicKey)
	}
	if c.Flags.Has(3) {
		b.PutBytes(c.Block)
	}
	if c.Flags.Has(3) {
		if err := c.Params.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.createConferenceCall#7d0444bb: field params: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *PhoneCreateConferenceCallRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode phone.createConferenceCall#7d0444bb to nil")
	}
	if err := b.ConsumeID(PhoneCreateConferenceCallRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode phone.createConferenceCall#7d0444bb: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *PhoneCreateConferenceCallRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode phone.createConferenceCall#7d0444bb to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode phone.createConferenceCall#7d0444bb: field flags: %w", err)
		}
	}
	c.Muted = c.Flags.Has(0)
	c.VideoStopped = c.Flags.Has(2)
	c.Join = c.Flags.Has(3)
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode phone.createConferenceCall#7d0444bb: field random_id: %w", err)
		}
		c.RandomID = value
	}
	if c.Flags.Has(3) {
		value, err := b.Int256()
		if err != nil {
			return fmt.Errorf("unable to decode phone.createConferenceCall#7d0444bb: field public_key: %w", err)
		}
		c.PublicKey = value
	}
	if c.Flags.Has(3) {
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode phone.createConferenceCall#7d0444bb: field block: %w", err)
		}
		c.Block = value
	}
	if c.Flags.Has(3) {
		if err := c.Params.Decode(b); err != nil {
			return fmt.Errorf("unable to decode phone.createConferenceCall#7d0444bb: field params: %w", err)
		}
	}
	return nil
}

// SetMuted sets value of Muted conditional field.
func (c *PhoneCreateConferenceCallRequest) SetMuted(value bool) {
	if value {
		c.Flags.Set(0)
		c.Muted = true
	} else {
		c.Flags.Unset(0)
		c.Muted = false
	}
}

// GetMuted returns value of Muted conditional field.
func (c *PhoneCreateConferenceCallRequest) GetMuted() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(0)
}

// SetVideoStopped sets value of VideoStopped conditional field.
func (c *PhoneCreateConferenceCallRequest) SetVideoStopped(value bool) {
	if value {
		c.Flags.Set(2)
		c.VideoStopped = true
	} else {
		c.Flags.Unset(2)
		c.VideoStopped = false
	}
}

// GetVideoStopped returns value of VideoStopped conditional field.
func (c *PhoneCreateConferenceCallRequest) GetVideoStopped() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(2)
}

// SetJoin sets value of Join conditional field.
func (c *PhoneCreateConferenceCallRequest) SetJoin(value bool) {
	if value {
		c.Flags.Set(3)
		c.Join = true
	} else {
		c.Flags.Unset(3)
		c.Join = false
	}
}

// GetJoin returns value of Join conditional field.
func (c *PhoneCreateConferenceCallRequest) GetJoin() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(3)
}

// GetRandomID returns value of RandomID field.
func (c *PhoneCreateConferenceCallRequest) GetRandomID() (value int) {
	if c == nil {
		return
	}
	return c.RandomID
}

// SetPublicKey sets value of PublicKey conditional field.
func (c *PhoneCreateConferenceCallRequest) SetPublicKey(value bin.Int256) {
	c.Flags.Set(3)
	c.PublicKey = value
}

// GetPublicKey returns value of PublicKey conditional field and
// boolean which is true if field was set.
func (c *PhoneCreateConferenceCallRequest) GetPublicKey() (value bin.Int256, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(3) {
		return value, false
	}
	return c.PublicKey, true
}

// SetBlock sets value of Block conditional field.
func (c *PhoneCreateConferenceCallRequest) SetBlock(value []byte) {
	c.Flags.Set(3)
	c.Block = value
}

// GetBlock returns value of Block conditional field and
// boolean which is true if field was set.
func (c *PhoneCreateConferenceCallRequest) GetBlock() (value []byte, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(3) {
		return value, false
	}
	return c.Block, true
}

// SetParams sets value of Params conditional field.
func (c *PhoneCreateConferenceCallRequest) SetParams(value DataJSON) {
	c.Flags.Set(3)
	c.Params = value
}

// GetParams returns value of Params conditional field and
// boolean which is true if field was set.
func (c *PhoneCreateConferenceCallRequest) GetParams() (value DataJSON, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(3) {
		return value, false
	}
	return c.Params, true
}

// PhoneCreateConferenceCall invokes method phone.createConferenceCall#7d0444bb returning error if any.
// Create and optionally join a new conference call.
//
// See https://core.telegram.org/method/phone.createConferenceCall for reference.
func (c *Client) PhoneCreateConferenceCall(ctx context.Context, request *PhoneCreateConferenceCallRequest) (UpdatesClass, error) {
	var result UpdatesBox

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