import express from 'express';
import os from 'os';
import logger from '../utils/logger.js';
import { JavaManager, VcRedistManager, DirectXManager } from '../modules/environment/index.js';
import { LinuxPackageManager } from '../modules/environment/packageManager.js';
import { authenticateToken } from '../middleware/auth.js';
let io = null;
let configManager;
export const setEnvironmentSocketIO = (socketIO) => {
    io = socketIO;
};
export const setEnvironmentConfigManager = (config) => {
    configManager = config;
};
const router = express.Router();
const javaManager = new JavaManager();
const vcRedistManager = new VcRedistManager();
const directxManager = new DirectXManager();
const packageManager = new LinuxPackageManager();
router.get('/system-info', authenticateToken, async (req, res) => {
    try {
        const rawPlatform = os.platform();
        const systemInfo = {
            platform: rawPlatform,
            rawPlatform: rawPlatform,
            arch: os.arch(),
            type: os.type(),
            release: os.release()
        };
        res.json({
            success: true,
            data: systemInfo
        });
    }
    catch (error) {
        logger.error('获取系统信息失败:', error);
        res.status(500).json({
            success: false,
            message: '获取系统信息失败'
        });
    }
});
router.get('/java', authenticateToken, async (req, res) => {
    try {
        const environments = await javaManager.getJavaEnvironments();
        res.json({
            success: true,
            data: environments
        });
    }
    catch (error) {
        logger.error('获取Java环境列表失败:', error);
        res.status(500).json({
            success: false,
            message: '获取Java环境列表失败'
        });
    }
});
function validateSponsorKey() {
    try {
        if (!configManager) {
            logger.warn('ConfigManager未初始化');
            return false;
        }
        const sponsorConfig = configManager.getSponsorConfig();
        if (!sponsorConfig || !sponsorConfig.key || !sponsorConfig.isValid) {
            return false;
        }
        if (sponsorConfig.expiryTime && new Date() > new Date(sponsorConfig.expiryTime)) {
            return false;
        }
        return true;
    }
    catch (error) {
        logger.error('验证赞助者密钥失败:', error);
        return false;
    }
}
function getSponsorDownloadUrl(version, platform, arch) {
    const downloadUrls = {
        java8: {
            windows: 'https://download.xiaozhuhouses.asia/download/v1/links/4GMNQ54kGwuviwcEOfgzVCRSWT6XzNPXp-ByPPVifYk',
            linux: 'https://download.xiaozhuhouses.asia/download/v1/links/WBaVRrXptRSqi0JjLkyYKDB2bnH3T67IQzJT-iPz6bA'
        },
        java11: {
            windows: 'https://download.xiaozhuhouses.asia/download/v1/links/enN1iE0CIwgJWmzDSq8bJJeWDnC1DuCx6IE_24aWQ2s',
            linux: 'https://download.xiaozhuhouses.asia/download/v1/links/_KQdgTNVpgZJZwrLozviN3gE6ZEcEpZZf58NUL9WYOA',
            arm: 'https://download.xiaozhuhouses.asia/download/v1/links/_ya4jKkyMFfDROU87g-oo2E9UnbRaxlgp_govHyDUYU'
        },
        java17: {
            windows: 'https://download.xiaozhuhouses.asia/download/v1/links/4_q8RzaqTgDGmFHQiVz1lMaBl3hTwjAp8YmFx0GtCjs',
            linux: 'https://download.xiaozhuhouses.asia/download/v1/links/oNn4sshvtLJ3V8dJApXecT5axaRLjTBUL5lqBkz0LPs',
            arm: 'https://download.xiaozhuhouses.asia/download/v1/links/9uS3rF5DO_-c_tcaM7BykYdI6ZrtPlnj4IVyVpK4F3Y'
        },
        java21: {
            windows: 'https://download.xiaozhuhouses.asia/download/v1/links/c0Heh97uhMO3_LCfYMr9tQyYCagRpX9Wi5gbm08dtuc',
            linux: 'https://download.xiaozhuhouses.asia/download/v1/links/rFPuJ-HY7XVmg-KnBsXwvtvewxI-2orfe95G949zFa0',
            arm: 'https://download.xiaozhuhouses.asia/download/v1/links/qWLHA8eDvA55KpG9pW35Aj1Ds-CNvuWT4JbO_8zIY9U'
        },
        java25: {
            windows: 'https://download.xiaozhuhouses.asia/download/v1/links/QBmtaNmE_wEATTjQoO0AAEncTPUVjwnCofWUxPY4EH4',
            linux: 'https://download.xiaozhuhouses.asia/download/v1/links/bvANX6e9XuW_nvdO6TmE89tyepAELCyub3wsXhcZMvU',
            arm: 'https://download.xiaozhuhouses.asia/download/v1/links/k-EfIFXJeFtP2DZv-8Fn9SwLCaQWL7HhfIbTkx1xeFk'
        }
    };
    let platformKey;
    if (platform === 'win32') {
        platformKey = 'windows';
    }
    else if (arch === 'arm64' || arch === 'aarch64') {
        platformKey = 'arm';
    }
    else {
        platformKey = 'linux';
    }
    const downloadUrl = downloadUrls[version]?.[platformKey];
    if (!downloadUrl) {
        throw new Error(`不支持的版本或平台: ${version}, ${platform}, ${arch}`);
    }
    return downloadUrl;
}
function getJavaArchiveFileName(version, platform, arch) {
    const fileNames = {
        java8: {
            windows: 'openjdk-8u44-windows-i586.zip',
            linux: 'openjdk-8u44-linux-x64.tar.gz'
        },
        java11: {
            windows: 'openjdk-11.0.0.2_windows-x64.zip',
            linux: 'openjdk-11.0.0.2_linux-x64.tar.gz',
            arm: 'microsoft-jdk-11.0.29-linux-aarch64.tar.gz'
        },
        java17: {
            windows: 'openjdk-17.0.0.1+2_windows-x64_bin.zip',
            linux: 'openjdk-17.0.0.1+2_linux-x64_bin.tar.gz',
            arm: 'openjdk-17.0.2_linux-aarch64_bin.tar.gz'
        },
        java21: {
            windows: 'openjdk-21+35_windows-x64_bin.zip',
            linux: 'openjdk-21+35_linux-x64_bin.tar.gz',
            arm: 'openjdk-21_linux-aarch64_bin.tar.gz'
        },
        java25: {
            windows: 'openjdk-25+36_windows-x64_bin.zip',
            linux: 'openjdk-25+36_linux-x64_bin.tar.gz',
            arm: 'openjdk-25.0.2_linux-aarch64_bin.tar.gz'
        }
    };
    let platformKey;
    if (platform === 'win32') {
        platformKey = 'windows';
    }
    else if (arch === 'arm64' || arch === 'aarch64') {
        platformKey = 'arm';
    }
    else {
        platformKey = 'linux';
    }
    const fileName = fileNames[version]?.[platformKey];
    if (!fileName) {
        throw new Error(`不支持的Java压缩包: ${version}, ${platform}, ${arch}`);
    }
    return fileName;
}
router.post('/java/install', authenticateToken, async (req, res) => {
    const { version, downloadUrl, socketId } = req.body;
    if (!version || !downloadUrl) {
        return res.status(400).json({
            success: false,
            message: '缺少必要参数'
        });
    }
    try {
        let finalDownloadUrl = downloadUrl;
        const archiveFileName = getJavaArchiveFileName(version, process.platform, os.arch());
        const isSponsor = validateSponsorKey();
        if (isSponsor) {
            try {
                const platform = process.platform;
                const arch = os.arch();
                finalDownloadUrl = getSponsorDownloadUrl(version, platform, arch);
                logger.info(`检测到有效赞助者，使用赞助者专用下载链接: ${finalDownloadUrl}`);
            }
            catch (error) {
                logger.warn(`获取赞助者下载链接失败，使用默认链接: ${error instanceof Error ? error.message : '未知错误'}`);
            }
        }
        res.json({
            success: true,
            message: `${version} 开始安装${isSponsor ? '（赞助者专用链接）' : ''}`
        });
        await javaManager.installJava(version, finalDownloadUrl, (stage, progress) => {
            if (io && socketId) {
                io.to(socketId).emit('java-install-progress', {
                    version,
                    stage,
                    progress
                });
            }
        }, archiveFileName);
        if (io && socketId) {
            io.to(socketId).emit('java-install-progress', {
                version,
                stage: 'extract',
                progress: 100
            });
        }
        if (io && socketId) {
            io.to(socketId).emit('java-install-complete', {
                version,
                success: true,
                message: `${version} 安装成功${isSponsor ? '（赞助者专用链接）' : ''}`
            });
        }
    }
    catch (error) {
        logger.error(`安装 ${version} 失败:`, error);
        if (io && socketId) {
            io.to(socketId).emit('java-install-complete', {
                version,
                success: false,
                message: `${version} 安装失败: ${error instanceof Error ? error.message : '未知错误'}`
            });
        }
    }
});
router.delete('/java/:version', authenticateToken, async (req, res) => {
    const { version } = req.params;
    try {
        await javaManager.uninstallJava(version);
        res.json({
            success: true,
            message: `${version} 卸载成功`
        });
    }
    catch (error) {
        logger.error(`卸载 ${version} 失败:`, error);
        const statusCode = error instanceof Error && error.message.includes('未安装') ? 404 : 500;
        res.status(statusCode).json({
            success: false,
            message: `卸载 ${version} 失败: ${error instanceof Error ? error.message : '未知错误'}`
        });
    }
});
router.get('/java/:version/verify', authenticateToken, async (req, res) => {
    const { version } = req.params;
    try {
        const result = await javaManager.verifyJava(version);
        res.json({
            success: true,
            data: result
        });
    }
    catch (error) {
        logger.error(`验证 ${version} 失败:`, error);
        const statusCode = error instanceof Error && error.message.includes('未安装') ? 404 : 500;
        res.status(statusCode).json({
            success: false,
            message: `验证 ${version} 失败: ${error instanceof Error ? error.message : '未知错误'}`
        });
    }
});
router.get('/vcredist', authenticateToken, async (req, res) => {
    try {
        const environments = await vcRedistManager.getVcRedistEnvironments();
        res.json({
            success: true,
            data: environments
        });
    }
    catch (error) {
        logger.error('获取Visual C++运行库环境列表失败:', error);
        res.status(500).json({
            success: false,
            message: '获取Visual C++运行库环境列表失败'
        });
    }
});
router.post('/vcredist/install', authenticateToken, async (req, res) => {
    const { version, architecture, downloadUrl, socketId } = req.body;
    if (!version || !architecture || !downloadUrl) {
        return res.status(400).json({
            success: false,
            message: '缺少必要参数'
        });
    }
    try {
        res.json({
            success: true,
            message: `Visual C++ ${version} ${architecture} 开始安装`
        });
        await vcRedistManager.installVcRedist(version, architecture, downloadUrl, (stage, progress) => {
            if (io && socketId) {
                io.to(socketId).emit('vcredist-install-progress', {
                    version,
                    architecture,
                    stage,
                    progress
                });
            }
        });
        if (io && socketId) {
            io.to(socketId).emit('vcredist-install-complete', {
                version,
                architecture,
                success: true,
                message: `Visual C++ ${version} ${architecture} 安装成功`
            });
        }
    }
    catch (error) {
        logger.error(`安装 Visual C++ ${version} ${architecture} 失败:`, error);
        if (io && socketId) {
            io.to(socketId).emit('vcredist-install-complete', {
                version,
                architecture,
                success: false,
                message: `Visual C++ ${version} ${architecture} 安装失败: ${error instanceof Error ? error.message : '未知错误'}`
            });
        }
    }
});
router.delete('/vcredist/:version/:architecture', authenticateToken, async (req, res) => {
    const { version, architecture } = req.params;
    const { socketId } = req.body;
    try {
        res.json({
            success: true,
            message: `Visual C++ ${version} ${architecture} 卸载命令已下发`
        });
        await vcRedistManager.uninstallVcRedist(version, architecture);
        if (io && socketId) {
            io.to(socketId).emit('vcredist-uninstall-complete', {
                version,
                architecture,
                success: true,
                message: `Visual C++ ${version} ${architecture} 卸载成功`
            });
        }
    }
    catch (error) {
        logger.error(`卸载 Visual C++ ${version} ${architecture} 失败:`, error);
        if (io && socketId) {
            io.to(socketId).emit('vcredist-uninstall-complete', {
                version,
                architecture,
                success: false,
                message: `Visual C++ ${version} ${architecture} 卸载失败: ${error instanceof Error ? error.message : '未知错误'}`
            });
        }
    }
});
router.get('/vcredist/:version/:architecture/verify', authenticateToken, async (req, res) => {
    const { version, architecture } = req.params;
    try {
        const result = await vcRedistManager.verifyVcRedist(version, architecture);
        res.json({
            success: true,
            data: result
        });
    }
    catch (error) {
        logger.error(`验证 Visual C++ ${version} ${architecture} 失败:`, error);
        res.status(500).json({
            success: false,
            message: `验证 Visual C++ ${version} ${architecture} 失败: ${error instanceof Error ? error.message : '未知错误'}`
        });
    }
});
router.get('/directx', authenticateToken, async (req, res) => {
    try {
        const environments = await directxManager.getDirectXEnvironments();
        res.json({
            success: true,
            data: environments
        });
    }
    catch (error) {
        logger.error('获取DirectX环境列表失败:', error);
        res.status(500).json({
            success: false,
            message: '获取DirectX环境列表失败'
        });
    }
});
router.post('/directx/install', authenticateToken, async (req, res) => {
    const { downloadUrl, socketId } = req.body;
    if (!downloadUrl) {
        return res.status(400).json({
            success: false,
            message: '缺少必要参数'
        });
    }
    try {
        res.json({
            success: true,
            message: 'DirectX 开始安装'
        });
        await directxManager.installDirectX(downloadUrl, (stage, progress) => {
            if (io && socketId) {
                io.to(socketId).emit('directx-install-progress', {
                    stage,
                    progress
                });
            }
        });
        if (io && socketId) {
            io.to(socketId).emit('directx-install-complete', {
                success: true,
                message: 'DirectX 安装成功'
            });
        }
    }
    catch (error) {
        logger.error('安装 DirectX 失败:', error);
        if (io && socketId) {
            io.to(socketId).emit('directx-install-complete', {
                success: false,
                message: `DirectX 安装失败: ${error instanceof Error ? error.message : '未知错误'}`
            });
        }
    }
});
router.delete('/directx', authenticateToken, async (req, res) => {
    try {
        await directxManager.uninstallDirectX();
        res.json({
            success: true,
            message: 'DirectX 安装文件已清理'
        });
    }
    catch (error) {
        logger.error('清理 DirectX 安装文件失败:', error);
        res.status(500).json({
            success: false,
            message: `清理 DirectX 安装文件失败: ${error instanceof Error ? error.message : '未知错误'}`
        });
    }
});
router.get('/package-managers', authenticateToken, async (req, res) => {
    try {
        const managers = await packageManager.getAvailablePackageManagers();
        res.json({
            success: true,
            data: managers
        });
    }
    catch (error) {
        logger.error('获取包管理器列表失败:', error);
        res.status(500).json({
            success: false,
            message: '获取包管理器列表失败'
        });
    }
});
router.get('/packages/:packageManager', authenticateToken, async (req, res) => {
    const { packageManager: pmName } = req.params;
    try {
        const packages = await packageManager.getPackageList(pmName);
        res.json({
            success: true,
            data: packages
        });
    }
    catch (error) {
        logger.error(`获取 ${pmName} 包列表失败:`, error);
        res.status(500).json({
            success: false,
            message: `获取 ${pmName} 包列表失败`
        });
    }
});
router.post('/packages/:packageManager/install', authenticateToken, async (req, res) => {
    const { packageManager: pmName } = req.params;
    const { packages, socketId } = req.body;
    if (!packages || !Array.isArray(packages) || packages.length === 0) {
        return res.status(400).json({
            success: false,
            message: '缺少必要参数或包列表为空'
        });
    }
    try {
        res.json({
            success: true,
            message: '操作命令已下发'
        });
        await packageManager.installPackages(pmName, packages, (task) => {
            if (io && socketId) {
                io.to(socketId).emit('package-task-progress', task);
            }
        });
        if (io && socketId) {
            io.to(socketId).emit('package-install-complete', {
                packageManager: pmName,
                packages,
                success: true,
                message: `成功安装 ${packages.length} 个包`
            });
        }
    }
    catch (error) {
        logger.error(`安装包失败:`, error);
        if (io && socketId) {
            io.to(socketId).emit('package-install-complete', {
                packageManager: pmName,
                packages,
                success: false,
                message: `安装包失败: ${error instanceof Error ? error.message : '未知错误'}`
            });
        }
    }
});
router.post('/packages/:packageManager/uninstall', authenticateToken, async (req, res) => {
    const { packageManager: pmName } = req.params;
    const { packages, socketId } = req.body;
    if (!packages || !Array.isArray(packages) || packages.length === 0) {
        return res.status(400).json({
            success: false,
            message: '缺少必要参数或包列表为空'
        });
    }
    try {
        res.json({
            success: true,
            message: '操作命令已下发'
        });
        await packageManager.uninstallPackages(pmName, packages, (task) => {
            if (io && socketId) {
                io.to(socketId).emit('package-task-progress', task);
            }
        });
        if (io && socketId) {
            io.to(socketId).emit('package-uninstall-complete', {
                packageManager: pmName,
                packages,
                success: true,
                message: `成功卸载 ${packages.length} 个包`
            });
        }
    }
    catch (error) {
        logger.error(`卸载包失败:`, error);
        if (io && socketId) {
            io.to(socketId).emit('package-uninstall-complete', {
                packageManager: pmName,
                packages,
                success: false,
                message: `卸载包失败: ${error instanceof Error ? error.message : '未知错误'}`
            });
        }
    }
});
export default router;
