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

// UnconfirmedSession represents TL type `unconfirmedSession#850d49f9`.
type UnconfirmedSession struct {
	// Session identifier
	ID int64
	// Point in time (Unix timestamp) when the user has logged in
	LogInDate int32
	// Model of the device that was used for the session creation, as provided by the
	// application
	DeviceModel string
	// A human-readable description of the location from which the session was created, based
	// on the IP address
	Location string
}

// UnconfirmedSessionTypeID is TL type id of UnconfirmedSession.
const UnconfirmedSessionTypeID = 0x850d49f9

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

func (u *UnconfirmedSession) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.ID == 0) {
		return false
	}
	if !(u.LogInDate == 0) {
		return false
	}
	if !(u.DeviceModel == "") {
		return false
	}
	if !(u.Location == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (u *UnconfirmedSession) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "unconfirmedSession",
		ID:   UnconfirmedSessionTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "LogInDate",
			SchemaName: "log_in_date",
		},
		{
			Name:       "DeviceModel",
			SchemaName: "device_model",
		},
		{
			Name:       "Location",
			SchemaName: "location",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UnconfirmedSession) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode unconfirmedSession#850d49f9 as nil")
	}
	b.PutID(UnconfirmedSessionTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UnconfirmedSession) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode unconfirmedSession#850d49f9 as nil")
	}
	b.PutLong(u.ID)
	b.PutInt32(u.LogInDate)
	b.PutString(u.DeviceModel)
	b.PutString(u.Location)
	return nil
}

// Decode implements bin.Decoder.
func (u *UnconfirmedSession) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode unconfirmedSession#850d49f9 to nil")
	}
	if err := b.ConsumeID(UnconfirmedSessionTypeID); err != nil {
		return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *UnconfirmedSession) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode unconfirmedSession#850d49f9 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field id: %w", err)
		}
		u.ID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field log_in_date: %w", err)
		}
		u.LogInDate = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field device_model: %w", err)
		}
		u.DeviceModel = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field location: %w", err)
		}
		u.Location = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UnconfirmedSession) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode unconfirmedSession#850d49f9 as nil")
	}
	b.ObjStart()
	b.PutID("unconfirmedSession")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(u.ID)
	b.Comma()
	b.FieldStart("log_in_date")
	b.PutInt32(u.LogInDate)
	b.Comma()
	b.FieldStart("device_model")
	b.PutString(u.DeviceModel)
	b.Comma()
	b.FieldStart("location")
	b.PutString(u.Location)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (u *UnconfirmedSession) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode unconfirmedSession#850d49f9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("unconfirmedSession"); err != nil {
				return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field id: %w", err)
			}
			u.ID = value
		case "log_in_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field log_in_date: %w", err)
			}
			u.LogInDate = value
		case "device_model":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field device_model: %w", err)
			}
			u.DeviceModel = value
		case "location":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode unconfirmedSession#850d49f9: field location: %w", err)
			}
			u.Location = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

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