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

// GetThemeParametersJSONStringRequest represents TL type `getThemeParametersJsonString#91b905f8`.
type GetThemeParametersJSONStringRequest struct {
	// Theme parameters to convert to JSON
	Theme ThemeParameters
}

// GetThemeParametersJSONStringRequestTypeID is TL type id of GetThemeParametersJSONStringRequest.
const GetThemeParametersJSONStringRequestTypeID = 0x91b905f8

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetThemeParametersJSONStringRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getThemeParametersJsonString",
		ID:   GetThemeParametersJSONStringRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Theme",
			SchemaName: "theme",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetThemeParametersJSONStringRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getThemeParametersJsonString#91b905f8 as nil")
	}
	b.PutID(GetThemeParametersJSONStringRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetThemeParametersJSONStringRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getThemeParametersJsonString#91b905f8 as nil")
	}
	if err := g.Theme.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getThemeParametersJsonString#91b905f8: field theme: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetThemeParametersJSONStringRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getThemeParametersJsonString#91b905f8 to nil")
	}
	{
		if err := g.Theme.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getThemeParametersJsonString#91b905f8: field theme: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetThemeParametersJSONStringRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getThemeParametersJsonString#91b905f8 as nil")
	}
	b.ObjStart()
	b.PutID("getThemeParametersJsonString")
	b.Comma()
	b.FieldStart("theme")
	if err := g.Theme.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getThemeParametersJsonString#91b905f8: field theme: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getThemeParametersJsonString"); err != nil {
				return fmt.Errorf("unable to decode getThemeParametersJsonString#91b905f8: %w", err)
			}
		case "theme":
			if err := g.Theme.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode getThemeParametersJsonString#91b905f8: field theme: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTheme returns value of Theme field.
func (g *GetThemeParametersJSONStringRequest) GetTheme() (value ThemeParameters) {
	if g == nil {
		return
	}
	return g.Theme
}

// GetThemeParametersJSONString invokes method getThemeParametersJsonString#91b905f8 returning error if any.
func (c *Client) GetThemeParametersJSONString(ctx context.Context, theme ThemeParameters) (*Text, error) {
	var result Text

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