<?php
class Score
{
    // database connection and table name
    private $conn;

    private $score_path;
    private $root_path;

    // constructor with $db as database connection
    public function __construct($db, $path, $root)
    {
        $this->conn = $db;
        $this->score_path = $path;
        $this->root_path = $root;
    }

    // read score list
    public function read_score_list()
    {
        $scores_arr = array();
        $scores_arr["scores"] = array();

        $this->conn->connect();
        //$theDirectory = opendir($this->mScorePath);
        // row 0: id
        // row 1: numero
        // row 2: nom
        // row 3: compo
        // row 4: style
        $the_directory = scandir($this->score_path);
        sort($the_directory);
        foreach ($the_directory as $entry_name) {
            if (($entry_name != ".")  && ($entry_name != "..")) {
                $query = "select * from repertoire where numero=$entry_name";
                $resultat = $this->conn->execute($query);
                $row = mysql_fetch_row($resultat);

                $score_item = array(
                    "id" => $row[1],
                    "name" => $row[2],
                    "compositor" => $row[3]
                );
                // "style" => $row[4]
                array_push($scores_arr["scores"], $score_item);
            }
        }

        $this->conn->close();

        return $scores_arr;
    }

    public function read_score($id)
    {
        $this->conn->connect();

        $query = "select * from repertoire where numero=$id";
        $resultat = $this->conn->execute($query);
        $row = mysql_fetch_row($resultat);

        // row 2: nom
        // row 3: compo

        $name = $row[2];
        $compositor = $row[3];
        $instruments = array();

        $the_score_path = $this->score_path . $id;
        $the_directory = opendir($the_score_path);
        while ($entry_name = readdir($the_directory)) {
            if (($entry_name != ".")  && ($entry_name != "..")) {
                $title = $this->get_entry_name($entry_name);

                $the_partition_dir = opendir($the_score_path . "/" . $entry_name);
                $number = array();

                while ($the_num = readdir($the_partition_dir)) {
                    if (($the_num != ".")  && ($the_num != "..")) {
                        $the_page_dir = opendir($the_score_path . "/" . $entry_name . "/" . $the_num);
                        $the_num_string = $the_num;
                        $pages = array();

                        while ($the_page = readdir($the_page_dir)) {
                            if (($the_page != ".")  && ($the_page != "..")) {
                                $the_name = substr($the_page, 0, strpos($the_page, '.'));
                                $uri = $this->root_path . "/" . $id . "/" . $entry_name . "/" . $the_num . "/" . $the_page;
                                $page_item = array(
                                    "name" => $the_name,
                                    "uri" => $uri
                                );
                                array_push($pages, $page_item);
                            }
                        }
                        closedir($the_page_dir);
                        $number_item = array(
                            "id" => $the_num_string,
                            "pages" => $pages
                        );
                        array_push($number, $number_item);
                    }
                }
                closedir($the_partition_dir);

                // Save instrument
                $instrument_item = array(
                    "id" => $entry_name,
                    "title" => $title,
                    "number" => $number,
                );

                array_push($instruments, $instrument_item);
            }
        }

        $result_item = array(
            "id" => $id,
            "name" => $row[2],
            "compositor" => $row[3],
            "instruments" => $instruments
        );

        return $result_item;
    }

    private function get_entry_name($name)
    {
        if ($name == "cla") return "Clarinette";
        else if ($name == "flu") return "Flute";
        else if ($name == "trb") return "Trombone";
        else if ($name == "pic") return "Piccolo";
        else if ($name == "per") return "Percussions";
        else if ($name == "htb") return "Hautbois";
        else if ($name == "trp") return "Trompette";
        else if ($name == "dir") return "Direction";
        else if ($name == "sax") return "Sax Alto";
        else if ($name == "sat") return "Sax T&eacute;nor";
        else if ($name == "sab") return "Sax Baryton";
        else if ($name == "cor") return "Cor";
        else if ($name == "eup") return "Euphonium";
        else if ($name == "bas") return "Basson";
        else if ($name == "cba") return "Contrebasse";
        else if ($name == "crn") return "Cornet";
        else if ($name == "coa") return "Cor Anglais";
        else if ($name == "clb") return "Clarinette Basse";
        else if ($name == "har") return "Harpe";
        else if ($name == "pia") return "Piano";
        else if ($name == "tub") return "Tuba";
        else if ($name == "sup") return "Parties supplementaires";
        else if ($name == "par") return "Parties";
        else return $name;
    }
}
