<?php

/**
 * CacheService - 缓存管理服务
 */
if (!defined('ROOT_PATH')) {
    define('ROOT_PATH', dirname(__DIR__) . '/');
}

class CacheService
{
    /**
     * 获取缓存目录大小
     */
    public function getCacheSize()
    {
        $size = 0;
        
        // 统计临时文件目录
        $tempDir = ROOT_PATH . 'cache/temp/';
        if (is_dir($tempDir)) {
            $size += $this->getDirectorySize($tempDir);
        }
        
        // 统计缓存目录
        $cacheDir = ROOT_PATH . 'cache/data/';
        if (is_dir($cacheDir)) {
            $size += $this->getDirectorySize($cacheDir);
        }
        
        // 统计缩略图目录
        $thumbDir = ROOT_PATH . 'upload/thumbnails/';
        if (is_dir($thumbDir)) {
            $size += $this->getDirectorySize($thumbDir);
        }
        
        return $size;
    }
    
    /**
     * 获取目录大小
     */
    private function getDirectorySize($dir)
    {
        $size = 0;
        
        if (!is_dir($dir)) {
            return 0;
        }
        
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
        
        foreach ($iterator as $file) {
            if ($file->isFile()) {
                $size += $file->getSize();
            }
        }
        
        return $size;
    }
    
    /**
     * 格式化文件大小
     */
    public static function formatSize($bytes)
    {
        if ($bytes < 1024) {
            return $bytes . ' B';
        } elseif ($bytes < 1024 * 1024) {
            return round($bytes / 1024, 2) . ' KB';
        } elseif ($bytes < 1024 * 1024 * 1024) {
            return round($bytes / (1024 * 1024), 2) . ' MB';
        } else {
            return round($bytes / (1024 * 1024 * 1024), 2) . ' GB';
        }
    }
    
    /**
     * 清理所有缓存
     */
    public function clearAll()
    {
        $results = [];
        
        // 清理临时文件
        $results['temp'] = $this->clearCacheDirectory(ROOT_PATH . 'cache/temp/');
        
        // 清理缓存数据
        $results['cache'] = $this->clearCacheDirectory(ROOT_PATH . 'cache/data/');
        
        // 清理缩略图
        $results['thumbnails'] = $this->clearCacheDirectory(ROOT_PATH . 'upload/thumbnails/');
        
        // 清理设置缓存
        Setting::clearCache();
        $results['setting'] = 'cleared';
        
        // 清理模板缓存 - 直接删除 template_c 目录
        $templateCacheDir = ROOT_PATH . 'template_c/';
        if (is_dir($templateCacheDir)) {
            $results['template'] = $this->clearCacheDirectory($templateCacheDir);
        } else {
            $results['template'] = 0;
        }
        
        return $results;
    }
    
    /**
     * 清理指定目录
     */
    public function clearCacheDirectory($dir)
    {
        if (!is_dir($dir)) {
            return 0;
        }
        
        $count = 0;
        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );
        
        foreach ($iterator as $file) {
            if ($file->isFile()) {
                if (unlink($file->getPathname())) {
                    $count++;
                }
            } elseif ($file->isDir()) {
                rmdir($file->getPathname());
            }
        }
        
        return $count;
    }
}
