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

// GetInstalledBackgroundsRequest represents TL type `getInstalledBackgrounds#c154d05f`.
type GetInstalledBackgroundsRequest struct {
	// Pass true to order returned backgrounds for a dark theme
	ForDarkTheme bool
}

// GetInstalledBackgroundsRequestTypeID is TL type id of GetInstalledBackgroundsRequest.
const GetInstalledBackgroundsRequestTypeID = 0xc154d05f

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

func (g *GetInstalledBackgroundsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ForDarkTheme == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetInstalledBackgroundsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getInstalledBackgrounds",
		ID:   GetInstalledBackgroundsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ForDarkTheme",
			SchemaName: "for_dark_theme",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetInstalledBackgroundsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getInstalledBackgrounds#c154d05f as nil")
	}
	b.PutID(GetInstalledBackgroundsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetInstalledBackgroundsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getInstalledBackgrounds#c154d05f as nil")
	}
	b.PutBool(g.ForDarkTheme)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetInstalledBackgroundsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getInstalledBackgrounds#c154d05f to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getInstalledBackgrounds#c154d05f: field for_dark_theme: %w", err)
		}
		g.ForDarkTheme = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetInstalledBackgroundsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getInstalledBackgrounds#c154d05f as nil")
	}
	b.ObjStart()
	b.PutID("getInstalledBackgrounds")
	b.Comma()
	b.FieldStart("for_dark_theme")
	b.PutBool(g.ForDarkTheme)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetForDarkTheme returns value of ForDarkTheme field.
func (g *GetInstalledBackgroundsRequest) GetForDarkTheme() (value bool) {
	if g == nil {
		return
	}
	return g.ForDarkTheme
}

// GetInstalledBackgrounds invokes method getInstalledBackgrounds#c154d05f returning error if any.
func (c *Client) GetInstalledBackgrounds(ctx context.Context, fordarktheme bool) (*Backgrounds, error) {
	var result Backgrounds

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