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

// PhoneDeleteGroupCallMessagesRequest represents TL type `phone.deleteGroupCallMessages#f64f54f7`.
//
// See https://core.telegram.org/method/phone.deleteGroupCallMessages for reference.
type PhoneDeleteGroupCallMessagesRequest struct {
	// Flags field of PhoneDeleteGroupCallMessagesRequest.
	Flags bin.Fields
	// ReportSpam field of PhoneDeleteGroupCallMessagesRequest.
	ReportSpam bool
	// Call field of PhoneDeleteGroupCallMessagesRequest.
	Call InputGroupCallClass
	// Messages field of PhoneDeleteGroupCallMessagesRequest.
	Messages []int
}

// PhoneDeleteGroupCallMessagesRequestTypeID is TL type id of PhoneDeleteGroupCallMessagesRequest.
const PhoneDeleteGroupCallMessagesRequestTypeID = 0xf64f54f7

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

func (d *PhoneDeleteGroupCallMessagesRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Flags.Zero()) {
		return false
	}
	if !(d.ReportSpam == false) {
		return false
	}
	if !(d.Call == nil) {
		return false
	}
	if !(d.Messages == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneDeleteGroupCallMessagesRequest from given interface.
func (d *PhoneDeleteGroupCallMessagesRequest) FillFrom(from interface {
	GetReportSpam() (value bool)
	GetCall() (value InputGroupCallClass)
	GetMessages() (value []int)
}) {
	d.ReportSpam = from.GetReportSpam()
	d.Call = from.GetCall()
	d.Messages = from.GetMessages()
}

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

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

// TypeInfo returns info about TL type.
func (d *PhoneDeleteGroupCallMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.deleteGroupCallMessages",
		ID:   PhoneDeleteGroupCallMessagesRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ReportSpam",
			SchemaName: "report_spam",
			Null:       !d.Flags.Has(0),
		},
		{
			Name:       "Call",
			SchemaName: "call",
		},
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (d *PhoneDeleteGroupCallMessagesRequest) SetFlags() {
	if !(d.ReportSpam == false) {
		d.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (d *PhoneDeleteGroupCallMessagesRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode phone.deleteGroupCallMessages#f64f54f7 as nil")
	}
	b.PutID(PhoneDeleteGroupCallMessagesRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *PhoneDeleteGroupCallMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode phone.deleteGroupCallMessages#f64f54f7 as nil")
	}
	d.SetFlags()
	if err := d.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.deleteGroupCallMessages#f64f54f7: field flags: %w", err)
	}
	if d.Call == nil {
		return fmt.Errorf("unable to encode phone.deleteGroupCallMessages#f64f54f7: field call is nil")
	}
	if err := d.Call.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.deleteGroupCallMessages#f64f54f7: field call: %w", err)
	}
	b.PutVectorHeader(len(d.Messages))
	for _, v := range d.Messages {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *PhoneDeleteGroupCallMessagesRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode phone.deleteGroupCallMessages#f64f54f7 to nil")
	}
	if err := b.ConsumeID(PhoneDeleteGroupCallMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode phone.deleteGroupCallMessages#f64f54f7: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *PhoneDeleteGroupCallMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode phone.deleteGroupCallMessages#f64f54f7 to nil")
	}
	{
		if err := d.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode phone.deleteGroupCallMessages#f64f54f7: field flags: %w", err)
		}
	}
	d.ReportSpam = d.Flags.Has(0)
	{
		value, err := DecodeInputGroupCall(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.deleteGroupCallMessages#f64f54f7: field call: %w", err)
		}
		d.Call = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode phone.deleteGroupCallMessages#f64f54f7: field messages: %w", err)
		}

		if headerLen > 0 {
			d.Messages = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode phone.deleteGroupCallMessages#f64f54f7: field messages: %w", err)
			}
			d.Messages = append(d.Messages, value)
		}
	}
	return nil
}

// SetReportSpam sets value of ReportSpam conditional field.
func (d *PhoneDeleteGroupCallMessagesRequest) SetReportSpam(value bool) {
	if value {
		d.Flags.Set(0)
		d.ReportSpam = true
	} else {
		d.Flags.Unset(0)
		d.ReportSpam = false
	}
}

// GetReportSpam returns value of ReportSpam conditional field.
func (d *PhoneDeleteGroupCallMessagesRequest) GetReportSpam() (value bool) {
	if d == nil {
		return
	}
	return d.Flags.Has(0)
}

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

// GetMessages returns value of Messages field.
func (d *PhoneDeleteGroupCallMessagesRequest) GetMessages() (value []int) {
	if d == nil {
		return
	}
	return d.Messages
}

// PhoneDeleteGroupCallMessages invokes method phone.deleteGroupCallMessages#f64f54f7 returning error if any.
//
// See https://core.telegram.org/method/phone.deleteGroupCallMessages for reference.
func (c *Client) PhoneDeleteGroupCallMessages(ctx context.Context, request *PhoneDeleteGroupCallMessagesRequest) (UpdatesClass, error) {
	var result UpdatesBox

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