<?php
class ScoreScanner {
    private $scoresPath;
    
    public function __construct($path = '../legacy/Scores/') {
        $this->scoresPath = $path;
    }
    
    public function getAllScores() {
        $scores = [];
        $directories = scandir($this->scoresPath);
        
        foreach ($directories as $dir) {
            if ($dir === '.' || $dir === '..') continue;
            
            $scorePath = $this->scoresPath . $dir;
            if (!is_dir($scorePath)) continue;
            
            $score = $this->getScoreInfo($dir);
            if ($score) {
                $scores[] = $score;
            }
        }
        
        // Trier par ID
        usort($scores, function($a, $b) {
            return intval($a['id']) - intval($b['id']);
        });
        
        return $scores;
    }
    
    public function getScoreInfo($id) {
        $scoreDir = $this->scoresPath . $id;
        $iniFile = $scoreDir . '/score.ini';
        
        if (!file_exists($iniFile)) {
            return null;
        }
        
        $ini = parse_ini_file($iniFile);
        
        return [
            'id' => $id,
            'name' => $ini['name'] ?? 'Inconnu',
            'compositor' => $ini['compositor'] ?? 'Inconnu'
        ];
    }
    
    public function getScoreDetail($id) {
        $scoreDir = $this->scoresPath . $id;
        
        if (!is_dir($scoreDir)) {
            return null;
        }
        
        $basicInfo = $this->getScoreInfo($id);
        if (!$basicInfo) {
            return null;
        }
        
        $instruments = [];
        $entries = scandir($scoreDir);
        
        foreach ($entries as $entry) {
            if ($entry === '.' || $entry === '..' || $entry === 'score.ini') continue;
            
            $instrumentPath = $scoreDir . '/' . $entry;
            if (!is_dir($instrumentPath)) continue;
            
            $instrument = $this->getInstrumentInfo($id, $entry);
            if ($instrument) {
                $instruments[] = $instrument;
            }
        }
        
        // Trier instruments par nom
        usort($instruments, function($a, $b) {
            return strcmp($a['title'], $b['title']);
        });
        
        return array_merge($basicInfo, ['instruments' => $instruments]);
    }
    
    private function getInstrumentInfo($scoreId, $instrumentId) {
        $instrumentPath = $this->scoresPath . $scoreId . '/' . $instrumentId;
        
        if (!is_dir($instrumentPath)) {
            return null;
        }
        
        $title = $this->getInstrumentName($instrumentId);
        $parts = [];
        
        $entries = scandir($instrumentPath);
        foreach ($entries as $entry) {
            if ($entry === '.' || $entry === '..') continue;
            
            $partPath = $instrumentPath . '/' . $entry;
            if (!is_dir($partPath)) continue;
            
            $part = $this->getPartInfo($scoreId, $instrumentId, $entry);
            if ($part) {
                $parts[] = $part;
            }
        }
        
        // Trier les parties par numéro
        usort($parts, function($a, $b) {
            return intval($a['id']) - intval($b['id']);
        });
        
        if (empty($parts)) {
            return null;
        }
        
        return [
            'id' => $instrumentId,
            'title' => $title,
            'parts' => $parts
        ];
    }
    
    private function getPartInfo($scoreId, $instrumentId, $partId) {
        $partPath = $this->scoresPath . $scoreId . '/' . $instrumentId . '/' . $partId;
        
        if (!is_dir($partPath)) {
            return null;
        }
        
        $files = [];
        $entries = scandir($partPath);
        
        foreach ($entries as $entry) {
            if ($entry === '.' || $entry === '..') continue;
            
            $filePath = $partPath . '/' . $entry;
            if (!is_file($filePath)) continue;
            
            // Vérifier que c'est un PDF
            if (strtolower(pathinfo($entry, PATHINFO_EXTENSION)) !== 'pdf') continue;
            
            $files[] = [
                'name' => pathinfo($entry, PATHINFO_FILENAME),
                'filename' => $entry,
                'path' => $scoreId . '/' . $instrumentId . '/' . $partId . '/' . $entry
            ];
        }
        
        if (empty($files)) {
            return null;
        }
        
        // Trier les fichiers par nom
        usort($files, function($a, $b) {
            return strcmp($a['name'], $b['name']);
        });
        
        return [
            'id' => $partId,
            'files' => $files
        ];
    }
    
    private function getInstrumentName($code) {
        $names = [
            'cla' => 'Clarinette',
            'flu' => 'Flûte',
            'trb' => 'Trombone',
            'pic' => 'Piccolo',
            'per' => 'Percussions',
            'htb' => 'Hautbois',
            'trp' => 'Trompette',
            'dir' => 'Direction',
            'sax' => 'Sax Alto',
            'sat' => 'Sax Ténor',
            'sab' => 'Sax Baryton',
            'cor' => 'Cor',
            'eup' => 'Euphonium',
            'bas' => 'Basson',
            'cba' => 'Contrebasse',
            'crn' => 'Cornet',
            'coa' => 'Cor Anglais',
            'clb' => 'Clarinette Basse',
            'har' => 'Harpe',
            'pia' => 'Piano',
            'tub' => 'Tuba',
            'sup' => 'Parties supplémentaires',
            'par' => 'Parties'
        ];
        
        return $names[$code] ?? $code;
    }
}
