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

// TransferChatOwnershipRequest represents TL type `transferChatOwnership#77a00a13`.
type TransferChatOwnershipRequest struct {
	// Chat identifier
	ChatID int64
	// Identifier of the user to which transfer the ownership. The ownership can't be
	// transferred to a bot or to a deleted user
	UserID int64
	// The 2-step verification password of the current user
	Password string
}

// TransferChatOwnershipRequestTypeID is TL type id of TransferChatOwnershipRequest.
const TransferChatOwnershipRequestTypeID = 0x77a00a13

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

func (t *TransferChatOwnershipRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.ChatID == 0) {
		return false
	}
	if !(t.UserID == 0) {
		return false
	}
	if !(t.Password == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TransferChatOwnershipRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "transferChatOwnership",
		ID:   TransferChatOwnershipRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "Password",
			SchemaName: "password",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TransferChatOwnershipRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode transferChatOwnership#77a00a13 as nil")
	}
	b.PutID(TransferChatOwnershipRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TransferChatOwnershipRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode transferChatOwnership#77a00a13 as nil")
	}
	b.PutInt53(t.ChatID)
	b.PutInt53(t.UserID)
	b.PutString(t.Password)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *TransferChatOwnershipRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode transferChatOwnership#77a00a13 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode transferChatOwnership#77a00a13: field chat_id: %w", err)
		}
		t.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode transferChatOwnership#77a00a13: field user_id: %w", err)
		}
		t.UserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode transferChatOwnership#77a00a13: field password: %w", err)
		}
		t.Password = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TransferChatOwnershipRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode transferChatOwnership#77a00a13 as nil")
	}
	b.ObjStart()
	b.PutID("transferChatOwnership")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(t.ChatID)
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(t.UserID)
	b.Comma()
	b.FieldStart("password")
	b.PutString(t.Password)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TransferChatOwnershipRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode transferChatOwnership#77a00a13 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("transferChatOwnership"); err != nil {
				return fmt.Errorf("unable to decode transferChatOwnership#77a00a13: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode transferChatOwnership#77a00a13: field chat_id: %w", err)
			}
			t.ChatID = value
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode transferChatOwnership#77a00a13: field user_id: %w", err)
			}
			t.UserID = value
		case "password":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode transferChatOwnership#77a00a13: field password: %w", err)
			}
			t.Password = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetUserID returns value of UserID field.
func (t *TransferChatOwnershipRequest) GetUserID() (value int64) {
	if t == nil {
		return
	}
	return t.UserID
}

// GetPassword returns value of Password field.
func (t *TransferChatOwnershipRequest) GetPassword() (value string) {
	if t == nil {
		return
	}
	return t.Password
}

// TransferChatOwnership invokes method transferChatOwnership#77a00a13 returning error if any.
func (c *Client) TransferChatOwnership(ctx context.Context, request *TransferChatOwnershipRequest) error {
	var ok Ok

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