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

// OpenChatRequest represents TL type `openChat#ecb9be0b`.
type OpenChatRequest struct {
	// Chat identifier
	ChatID int64
}

// OpenChatRequestTypeID is TL type id of OpenChatRequest.
const OpenChatRequestTypeID = 0xecb9be0b

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

func (o *OpenChatRequest) Zero() bool {
	if o == nil {
		return true
	}
	if !(o.ChatID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (o *OpenChatRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "openChat",
		ID:   OpenChatRequestTypeID,
	}
	if o == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (o *OpenChatRequest) Encode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode openChat#ecb9be0b as nil")
	}
	b.PutID(OpenChatRequestTypeID)
	return o.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (o *OpenChatRequest) EncodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode openChat#ecb9be0b as nil")
	}
	b.PutInt53(o.ChatID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (o *OpenChatRequest) DecodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode openChat#ecb9be0b to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode openChat#ecb9be0b: field chat_id: %w", err)
		}
		o.ChatID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (o *OpenChatRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if o == nil {
		return fmt.Errorf("can't encode openChat#ecb9be0b as nil")
	}
	b.ObjStart()
	b.PutID("openChat")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(o.ChatID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (o *OpenChatRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if o == nil {
		return fmt.Errorf("can't decode openChat#ecb9be0b to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("openChat"); err != nil {
				return fmt.Errorf("unable to decode openChat#ecb9be0b: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode openChat#ecb9be0b: field chat_id: %w", err)
			}
			o.ChatID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// OpenChat invokes method openChat#ecb9be0b returning error if any.
func (c *Client) OpenChat(ctx context.Context, chatid int64) error {
	var ok Ok

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