"""
setup.py for barcode-pao-native

Build and install:
    pip install -e .

Build wheel:
    pip install build
    python -m build
"""

import os
import sys
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext

# pybind11 is required for building
try:
    import pybind11
except ImportError:
    print("pybind11 is required for building. Install with: pip install pybind11")
    sys.exit(1)


class get_pybind_include:
    """Helper class to determine the pybind11 include path."""

    def __str__(self):
        return pybind11.get_include()


# Get the parent directory (where C++ source files are located)
cpp_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# List of C++ source files (relative paths for setuptools compatibility)
cpp_sources = [
    "src/barcode_bindings_pybind.cpp",
    "../barcode_base.cpp",
    "../BarcodeBase1D.cpp",
    "../BarcodeBase2D.cpp",
    "../code39.cpp",
    "../code93.cpp",
    "../code128.cpp",
    "../gs1_128.cpp",
    "../nw7.cpp",
    "../itf.cpp",
    "../Matrix2of5.cpp",
    "../NEC2of5.cpp",
    "../jan8.cpp",
    "../jan13.cpp",
    "../upc_a.cpp",
    "../upc_e.cpp",
    "../gs1_databar_14.cpp",
    "../gs1_databar_limited.cpp",
    "../gs1_databar_expanded.cpp",
    "../YubinCustomer.cpp",
    "../DataMatrix.cpp",
    "../QR.cpp",
    "../PDF417.cpp",
    "../ProductInfo.cpp",
]

# Include directories
include_dirs = [
    get_pybind_include(),
    cpp_dir,
]

# Library directories
library_dirs = []

if sys.platform == "win32":
    # Windows: NuGet package paths
    include_dirs += [
        os.path.join(cpp_dir, "packages", "sdl2.nuget.2.30.9", "build", "native", "include"),
        os.path.join(cpp_dir, "packages", "sdl2_image.nuget.2.8.2", "build", "native", "include"),
        os.path.join(cpp_dir, "packages", "sdl2_ttf.nuget.2.22.0", "build", "native", "include"),
    ]
    library_dirs += [
        os.path.join(cpp_dir, "packages", "sdl2.nuget.2.30.9", "build", "native", "lib", "x64"),
        os.path.join(cpp_dir, "packages", "sdl2_image.nuget.2.8.2", "build", "native", "lib", "x64"),
        os.path.join(cpp_dir, "packages", "sdl2_ttf.nuget.2.22.0", "build", "native", "lib", "x64"),
    ]
else:
    # Linux/macOS: system SDL2 paths
    include_dirs.append("/usr/include/SDL2")

# Libraries to link
libraries = ["SDL2", "SDL2_image", "SDL2_ttf"]

# Compiler and linker flags
extra_compile_args = []
extra_link_args = []

if sys.platform == "win32":
    extra_compile_args = ["/std:c++17", "/EHsc", "/O2"]
elif sys.platform == "darwin":
    extra_compile_args = ["-std=c++17", "-O3", "-fvisibility=hidden"]
    extra_link_args = ["-stdlib=libc++"]
else:
    extra_compile_args = ["-std=c++17", "-O3", "-fvisibility=hidden"]


ext_modules = [
    Extension(
        "barcode_pao_native._barcode_pao_native",
        sources=cpp_sources,
        include_dirs=include_dirs,
        library_dirs=library_dirs,
        libraries=libraries,
        extra_compile_args=extra_compile_args,
        extra_link_args=extra_link_args,
        language="c++",
    ),
]


class BuildExt(build_ext):
    """A custom build extension for adding compiler-specific options."""

    def build_extensions(self):
        # Check if compiler is MSVC
        if self.compiler.compiler_type == "msvc":
            for ext in self.extensions:
                ext.extra_compile_args = ["/std:c++17", "/EHsc", "/O2"]
        else:
            for ext in self.extensions:
                ext.extra_compile_args = ["-std=c++17", "-O3", "-fvisibility=hidden"]

        build_ext.build_extensions(self)


setup(
    name="barcode-pao",
    version="1.0.1",
    author="Pao",
    author_email="info@pao.ac",
    description="High-performance barcode generation library for Python (Native C++ Extension)",
    long_description=open("README.md", encoding="utf-8").read() if os.path.exists("README.md") else "",
    long_description_content_type="text/markdown",
    url="https://github.com/pao-ac/barcode-pao-native",
    packages=["barcode_pao_native"],
    package_data={"barcode_pao_native": ["*.dll", "fonts/*.ttf"]},
    ext_modules=ext_modules,
    cmdclass={"build_ext": BuildExt},
    python_requires=">=3.8",
    install_requires=[],
    setup_requires=["pybind11>=2.10.0"],
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Programming Language :: C++",
        "Topic :: Multimedia :: Graphics",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
    keywords="barcode qr code128 code39 datamatrix pdf417 ean jan upc gs1",
)
