# -*- mode: python ; coding: utf-8 -*-

import sys
import os
from PyInstaller.utils.hooks import collect_submodules, collect_data_files

# ========================================================
# 1. 工程配置区 (Project Config)
# ========================================================
block_cipher = None
ENTRY_POINT = 'start_parse.py'
APP_NAME = 'start_parse'  # 打包后生成的文件夹名和 exe 名

# ========================================================
# 2. 依赖分析 (Dependency Analysis)
# ========================================================
hidden_imports = [
    # eegParser 依赖
    'numpy',
    'numpy.lib.stride_tricks',
    'pandas',
    'scipy',
    'scipy.io',
    'scipy.io.savemat',
    'scipy.signal',

    # SunnyLinker 依赖
    'serial',
    'serial.serialutil',
    'socket',

    # zmq 通信依赖
    'zmq',
    'zmq.asyncio',

    # 其他可能遗漏的模块
    'threading',
    'datetime',
]

# 收集 zmq 的所有子模块
try:
    hidden_imports += collect_submodules('zmq')
except:
    pass

# ========================================================
# 3. 资源锚定 (Data Anchoring)
# ========================================================
# 打包时需要包含的资源文件
datas = [
    ('xy_64.xlsx', '.'),  # 电极位置文件
]

# 收集 mne 的数据文件（如果有）
try:
    datas += collect_data_files('mne')
except:
    pass

# ========================================================
# 4. 构建流程 (Build Process)
# ========================================================
a = Analysis(
    [ENTRY_POINT],
    pathex=[],
    binaries=[],
    datas=datas,
    hiddenimports=hidden_imports,
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=['tkinter', 'PyQt5', 'PySide2', 'IPython', 'matplotlib'],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name=APP_NAME,
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

# ========================================================
# 5. 打包模式: OneDir (单文件夹)
# ========================================================
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name=APP_NAME,
)
