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

// Session represents TL type `session#9759a3c`.
type Session struct {
	// Session identifier
	ID int64
	// True, if this session is the current session
	IsCurrent bool
	// True, if a 2-step verification password is needed to complete authorization of the
	// session
	IsPasswordPending bool
	// True, if the session wasn't confirmed from another session
	IsUnconfirmed bool
	// True, if incoming secret chats can be accepted by the session
	CanAcceptSecretChats bool
	// True, if incoming calls can be accepted by the session
	CanAcceptCalls bool
	// Session type based on the system and application version, which can be used to display
	// a corresponding icon
	Type SessionTypeClass
	// Telegram API identifier, as provided by the application
	APIID int32
	// Name of the application, as provided by the application
	ApplicationName string
	// The version of the application, as provided by the application
	ApplicationVersion string
	// True, if the application is an official application or uses the api_id of an official
	// application
	IsOfficialApplication bool
	// Model of the device the application has been run or is running on, as provided by the
	// application
	DeviceModel string
	// Operating system the application has been run or is running on, as provided by the
	// application
	Platform string
	// Version of the operating system the application has been run or is running on, as
	// provided by the application
	SystemVersion string
	// Point in time (Unix timestamp) when the user has logged in
	LogInDate int32
	// Point in time (Unix timestamp) when the session was last used
	LastActiveDate int32
	// IP address from which the session was created, in human-readable format
	IPAddress string
	// A human-readable description of the location from which the session was created, based
	// on the IP address
	Location string
}

// SessionTypeID is TL type id of Session.
const SessionTypeID = 0x9759a3c

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

