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

// SetPasswordRequest represents TL type `setPassword#b8db46dd`.
type SetPasswordRequest struct {
	// Previous 2-step verification password of the user
	OldPassword string
	// New 2-step verification password of the user; may be empty to remove the password
	NewPassword string
	// New password hint; may be empty
	NewHint string
	// Pass true to change also the recovery email address
	SetRecoveryEmailAddress bool
	// New recovery email address; may be empty
	NewRecoveryEmailAddress string
}

// SetPasswordRequestTypeID is TL type id of SetPasswordRequest.
const SetPasswordRequestTypeID = 0xb8db46dd

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

func (s *SetPasswordRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.OldPassword == "") {
		return false
	}
	if !(s.NewPassword == "") {
		return false
	}
	if !(s.NewHint == "") {
		return false
	}
	if !(s.SetRecoveryEmailAddress == false) {
		return false
	}
	if !(s.NewRecoveryEmailAddress == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetPasswordRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setPassword",
		ID:   SetPasswordRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OldPassword",
			SchemaName: "old_password",
		},
		{
			Name:       "NewPassword",
			SchemaName: "new_password",
		},
		{
			Name:       "NewHint",
			SchemaName: "new_hint",
		},
		{
			Name:       "SetRecoveryEmailAddress",
			SchemaName: "set_recovery_email_address",
		},
		{
			Name:       "NewRecoveryEmailAddress",
			SchemaName: "new_recovery_email_address",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetPasswordRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPassword#b8db46dd as nil")
	}
	b.PutID(SetPasswordRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetPasswordRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPassword#b8db46dd as nil")
	}
	b.PutString(s.OldPassword)
	b.PutString(s.NewPassword)
	b.PutString(s.NewHint)
	b.PutBool(s.SetRecoveryEmailAddress)
	b.PutString(s.NewRecoveryEmailAddress)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetPasswordRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPassword#b8db46dd to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setPassword#b8db46dd: field old_password: %w", err)
		}
		s.OldPassword = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setPassword#b8db46dd: field new_password: %w", err)
		}
		s.NewPassword = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setPassword#b8db46dd: field new_hint: %w", err)
		}
		s.NewHint = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode setPassword#b8db46dd: field set_recovery_email_address: %w", err)
		}
		s.SetRecoveryEmailAddress = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setPassword#b8db46dd: field new_recovery_email_address: %w", err)
		}
		s.NewRecoveryEmailAddress = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetPasswordRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setPassword#b8db46dd as nil")
	}
	b.ObjStart()
	b.PutID("setPassword")
	b.Comma()
	b.FieldStart("old_password")
	b.PutString(s.OldPassword)
	b.Comma()
	b.FieldStart("new_password")
	b.PutString(s.NewPassword)
	b.Comma()
	b.FieldStart("new_hint")
	b.PutString(s.NewHint)
	b.Comma()
	b.FieldStart("set_recovery_email_address")
	b.PutBool(s.SetRecoveryEmailAddress)
	b.Comma()
	b.FieldStart("new_recovery_email_address")
	b.PutString(s.NewRecoveryEmailAddress)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setPassword"); err != nil {
				return fmt.Errorf("unable to decode setPassword#b8db46dd: %w", err)
			}
		case "old_password":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setPassword#b8db46dd: field old_password: %w", err)
			}
			s.OldPassword = value
		case "new_password":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setPassword#b8db46dd: field new_password: %w", err)
			}
			s.NewPassword = value
		case "new_hint":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setPassword#b8db46dd: field new_hint: %w", err)
			}
			s.NewHint = value
		case "set_recovery_email_address":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode setPassword#b8db46dd: field set_recovery_email_address: %w", err)
			}
			s.SetRecoveryEmailAddress = value
		case "new_recovery_email_address":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setPassword#b8db46dd: field new_recovery_email_address: %w", err)
			}
			s.NewRecoveryEmailAddress = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOldPassword returns value of OldPassword field.
func (s *SetPasswordRequest) GetOldPassword() (value string) {
	if s == nil {
		return
	}
	return s.OldPassword
}

// GetNewPassword returns value of NewPassword field.
func (s *SetPasswordRequest) GetNewPassword() (value string) {
	if s == nil {
		return
	}
	return s.NewPassword
}

// GetNewHint returns value of NewHint field.
func (s *SetPasswordRequest) GetNewHint() (value string) {
	if s == nil {
		return
	}
	return s.NewHint
}

// GetSetRecoveryEmailAddress returns value of SetRecoveryEmailAddress field.
func (s *SetPasswordRequest) GetSetRecoveryEmailAddress() (value bool) {
	if s == nil {
		return
	}
	return s.SetRecoveryEmailAddress
}

// GetNewRecoveryEmailAddress returns value of NewRecoveryEmailAddress field.
func (s *SetPasswordRequest) GetNewRecoveryEmailAddress() (value string) {
	if s == nil {
		return
	}
	return s.NewRecoveryEmailAddress
}

// SetPassword invokes method setPassword#b8db46dd returning error if any.
func (c *Client) SetPassword(ctx context.Context, request *SetPasswordRequest) (*PasswordState, error) {
	var result PasswordState

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