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

// RemoveInstalledBackgroundRequest represents TL type `removeInstalledBackground#5041253c`.
type RemoveInstalledBackgroundRequest struct {
	// The background identifier
	BackgroundID int64
}

// RemoveInstalledBackgroundRequestTypeID is TL type id of RemoveInstalledBackgroundRequest.
const RemoveInstalledBackgroundRequestTypeID = 0x5041253c

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

func (r *RemoveInstalledBackgroundRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.BackgroundID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *RemoveInstalledBackgroundRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "removeInstalledBackground",
		ID:   RemoveInstalledBackgroundRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BackgroundID",
			SchemaName: "background_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *RemoveInstalledBackgroundRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode removeInstalledBackground#5041253c as nil")
	}
	b.PutID(RemoveInstalledBackgroundRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *RemoveInstalledBackgroundRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode removeInstalledBackground#5041253c as nil")
	}
	b.PutLong(r.BackgroundID)
	return nil
}

// Decode implements bin.Decoder.
func (r *RemoveInstalledBackgroundRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode removeInstalledBackground#5041253c to nil")
	}
	if err := b.ConsumeID(RemoveInstalledBackgroundRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode removeInstalledBackground#5041253c: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *RemoveInstalledBackgroundRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode removeInstalledBackground#5041253c to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode removeInstalledBackground#5041253c: field background_id: %w", err)
		}
		r.BackgroundID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *RemoveInstalledBackgroundRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode removeInstalledBackground#5041253c as nil")
	}
	b.ObjStart()
	b.PutID("removeInstalledBackground")
	b.Comma()
	b.FieldStart("background_id")
	b.PutLong(r.BackgroundID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *RemoveInstalledBackgroundRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode removeInstalledBackground#5041253c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("removeInstalledBackground"); err != nil {
				return fmt.Errorf("unable to decode removeInstalledBackground#5041253c: %w", err)
			}
		case "background_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode removeInstalledBackground#5041253c: field background_id: %w", err)
			}
			r.BackgroundID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBackgroundID returns value of BackgroundID field.
func (r *RemoveInstalledBackgroundRequest) GetBackgroundID() (value int64) {
	if r == nil {
		return
	}
	return r.BackgroundID
}

// RemoveInstalledBackground invokes method removeInstalledBackground#5041253c returning error if any.
func (c *Client) RemoveInstalledBackground(ctx context.Context, backgroundid int64) error {
	var ok Ok

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