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

// GetMessageFileTypeRequest represents TL type `getMessageFileType#e2c70fd4`.
type GetMessageFileTypeRequest struct {
	// Beginning of the message file; up to 100 first lines
	MessageFileHead string
}

// GetMessageFileTypeRequestTypeID is TL type id of GetMessageFileTypeRequest.
const GetMessageFileTypeRequestTypeID = 0xe2c70fd4

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

func (g *GetMessageFileTypeRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.MessageFileHead == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetMessageFileTypeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getMessageFileType",
		ID:   GetMessageFileTypeRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MessageFileHead",
			SchemaName: "message_file_head",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetMessageFileTypeRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getMessageFileType#e2c70fd4 as nil")
	}
	b.PutID(GetMessageFileTypeRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetMessageFileTypeRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getMessageFileType#e2c70fd4 as nil")
	}
	b.PutString(g.MessageFileHead)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetMessageFileTypeRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getMessageFileType#e2c70fd4 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getMessageFileType#e2c70fd4: field message_file_head: %w", err)
		}
		g.MessageFileHead = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetMessageFileTypeRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getMessageFileType#e2c70fd4 as nil")
	}
	b.ObjStart()
	b.PutID("getMessageFileType")
	b.Comma()
	b.FieldStart("message_file_head")
	b.PutString(g.MessageFileHead)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetMessageFileTypeRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getMessageFileType#e2c70fd4 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getMessageFileType"); err != nil {
				return fmt.Errorf("unable to decode getMessageFileType#e2c70fd4: %w", err)
			}
		case "message_file_head":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getMessageFileType#e2c70fd4: field message_file_head: %w", err)
			}
			g.MessageFileHead = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMessageFileHead returns value of MessageFileHead field.
func (g *GetMessageFileTypeRequest) GetMessageFileHead() (value string) {
	if g == nil {
		return
	}
	return g.MessageFileHead
}

// GetMessageFileType invokes method getMessageFileType#e2c70fd4 returning error if any.
func (c *Client) GetMessageFileType(ctx context.Context, messagefilehead string) (MessageFileTypeClass, error) {
	var result MessageFileTypeBox

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