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

// SetUserPrivacySettingRulesRequest represents TL type `setUserPrivacySettingRules#e3c230fb`.
type SetUserPrivacySettingRulesRequest struct {
	// The privacy setting
	Setting UserPrivacySettingClass
	// The new privacy rules
	Rules UserPrivacySettingRules
}

// SetUserPrivacySettingRulesRequestTypeID is TL type id of SetUserPrivacySettingRulesRequest.
const SetUserPrivacySettingRulesRequestTypeID = 0xe3c230fb

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

func (s *SetUserPrivacySettingRulesRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Setting == nil) {
		return false
	}
	if !(s.Rules.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetUserPrivacySettingRulesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setUserPrivacySettingRules",
		ID:   SetUserPrivacySettingRulesRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Setting",
			SchemaName: "setting",
		},
		{
			Name:       "Rules",
			SchemaName: "rules",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetUserPrivacySettingRulesRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setUserPrivacySettingRules#e3c230fb as nil")
	}
	b.PutID(SetUserPrivacySettingRulesRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetUserPrivacySettingRulesRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setUserPrivacySettingRules#e3c230fb as nil")
	}
	if s.Setting == nil {
		return fmt.Errorf("unable to encode setUserPrivacySettingRules#e3c230fb: field setting is nil")
	}
	if err := s.Setting.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setUserPrivacySettingRules#e3c230fb: field setting: %w", err)
	}
	if err := s.Rules.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setUserPrivacySettingRules#e3c230fb: field rules: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetUserPrivacySettingRulesRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setUserPrivacySettingRules#e3c230fb to nil")
	}
	{
		value, err := DecodeUserPrivacySetting(b)
		if err != nil {
			return fmt.Errorf("unable to decode setUserPrivacySettingRules#e3c230fb: field setting: %w", err)
		}
		s.Setting = value
	}
	{
		if err := s.Rules.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setUserPrivacySettingRules#e3c230fb: field rules: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetUserPrivacySettingRulesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setUserPrivacySettingRules#e3c230fb as nil")
	}
	b.ObjStart()
	b.PutID("setUserPrivacySettingRules")
	b.Comma()
	b.FieldStart("setting")
	if s.Setting == nil {
		return fmt.Errorf("unable to encode setUserPrivacySettingRules#e3c230fb: field setting is nil")
	}
	if err := s.Setting.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setUserPrivacySettingRules#e3c230fb: field setting: %w", err)
	}
	b.Comma()
	b.FieldStart("rules")
	if err := s.Rules.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setUserPrivacySettingRules#e3c230fb: field rules: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setUserPrivacySettingRules"); err != nil {
				return fmt.Errorf("unable to decode setUserPrivacySettingRules#e3c230fb: %w", err)
			}
		case "setting":
			value, err := DecodeTDLibJSONUserPrivacySetting(b)
			if err != nil {
				return fmt.Errorf("unable to decode setUserPrivacySettingRules#e3c230fb: field setting: %w", err)
			}
			s.Setting = value
		case "rules":
			if err := s.Rules.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setUserPrivacySettingRules#e3c230fb: field rules: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSetting returns value of Setting field.
func (s *SetUserPrivacySettingRulesRequest) GetSetting() (value UserPrivacySettingClass) {
	if s == nil {
		return
	}
	return s.Setting
}

// GetRules returns value of Rules field.
func (s *SetUserPrivacySettingRulesRequest) GetRules() (value UserPrivacySettingRules) {
	if s == nil {
		return
	}
	return s.Rules
}

// SetUserPrivacySettingRules invokes method setUserPrivacySettingRules#e3c230fb returning error if any.
func (c *Client) SetUserPrivacySettingRules(ctx context.Context, request *SetUserPrivacySettingRulesRequest) error {
	var ok Ok

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