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

// WriteGeneratedFilePartRequest represents TL type `writeGeneratedFilePart#cc89e95`.
type WriteGeneratedFilePartRequest struct {
	// The identifier of the generation process
	GenerationID int64
	// The offset from which to write the data to the file
	Offset int64
	// The data to write
	Data []byte
}

// WriteGeneratedFilePartRequestTypeID is TL type id of WriteGeneratedFilePartRequest.
const WriteGeneratedFilePartRequestTypeID = 0xcc89e95

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

func (w *WriteGeneratedFilePartRequest) Zero() bool {
	if w == nil {
		return true
	}
	if !(w.GenerationID == 0) {
		return false
	}
	if !(w.Offset == 0) {
		return false
	}
	if !(w.Data == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (w *WriteGeneratedFilePartRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "writeGeneratedFilePart",
		ID:   WriteGeneratedFilePartRequestTypeID,
	}
	if w == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GenerationID",
			SchemaName: "generation_id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Data",
			SchemaName: "data",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (w *WriteGeneratedFilePartRequest) Encode(b *bin.Buffer) error {
	if w == nil {
		return fmt.Errorf("can't encode writeGeneratedFilePart#cc89e95 as nil")
	}
	b.PutID(WriteGeneratedFilePartRequestTypeID)
	return w.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (w *WriteGeneratedFilePartRequest) EncodeBare(b *bin.Buffer) error {
	if w == nil {
		return fmt.Errorf("can't encode writeGeneratedFilePart#cc89e95 as nil")
	}
	b.PutLong(w.GenerationID)
	b.PutInt53(w.Offset)
	b.PutBytes(w.Data)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (w *WriteGeneratedFilePartRequest) DecodeBare(b *bin.Buffer) error {
	if w == nil {
		return fmt.Errorf("can't decode writeGeneratedFilePart#cc89e95 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode writeGeneratedFilePart#cc89e95: field generation_id: %w", err)
		}
		w.GenerationID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode writeGeneratedFilePart#cc89e95: field offset: %w", err)
		}
		w.Offset = value
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode writeGeneratedFilePart#cc89e95: field data: %w", err)
		}
		w.Data = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (w *WriteGeneratedFilePartRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if w == nil {
		return fmt.Errorf("can't encode writeGeneratedFilePart#cc89e95 as nil")
	}
	b.ObjStart()
	b.PutID("writeGeneratedFilePart")
	b.Comma()
	b.FieldStart("generation_id")
	b.PutLong(w.GenerationID)
	b.Comma()
	b.FieldStart("offset")
	b.PutInt53(w.Offset)
	b.Comma()
	b.FieldStart("data")
	b.PutBytes(w.Data)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (w *WriteGeneratedFilePartRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if w == nil {
		return fmt.Errorf("can't decode writeGeneratedFilePart#cc89e95 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("writeGeneratedFilePart"); err != nil {
				return fmt.Errorf("unable to decode writeGeneratedFilePart#cc89e95: %w", err)
			}
		case "generation_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode writeGeneratedFilePart#cc89e95: field generation_id: %w", err)
			}
			w.GenerationID = value
		case "offset":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode writeGeneratedFilePart#cc89e95: field offset: %w", err)
			}
			w.Offset = value
		case "data":
			value, err := b.Bytes()
			if err != nil {
				return fmt.Errorf("unable to decode writeGeneratedFilePart#cc89e95: field data: %w", err)
			}
			w.Data = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGenerationID returns value of GenerationID field.
func (w *WriteGeneratedFilePartRequest) GetGenerationID() (value int64) {
	if w == nil {
		return
	}
	return w.GenerationID
}

// GetOffset returns value of Offset field.
func (w *WriteGeneratedFilePartRequest) GetOffset() (value int64) {
	if w == nil {
		return
	}
	return w.Offset
}

// GetData returns value of Data field.
func (w *WriteGeneratedFilePartRequest) GetData() (value []byte) {
	if w == nil {
		return
	}
	return w.Data
}

// WriteGeneratedFilePart invokes method writeGeneratedFilePart#cc89e95 returning error if any.
func (c *Client) WriteGeneratedFilePart(ctx context.Context, request *WriteGeneratedFilePartRequest) error {
	var ok Ok

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