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

// SaveApplicationLogEventRequest represents TL type `saveApplicationLogEvent#cfa6c20e`.
type SaveApplicationLogEventRequest struct {
	// Event type
	Type string
	// Optional chat identifier, associated with the event
	ChatID int64
	// The log event data
	Data JSONValueClass
}

// SaveApplicationLogEventRequestTypeID is TL type id of SaveApplicationLogEventRequest.
const SaveApplicationLogEventRequestTypeID = 0xcfa6c20e

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

func (s *SaveApplicationLogEventRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Type == "") {
		return false
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.Data == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SaveApplicationLogEventRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "saveApplicationLogEvent",
		ID:   SaveApplicationLogEventRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Data",
			SchemaName: "data",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SaveApplicationLogEventRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode saveApplicationLogEvent#cfa6c20e as nil")
	}
	b.PutID(SaveApplicationLogEventRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SaveApplicationLogEventRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode saveApplicationLogEvent#cfa6c20e as nil")
	}
	b.PutString(s.Type)
	b.PutInt53(s.ChatID)
	if s.Data == nil {
		return fmt.Errorf("unable to encode saveApplicationLogEvent#cfa6c20e: field data is nil")
	}
	if err := s.Data.Encode(b); err != nil {
		return fmt.Errorf("unable to encode saveApplicationLogEvent#cfa6c20e: field data: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SaveApplicationLogEventRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode saveApplicationLogEvent#cfa6c20e to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode saveApplicationLogEvent#cfa6c20e: field type: %w", err)
		}
		s.Type = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode saveApplicationLogEvent#cfa6c20e: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := DecodeJSONValue(b)
		if err != nil {
			return fmt.Errorf("unable to decode saveApplicationLogEvent#cfa6c20e: field data: %w", err)
		}
		s.Data = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SaveApplicationLogEventRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode saveApplicationLogEvent#cfa6c20e as nil")
	}
	b.ObjStart()
	b.PutID("saveApplicationLogEvent")
	b.Comma()
	b.FieldStart("type")
	b.PutString(s.Type)
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("data")
	if s.Data == nil {
		return fmt.Errorf("unable to encode saveApplicationLogEvent#cfa6c20e: field data is nil")
	}
	if err := s.Data.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode saveApplicationLogEvent#cfa6c20e: field data: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("saveApplicationLogEvent"); err != nil {
				return fmt.Errorf("unable to decode saveApplicationLogEvent#cfa6c20e: %w", err)
			}
		case "type":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode saveApplicationLogEvent#cfa6c20e: field type: %w", err)
			}
			s.Type = value
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode saveApplicationLogEvent#cfa6c20e: field chat_id: %w", err)
			}
			s.ChatID = value
		case "data":
			value, err := DecodeTDLibJSONJSONValue(b)
			if err != nil {
				return fmt.Errorf("unable to decode saveApplicationLogEvent#cfa6c20e: field data: %w", err)
			}
			s.Data = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (s *SaveApplicationLogEventRequest) GetType() (value string) {
	if s == nil {
		return
	}
	return s.Type
}

// GetChatID returns value of ChatID field.
func (s *SaveApplicationLogEventRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetData returns value of Data field.
func (s *SaveApplicationLogEventRequest) GetData() (value JSONValueClass) {
	if s == nil {
		return
	}
	return s.Data
}

// SaveApplicationLogEvent invokes method saveApplicationLogEvent#cfa6c20e returning error if any.
func (c *Client) SaveApplicationLogEvent(ctx context.Context, request *SaveApplicationLogEventRequest) error {
	var ok Ok

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