func (s *Session) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ID == 0) {
		return false
	}
	if !(s.IsCurrent == false) {
		return false
	}
	if !(s.IsPasswordPending == false) {
		return false
	}
	if !(s.IsUnconfirmed == false) {
		return false
	}
	if !(s.CanAcceptSecretChats == false) {
		return false
	}
	if !(s.CanAcceptCalls == false) {
		return false
	}
	if !(s.Type == nil) {
		return false
	}
	if !(s.APIID == 0) {
		return false
	}
	if !(s.ApplicationName == "") {
		return false
	}
	if !(s.ApplicationVersion == "") {
		return false
	}
	if !(s.IsOfficialApplication == false) {
		return false
	}
	if !(s.DeviceModel == "") {
		return false
	}
	if !(s.Platform == "") {
		return false
	}
	if !(s.SystemVersion == "") {
		return false
	}
	if !(s.LogInDate == 0) {
		return false
	}
	if !(s.LastActiveDate == 0) {
		return false
	}
	if !(s.IPAddress == "") {
		return false
	}
	if !(s.Location == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *Session) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "session",
		ID:   SessionTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "IsCurrent",
			SchemaName: "is_current",
		},
		{
			Name:       "IsPasswordPending",
			SchemaName: "is_password_pending",
		},
		{
			Name:       "IsUnconfirmed",
			SchemaName: "is_unconfirmed",
		},
		{
			Name:       "CanAcceptSecretChats",
			SchemaName: "can_accept_secret_chats",
		},
		{
			Name:       "CanAcceptCalls",
			SchemaName: "can_accept_calls",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "APIID",
			SchemaName: "api_id",
		},
		{
			Name:       "ApplicationName",
			SchemaName: "application_name",
		},
		{
			Name:       "ApplicationVersion",
			SchemaName: "application_version",
		},
		{
			Name:       "IsOfficialApplication",
			SchemaName: "is_official_application",
		},
		{
			Name:       "DeviceModel",
			SchemaName: "device_model",
		},
		{
			Name:       "Platform",
			SchemaName: "platform",
		},
		{
			Name:       "SystemVersion",
			SchemaName: "system_version",
		},
		{
			Name:       "LogInDate",
			SchemaName: "log_in_date",
		},
		{
			Name:       "LastActiveDate",
			SchemaName: "last_active_date",
		},
		{
			Name:       "IPAddress",
			SchemaName: "ip_address",
		},
		{
			Name:       "Location",
			SchemaName: "location",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *Session) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode session#9759a3c as nil")
	}
	b.PutID(SessionTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *Session) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode session#9759a3c as nil")
	}
	b.PutLong(s.ID)
	b.PutBool(s.IsCurrent)
	b.PutBool(s.IsPasswordPending)
	b.PutBool(s.IsUnconfirmed)
	b.PutBool(s.CanAcceptSecretChats)
	b.PutBool(s.CanAcceptCalls)
	if s.Type == nil {
		return fmt.Errorf("unable to encode session#9759a3c: field type is nil")
	}
	if err := s.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode session#9759a3c: field type: %w", err)
	}
	b.PutInt32(s.APIID)
	b.PutString(s.ApplicationName)
	b.PutString(s.ApplicationVersion)
	b.PutBool(s.IsOfficialApplication)
	b.PutString(s.DeviceModel)
	b.PutString(s.Platform)
	b.PutString(s.SystemVersion)
	b.PutInt32(s.LogInDate)
	b.PutInt32(s.LastActiveDate)
	b.PutString(s.IPAddress)
	b.PutString(s.Location)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *Session) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode session#9759a3c to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field id: %w", err)
		}
		s.ID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field is_current: %w", err)
		}
		s.IsCurrent = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field is_password_pending: %w", err)
		}
		s.IsPasswordPending = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field is_unconfirmed: %w", err)
		}
		s.IsUnconfirmed = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field can_accept_secret_chats: %w", err)
		}
		s.CanAcceptSecretChats = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field can_accept_calls: %w", err)
		}
		s.CanAcceptCalls = value
	}
	{
		value, err := DecodeSessionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field type: %w", err)
		}
		s.Type = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field api_id: %w", err)
		}
		s.APIID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field application_name: %w", err)
		}
		s.ApplicationName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field application_version: %w", err)
		}
		s.ApplicationVersion = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field is_official_application: %w", err)
		}
		s.IsOfficialApplication = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field device_model: %w", err)
		}
		s.DeviceModel = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field platform: %w", err)
		}
		s.Platform = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field system_version: %w", err)
		}
		s.SystemVersion = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field log_in_date: %w", err)
		}
		s.LogInDate = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field last_active_date: %w", err)
		}
		s.LastActiveDate = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field ip_address: %w", err)
		}
		s.IPAddress = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode session#9759a3c: field location: %w", err)
		}
		s.Location = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *Session) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode session#9759a3c as nil")
	}
	b.ObjStart()
	b.PutID("session")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(s.ID)
	b.Comma()
	b.FieldStart("is_current")
	b.PutBool(s.IsCurrent)
	b.Comma()
	b.FieldStart("is_password_pending")
	b.PutBool(s.IsPasswordPending)
	b.Comma()
	b.FieldStart("is_unconfirmed")
	b.PutBool(s.IsUnconfirmed)
	b.Comma()
	b.FieldStart("can_accept_secret_chats")
	b.PutBool(s.CanAcceptSecretChats)
	b.Comma()
	b.FieldStart("can_accept_calls")
	b.PutBool(s.CanAcceptCalls)
	b.Comma()
	b.FieldStart("type")
	if s.Type == nil {
		return fmt.Errorf("unable to encode session#9759a3c: field type is nil")
	}
	if err := s.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode session#9759a3c: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("api_id")
	b.PutInt32(s.APIID)
	b.Comma()
	b.FieldStart("application_name")
	b.PutString(s.ApplicationName)
	b.Comma()
	b.FieldStart("application_version")
	b.PutString(s.ApplicationVersion)
	b.Comma()
	b.FieldStart("is_official_application")
	b.PutBool(s.IsOfficialApplication)
	b.Comma()
	b.FieldStart("device_model")
	b.PutString(s.DeviceModel)
	b.Comma()
	b.FieldStart("platform")
	b.PutString(s.Platform)
	b.Comma()
	b.FieldStart("system_version")
	b.PutString(s.SystemVersion)
	b.Comma()
	b.FieldStart("log_in_date")
	b.PutInt32(s.LogInDate)
	b.Comma()
	b.FieldStart("last_active_date")
	b.PutInt32(s.LastActiveDate)
	b.Comma()
	b.FieldStart("ip_address")
	b.PutString(s.IPAddress)
	b.Comma()
	b.FieldStart("location")
	b.PutString(s.Location)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("session"); err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field id: %w", err)
			}
			s.ID = value
		case "is_current":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field is_current: %w", err)
			}
			s.IsCurrent = value
		case "is_password_pending":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field is_password_pending: %w", err)
			}
			s.IsPasswordPending = value
		case "is_unconfirmed":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field is_unconfirmed: %w", err)
			}
			s.IsUnconfirmed = value
		case "can_accept_secret_chats":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field can_accept_secret_chats: %w", err)
			}
			s.CanAcceptSecretChats = value
		case "can_accept_calls":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field can_accept_calls: %w", err)
			}
			s.CanAcceptCalls = value
		case "type":
			value, err := DecodeTDLibJSONSessionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field type: %w", err)
			}
			s.Type = value
		case "api_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field api_id: %w", err)
			}
			s.APIID = value
		case "application_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field application_name: %w", err)
			}
			s.ApplicationName = value
		case "application_version":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field application_version: %w", err)
			}
			s.ApplicationVersion = value
		case "is_official_application":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field is_official_application: %w", err)
			}
			s.IsOfficialApplication = value
		case "device_model":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field device_model: %w", err)
			}
			s.DeviceModel = value
		case "platform":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field platform: %w", err)
			}
			s.Platform = value
		case "system_version":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field system_version: %w", err)
			}
			s.SystemVersion = value
		case "log_in_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field log_in_date: %w", err)
			}
			s.LogInDate = value
		case "last_active_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field last_active_date: %w", err)
			}
			s.LastActiveDate = value
		case "ip_address":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field ip_address: %w", err)
			}
			s.IPAddress = value
		case "location":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode session#9759a3c: field location: %w", err)
			}
			s.Location = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (s *Session) GetID() (value int64) {
	if s == nil {
		return
	}
	return s.ID
}

