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

// SetAutosaveSettingsRequest represents TL type `setAutosaveSettings#6878c0`.
type SetAutosaveSettingsRequest struct {
	// Autosave settings scope
	Scope AutosaveSettingsScopeClass
	// New autosave settings for the scope; pass null to set autosave settings to default
	Settings ScopeAutosaveSettings
}

// SetAutosaveSettingsRequestTypeID is TL type id of SetAutosaveSettingsRequest.
const SetAutosaveSettingsRequestTypeID = 0x6878c0

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

func (s *SetAutosaveSettingsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Scope == nil) {
		return false
	}
	if !(s.Settings.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetAutosaveSettingsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setAutosaveSettings",
		ID:   SetAutosaveSettingsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Scope",
			SchemaName: "scope",
		},
		{
			Name:       "Settings",
			SchemaName: "settings",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetAutosaveSettingsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAutosaveSettings#6878c0 as nil")
	}
	b.PutID(SetAutosaveSettingsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetAutosaveSettingsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAutosaveSettings#6878c0 as nil")
	}
	if s.Scope == nil {
		return fmt.Errorf("unable to encode setAutosaveSettings#6878c0: field scope is nil")
	}
	if err := s.Scope.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setAutosaveSettings#6878c0: field scope: %w", err)
	}
	if err := s.Settings.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setAutosaveSettings#6878c0: field settings: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetAutosaveSettingsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setAutosaveSettings#6878c0 to nil")
	}
	if err := b.ConsumeID(SetAutosaveSettingsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setAutosaveSettings#6878c0: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetAutosaveSettingsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setAutosaveSettings#6878c0 to nil")
	}
	{
		value, err := DecodeAutosaveSettingsScope(b)
		if err != nil {
			return fmt.Errorf("unable to decode setAutosaveSettings#6878c0: field scope: %w", err)
		}
		s.Scope = value
	}
	{
		if err := s.Settings.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setAutosaveSettings#6878c0: field settings: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetAutosaveSettingsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setAutosaveSettings#6878c0 as nil")
	}
	b.ObjStart()
	b.PutID("setAutosaveSettings")
	b.Comma()
	b.FieldStart("scope")
	if s.Scope == nil {
		return fmt.Errorf("unable to encode setAutosaveSettings#6878c0: field scope is nil")
	}
	if err := s.Scope.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setAutosaveSettings#6878c0: field scope: %w", err)
	}
	b.Comma()
	b.FieldStart("settings")
	if err := s.Settings.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setAutosaveSettings#6878c0: field settings: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetAutosaveSettingsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setAutosaveSettings#6878c0 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setAutosaveSettings"); err != nil {
				return fmt.Errorf("unable to decode setAutosaveSettings#6878c0: %w", err)
			}
		case "scope":
			value, err := DecodeTDLibJSONAutosaveSettingsScope(b)
			if err != nil {
				return fmt.Errorf("unable to decode setAutosaveSettings#6878c0: field scope: %w", err)
			}
			s.Scope = value
		case "settings":
			if err := s.Settings.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setAutosaveSettings#6878c0: field settings: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetScope returns value of Scope field.
func (s *SetAutosaveSettingsRequest) GetScope() (value AutosaveSettingsScopeClass) {
	if s == nil {
		return
	}
	return s.Scope
}

// GetSettings returns value of Settings field.
func (s *SetAutosaveSettingsRequest) GetSettings() (value ScopeAutosaveSettings) {
	if s == nil {
		return
	}
	return s.Settings
}

// SetAutosaveSettings invokes method setAutosaveSettings#6878c0 returning error if any.
func (c *Client) SetAutosaveSettings(ctx context.Context, request *SetAutosaveSettingsRequest) error {
	var ok Ok

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