// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// AccountSetAuthorizationTTLRequest represents TL type `account.setAuthorizationTTL#bf899aa0`.
// Set time-to-live of current session
//
// See https://core.telegram.org/method/account.setAuthorizationTTL for reference.
type AccountSetAuthorizationTTLRequest struct {
	// Time-to-live of current session in days
	AuthorizationTTLDays int
}

// AccountSetAuthorizationTTLRequestTypeID is TL type id of AccountSetAuthorizationTTLRequest.
const AccountSetAuthorizationTTLRequestTypeID = 0xbf899aa0

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

func (s *AccountSetAuthorizationTTLRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.AuthorizationTTLDays == 0) {
		return false
	}

	return true
}

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

// FillFrom fills AccountSetAuthorizationTTLRequest from given interface.
func (s *AccountSetAuthorizationTTLRequest) FillFrom(from interface {
	GetAuthorizationTTLDays() (value int)
}) {
	s.AuthorizationTTLDays = from.GetAuthorizationTTLDays()
}

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

// TypeName returns name of type in TL schema.
func (*AccountSetAuthorizationTTLRequest) TypeName() string {
	return "account.setAuthorizationTTL"
}

// TypeInfo returns info about TL type.
func (s *AccountSetAuthorizationTTLRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.setAuthorizationTTL",
		ID:   AccountSetAuthorizationTTLRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "AuthorizationTTLDays",
			SchemaName: "authorization_ttl_days",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *AccountSetAuthorizationTTLRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode account.setAuthorizationTTL#bf899aa0 as nil")
	}
	b.PutID(AccountSetAuthorizationTTLRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *AccountSetAuthorizationTTLRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode account.setAuthorizationTTL#bf899aa0 as nil")
	}
	b.PutInt(s.AuthorizationTTLDays)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *AccountSetAuthorizationTTLRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode account.setAuthorizationTTL#bf899aa0 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode account.setAuthorizationTTL#bf899aa0: field authorization_ttl_days: %w", err)
		}
		s.AuthorizationTTLDays = value
	}
	return nil
}

// GetAuthorizationTTLDays returns value of AuthorizationTTLDays field.
func (s *AccountSetAuthorizationTTLRequest) GetAuthorizationTTLDays() (value int) {
	if s == nil {
		return
	}
	return s.AuthorizationTTLDays
}

// AccountSetAuthorizationTTL invokes method account.setAuthorizationTTL#bf899aa0 returning error if any.
// Set time-to-live of current session
//
// Possible errors:
//
//	406 FRESH_RESET_AUTHORISATION_FORBIDDEN: You can't logout other sessions if less than 24 hours have passed since you logged on the current session.
//	400 TTL_DAYS_INVALID: The provided TTL is invalid.
//
// See https://core.telegram.org/method/account.setAuthorizationTTL for reference.
func (c *Client) AccountSetAuthorizationTTL(ctx context.Context, authorizationttldays int) (bool, error) {
	var result BoolBox

	request := &AccountSetAuthorizationTTLRequest{
		AuthorizationTTLDays: authorizationttldays,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