// GetIsCurrent returns value of IsCurrent field.
func (s *Session) GetIsCurrent() (value bool) {
	if s == nil {
		return
	}
	return s.IsCurrent
}

// GetIsPasswordPending returns value of IsPasswordPending field.
func (s *Session) GetIsPasswordPending() (value bool) {
	if s == nil {
		return
	}
	return s.IsPasswordPending
}

// GetIsUnconfirmed returns value of IsUnconfirmed field.
func (s *Session) GetIsUnconfirmed() (value bool) {
	if s == nil {
		return
	}
	return s.IsUnconfirmed
}

// GetCanAcceptSecretChats returns value of CanAcceptSecretChats field.
func (s *Session) GetCanAcceptSecretChats() (value bool) {
	if s == nil {
		return
	}
	return s.CanAcceptSecretChats
}

// GetCanAcceptCalls returns value of CanAcceptCalls field.
func (s *Session) GetCanAcceptCalls() (value bool) {
	if s == nil {
		return
	}
	return s.CanAcceptCalls
}

// GetType returns value of Type field.
func (s *Session) GetType() (value SessionTypeClass) {
	if s == nil {
		return
	}
	return s.Type
}

// GetAPIID returns value of APIID field.
func (s *Session) GetAPIID() (value int32) {
	if s == nil {
		return
	}
	return s.APIID
}

// GetApplicationName returns value of ApplicationName field.
func (s *Session) GetApplicationName() (value string) {
	if s == nil {
		return
	}
	return s.ApplicationName
}

// GetApplicationVersion returns value of ApplicationVersion field.
func (s *Session) GetApplicationVersion() (value string) {
	if s == nil {
		return
	}
	return s.ApplicationVersion
}

// GetIsOfficialApplication returns value of IsOfficialApplication field.
func (s *Session) GetIsOfficialApplication() (value bool) {
	if s == nil {
		return
	}
	return s.IsOfficialApplication
}

// GetDeviceModel returns value of DeviceModel field.
func (s *Session) GetDeviceModel() (value string) {
	if s == nil {
		return
	}
	return s.DeviceModel
}

// GetPlatform returns value of Platform field.
func (s *Session) GetPlatform() (value string) {
	if s == nil {
		return
	}
	return s.Platform
}

// GetSystemVersion returns value of SystemVersion field.
func (s *Session) GetSystemVersion() (value string) {
	if s == nil {
		return
	}
	return s.SystemVersion
}

// GetLogInDate returns value of LogInDate field.
func (s *Session) GetLogInDate() (value int32) {
	if s == nil {
		return
	}
	return s.LogInDate
}

// GetLastActiveDate returns value of LastActiveDate field.
func (s *Session) GetLastActiveDate() (value int32) {
	if s == nil {
		return
	}
	return s.LastActiveDate
}

// GetIPAddress returns value of IPAddress field.
func (s *Session) GetIPAddress() (value string) {
	if s == nil {
		return
	}
	return s.IPAddress
}

// GetLocation returns value of Location field.
func (s *Session) GetLocation() (value string) {
	if s == nil {
		return
	}
	return s.Location
}
