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

// SetOptionRequest represents TL type `setOption#7e0b4ef2`.
type SetOptionRequest struct {
	// The name of the option
	Name string
	// The new value of the option; pass null to reset option value to a default value
	Value OptionValueClass
}

// SetOptionRequestTypeID is TL type id of SetOptionRequest.
const SetOptionRequestTypeID = 0x7e0b4ef2

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

func (s *SetOptionRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Name == "") {
		return false
	}
	if !(s.Value == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetOptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setOption",
		ID:   SetOptionRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Value",
			SchemaName: "value",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetOptionRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setOption#7e0b4ef2 as nil")
	}
	b.PutID(SetOptionRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetOptionRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setOption#7e0b4ef2 as nil")
	}
	b.PutString(s.Name)
	if s.Value == nil {
		return fmt.Errorf("unable to encode setOption#7e0b4ef2: field value is nil")
	}
	if err := s.Value.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setOption#7e0b4ef2: field value: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetOptionRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setOption#7e0b4ef2 to nil")
	}
	if err := b.ConsumeID(SetOptionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setOption#7e0b4ef2: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetOptionRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setOption#7e0b4ef2 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setOption#7e0b4ef2: field name: %w", err)
		}
		s.Name = value
	}
	{
		value, err := DecodeOptionValue(b)
		if err != nil {
			return fmt.Errorf("unable to decode setOption#7e0b4ef2: field value: %w", err)
		}
		s.Value = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetOptionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setOption#7e0b4ef2 as nil")
	}
	b.ObjStart()
	b.PutID("setOption")
	b.Comma()
	b.FieldStart("name")
	b.PutString(s.Name)
	b.Comma()
	b.FieldStart("value")
	if s.Value == nil {
		return fmt.Errorf("unable to encode setOption#7e0b4ef2: field value is nil")
	}
	if err := s.Value.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setOption#7e0b4ef2: field value: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetOptionRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setOption#7e0b4ef2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setOption"); err != nil {
				return fmt.Errorf("unable to decode setOption#7e0b4ef2: %w", err)
			}
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setOption#7e0b4ef2: field name: %w", err)
			}
			s.Name = value
		case "value":
			value, err := DecodeTDLibJSONOptionValue(b)
			if err != nil {
				return fmt.Errorf("unable to decode setOption#7e0b4ef2: field value: %w", err)
			}
			s.Value = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetName returns value of Name field.
func (s *SetOptionRequest) GetName() (value string) {
	if s == nil {
		return
	}
	return s.Name
}

// GetValue returns value of Value field.
func (s *SetOptionRequest) GetValue() (value OptionValueClass) {
	if s == nil {
		return
	}
	return s.Value
}

// SetOption invokes method setOption#7e0b4ef2 returning error if any.
func (c *Client) SetOption(ctx context.Context, request *SetOptionRequest) error {
	var ok Ok

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