// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// PhoneCreateGroupCallRequest represents TL type `phone.createGroupCall#48cdc6d8`.
// Create a group call or livestream
//
// See https://core.telegram.org/method/phone.createGroupCall for reference.
type PhoneCreateGroupCallRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether RTMP stream support should be enabled: only the group/supergroup/channel¹
	// owner can use this flag.
	//
	// Links:
	//  1) https://core.telegram.org/api/channel
	RtmpStream bool
	// Associate the group call or livestream to the provided group/supergroup/channel¹
	//
	// Links:
	//  1) https://core.telegram.org/api/channel
	Peer InputPeerClass
	// Unique client message ID required to prevent creation of duplicate group calls
	RandomID int
	// Call title
	//
	// Use SetTitle and GetTitle helpers.
	Title string
	// For scheduled group call or livestreams, the absolute date when the group call will
	// start
	//
	// Use SetScheduleDate and GetScheduleDate helpers.
	ScheduleDate int
}

// PhoneCreateGroupCallRequestTypeID is TL type id of PhoneCreateGroupCallRequest.
const PhoneCreateGroupCallRequestTypeID = 0x48cdc6d8

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

func (c *PhoneCreateGroupCallRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.RtmpStream == false) {
		return false
	}
	if !(c.Peer == nil) {
		return false
	}
	if !(c.RandomID == 0) {
		return false
	}
	if !(c.Title == "") {
		return false
	}
	if !(c.ScheduleDate == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneCreateGroupCallRequest from given interface.
func (c *PhoneCreateGroupCallRequest) FillFrom(from interface {
	GetRtmpStream() (value bool)
	GetPeer() (value InputPeerClass)
	GetRandomID() (value int)
	GetTitle() (value string, ok bool)
	GetScheduleDate() (value int, ok bool)
}) {
	c.RtmpStream = from.GetRtmpStream()
	c.Peer = from.GetPeer()
	c.RandomID = from.GetRandomID()
	if val, ok := from.GetTitle(); ok {
		c.Title = val
	}

	if val, ok := from.GetScheduleDate(); ok {
		c.ScheduleDate = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (c *PhoneCreateGroupCallRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.createGroupCall",
		ID:   PhoneCreateGroupCallRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "RtmpStream",
			SchemaName: "rtmp_stream",
			Null:       !c.Flags.Has(2),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "Title",
			SchemaName: "title",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "ScheduleDate",
			SchemaName: "schedule_date",
			Null:       !c.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *PhoneCreateGroupCallRequest) SetFlags() {
	if !(c.RtmpStream == false) {
		c.Flags.Set(2)
	}
	if !(c.Title == "") {
		c.Flags.Set(0)
	}
	if !(c.ScheduleDate == 0) {
		c.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (c *PhoneCreateGroupCallRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode phone.createGroupCall#48cdc6d8 as nil")
	}
	b.PutID(PhoneCreateGroupCallRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *PhoneCreateGroupCallRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode phone.createGroupCall#48cdc6d8 as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.createGroupCall#48cdc6d8: field flags: %w", err)
	}
	if c.Peer == nil {
		return fmt.Errorf("unable to encode phone.createGroupCall#48cdc6d8: field peer is nil")
	}
	if err := c.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.createGroupCall#48cdc6d8: field peer: %w", err)
	}
	b.PutInt(c.RandomID)
	if c.Flags.Has(0) {
		b.PutString(c.Title)
	}
	if c.Flags.Has(1) {
		b.PutInt(c.ScheduleDate)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *PhoneCreateGroupCallRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode phone.createGroupCall#48cdc6d8 to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode phone.createGroupCall#48cdc6d8: field flags: %w", err)
		}
	}
	c.RtmpStream = c.Flags.Has(2)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.createGroupCall#48cdc6d8: field peer: %w", err)
		}
		c.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode phone.createGroupCall#48cdc6d8: field random_id: %w", err)
		}
		c.RandomID = value
	}
	if c.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode phone.createGroupCall#48cdc6d8: field title: %w", err)
		}
		c.Title = value
	}
	if c.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode phone.createGroupCall#48cdc6d8: field schedule_date: %w", err)
		}
		c.ScheduleDate = value
	}
	return nil
}

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

// GetRtmpStream returns value of RtmpStream conditional field.
func (c *PhoneCreateGroupCallRequest) GetRtmpStream() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(2)
}

// GetPeer returns value of Peer field.
func (c *PhoneCreateGroupCallRequest) GetPeer() (value InputPeerClass) {
	if c == nil {
		return
	}
	return c.Peer
}

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

// SetTitle sets value of Title conditional field.
func (c *PhoneCreateGroupCallRequest) SetTitle(value string) {
	c.Flags.Set(0)
	c.Title = value
}

// GetTitle returns value of Title conditional field and
// boolean which is true if field was set.
func (c *PhoneCreateGroupCallRequest) GetTitle() (value string, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.Title, true
}

// SetScheduleDate sets value of ScheduleDate conditional field.
func (c *PhoneCreateGroupCallRequest) SetScheduleDate(value int) {
	c.Flags.Set(1)
	c.ScheduleDate = value
}

// GetScheduleDate returns value of ScheduleDate conditional field and
// boolean which is true if field was set.
func (c *PhoneCreateGroupCallRequest) GetScheduleDate() (value int, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(1) {
		return value, false
	}
	return c.ScheduleDate, true
}

// PhoneCreateGroupCall invokes method phone.createGroupCall#48cdc6d8 returning error if any.
// Create a group call or livestream
//
// Possible errors:
//
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//	400 CHAT_ADMIN_REQUIRED: You must be an admin in this chat to do this.
//	400 CREATE_CALL_FAILED: An error occurred while creating the call.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 SCHEDULE_DATE_INVALID: Invalid schedule date provided.
//
// See https://core.telegram.org/method/phone.createGroupCall for reference.
func (c *Client) PhoneCreateGroupCall(ctx context.Context, request *PhoneCreateGroupCallRequest) (UpdatesClass, error) {
	var result UpdatesBox

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