// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// SendCallRatingRequest represents TL type `sendCallRating#d89b576c`.
type SendCallRatingRequest struct {
	// Call identifier
	CallID int32
	// Call rating; 1-5
	Rating int32
	// An optional user comment if the rating is less than 5
	Comment string
	// List of the exact types of problems with the call, specified by the user
	Problems []CallProblemClass
}

// SendCallRatingRequestTypeID is TL type id of SendCallRatingRequest.
const SendCallRatingRequestTypeID = 0xd89b576c

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

func (s *SendCallRatingRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.CallID == 0) {
		return false
	}
	if !(s.Rating == 0) {
		return false
	}
	if !(s.Comment == "") {
		return false
	}
	if !(s.Problems == nil) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*SendCallRatingRequest) TypeName() string {
	return "sendCallRating"
}

// TypeInfo returns info about TL type.
func (s *SendCallRatingRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sendCallRating",
		ID:   SendCallRatingRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CallID",
			SchemaName: "call_id",
		},
		{
			Name:       "Rating",
			SchemaName: "rating",
		},
		{
			Name:       "Comment",
			SchemaName: "comment",
		},
		{
			Name:       "Problems",
			SchemaName: "problems",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SendCallRatingRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendCallRating#d89b576c as nil")
	}
	b.PutID(SendCallRatingRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SendCallRatingRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendCallRating#d89b576c as nil")
	}
	b.PutInt32(s.CallID)
	b.PutInt32(s.Rating)
	b.PutString(s.Comment)
	b.PutInt(len(s.Problems))
	for idx, v := range s.Problems {
		if v == nil {
			return fmt.Errorf("unable to encode sendCallRating#d89b576c: field problems element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare sendCallRating#d89b576c: field problems element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SendCallRatingRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendCallRating#d89b576c to nil")
	}
	if err := b.ConsumeID(SendCallRatingRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode sendCallRating#d89b576c: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SendCallRatingRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendCallRating#d89b576c to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode sendCallRating#d89b576c: field call_id: %w", err)
		}
		s.CallID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode sendCallRating#d89b576c: field rating: %w", err)
		}
		s.Rating = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendCallRating#d89b576c: field comment: %w", err)
		}
		s.Comment = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode sendCallRating#d89b576c: field problems: %w", err)
		}

		if headerLen > 0 {
			s.Problems = make([]CallProblemClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeCallProblem(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendCallRating#d89b576c: field problems: %w", err)
			}
			s.Problems = append(s.Problems, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SendCallRatingRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sendCallRating#d89b576c as nil")
	}
	b.ObjStart()
	b.PutID("sendCallRating")
	b.Comma()
	b.FieldStart("call_id")
	b.PutInt32(s.CallID)
	b.Comma()
	b.FieldStart("rating")
	b.PutInt32(s.Rating)
	b.Comma()
	b.FieldStart("comment")
	b.PutString(s.Comment)
	b.Comma()
	b.FieldStart("problems")
	b.ArrStart()
	for idx, v := range s.Problems {
		if v == nil {
			return fmt.Errorf("unable to encode sendCallRating#d89b576c: field problems element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode sendCallRating#d89b576c: field problems element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SendCallRatingRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sendCallRating#d89b576c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sendCallRating"); err != nil {
				return fmt.Errorf("unable to decode sendCallRating#d89b576c: %w", err)
			}
		case "call_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode sendCallRating#d89b576c: field call_id: %w", err)
			}
			s.CallID = value
		case "rating":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode sendCallRating#d89b576c: field rating: %w", err)
			}
			s.Rating = value
		case "comment":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sendCallRating#d89b576c: field comment: %w", err)
			}
			s.Comment = value
		case "problems":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONCallProblem(b)
				if err != nil {
					return fmt.Errorf("unable to decode sendCallRating#d89b576c: field problems: %w", err)
				}
				s.Problems = append(s.Problems, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode sendCallRating#d89b576c: field problems: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCallID returns value of CallID field.
func (s *SendCallRatingRequest) GetCallID() (value int32) {
	if s == nil {
		return
	}
	return s.CallID
}

// GetRating returns value of Rating field.
func (s *SendCallRatingRequest) GetRating() (value int32) {
	if s == nil {
		return
	}
	return s.Rating
}

// GetComment returns value of Comment field.
func (s *SendCallRatingRequest) GetComment() (value string) {
	if s == nil {
		return
	}
	return s.Comment
}

// GetProblems returns value of Problems field.
func (s *SendCallRatingRequest) GetProblems() (value []CallProblemClass) {
	if s == nil {
		return
	}
	return s.Problems
}

// SendCallRating invokes method sendCallRating#d89b576c returning error if any.
func (c *Client) SendCallRating(ctx context.Context, request *SendCallRatingRequest) error {
	var ok Ok

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