#===============================================================================
# Imports
#===============================================================================
import sys
import ctypes

from .wintypes import *

#===============================================================================
# Globals/Aliases
#===============================================================================
PPYTHON = PVOID
PPYTRACEFUNC = PVOID
PUSERDATA = PVOID

#===============================================================================
# Classes
#===============================================================================
class TRACE_STORE_METADATA(Structure):
    _fields_ = [
        ('NumberOfRecords', ULARGE_INTEGER),
        ('RecordSize', LARGE_INTEGER),
    ]

PTRACE_STORE_METADATA = POINTER(TRACE_STORE_METADATA)

class _TRACE_STORE_METADATA(Union):
    _fields_ = [
        ('Metadata', TRACE_STORE_METADATA),
        ('pMetadata', PTRACE_STORE_METADATA),
    ]

class TRACE_STORE_MEMORY_MAP(Structure):
    _fields_ = [
        ('SlimReadWriteLock',   SRWLOCK),
        ('MappingHandle',       HANDLE),
        ('MappingSize',         LARGE_INTEGER),
        ('BaseAddress',         PVOID),
        ('ExtendAtAddress',     PVOID),
        ('EndAddress',          PVOID),
        ('PrevAddress',         PVOID),
        ('NextAddress',         PVOID),
    ]
PTRACE_STORE_MEMORY_MAP = POINTER(TRACE_STORE_MEMORY_MAP)

class TRACE_STORE(Structure):
    _fields_ = [
        ('TraceStores', PVOID),
        ('FileHandle', HANDLE),
        ('InitialSize', LARGE_INTEGER),
        ('ExtensionSize', LARGE_INTEGER),
        ('FileInfo', FILE_STANDARD_INFO),
        ('CriticalSection', PCRITICAL_SECTION),
        ('DroppedRecords', ULONG),
        ('MemoryMap', TRACE_STORE_MEMORY_MAP),
        ('NextMemoryMap', TRACE_STORE_MEMORY_MAP),
        ('MetadataStore', PVOID),
        ('AllocateRecords', PVOID),
        ('', _TRACE_STORE_METADATA),
    ]
PTRACE_STORE = POINTER(TRACE_STORE)

class TRACE_STORES(Structure):
    _fields_ = [
        ('Size',                USHORT),
        ('NumberOfTraceStores', USHORT),
        ('Reserved',            ULONG),
        ('Events',              TRACE_STORE),
        ('Frames',              TRACE_STORE),
        ('Modules',             TRACE_STORE),
        ('Functions',           TRACE_STORE),
        ('Exceptions',          TRACE_STORE),
        ('Lines',               TRACE_STORE),
        ('EventsMetadata',      TRACE_STORE),
        ('FramesMetadata',      TRACE_STORE),
        ('ModulesMetadata',     TRACE_STORE),
        ('FunctionsMetadata',   TRACE_STORE),
        ('ExceptionsMetadata',  TRACE_STORE),
        ('LinesMetadata',       TRACE_STORE),
    ]
PTRACE_STORES = POINTER(TRACE_STORES)

class TRACE_SESSION(Structure):
    _fields_ = [
        ('Size',            DWORD),
        ('SessionId',       LARGE_INTEGER),
        ('MachineGuid',     GUID),
        ('Sid',             PVOID),
        ('UserName',        PCWSTR),
        ('ComputerName',    PCWSTR),
        ('DomainName',      PCWSTR),
        ('SystemTime',      FILETIME),
    ]
PTRACE_SESSION = POINTER(TRACE_SESSION)