<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/markdown.php");
include_once($UTIL_DIR . "/refs.php");

function time2str($t)
{
  $min = sprintf("%d", floor($t / 60));
  $sec = $t % 60;
  return sprintf("%d:%02d", $min ,$sec);
}

function createNewlines($in)
{
  $str = "";
  $head = substr($in, 0, strpos($in, "\n"));
  $tail = substr($in, strpos($in, "\n")+1);
  $str .= "<div class=\"header\"><h2 class=\"lyrics_header\">".
    "<div class=\"header_text\">".$head."</div></h2></div>\n";
  $str .= str_replace("\n", "<br/>\n", $tail);
  return $str;
}

class Track {
  public $title;
  public $number;
  public $playtime;
  public $previewurl;
  public $lyrics;

  public function write($fp)
  {
    fwrite($fp, "      <track title=\"" .
	   htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "             number=\"" . $this->number . "\"\n");
    fwrite($fp, "             playtime=\"" . $this->playtime . "\"\n");
    fwrite($fp, "             previewurl=\"" . $this->previewurl . "\">\n");
    fwrite($fp, "        <lyrics>".
	   htmlspecialchars($this->lyrics, ENT_QUOTES, "UTF-8")."</lyrics>\n");
    fwrite($fp, "      </track>\n");
  }

  public function showLyrics()
  {
    $str = "";
    if($this->lyrics) {
      $str .= createNewlines(htmlspecialchars_decode($this->lyrics));
    }
    return $str;
  }

  public function show($disc)
  {
    global $GLOBALS;

    $page = $GLOBALS["page"];
    $str = "";
    $str .= "      <div class=\"track\">\n";

    $str .= "        <span class=\"preview\">\n";
    if($this->previewurl) {
      $str .= "          <object type=\"application/x-shockwave-flash\" ".
	"data=\"player_mp3_maxi.swf\" width=\"25\" height=\"12\">\n";
      $str .= "            <param name=\"movie\" ".
	"value=\"player_mp3_maxi.swf\"/>\n"; 
      $str .= "            <param name=\"FlashVars\" value=\"mp3=".
	$this->previewurl."&amp;showslider=0&amp;width=25&amp;height=12&amp;".
	"skin=design/rotr/chicken.jpg\"/>\n"; 
      $str .= "          </object>\n";
    } else {
    }
    $str .= "        </span>\n";

    $str .= "        <span class=\"number\">".$this->number.". </span>\n";
    $str .= "        <span class=\"title\">".$this->title."</span>\n";
    $str .= "        <span class=\"playtime\">".time2str($this->playtime).
      "</span>\n";
    if($this->lyrics) {
      $str .= "      <a href=\"?page=".$page."&amp;lyrics=".$disc.
	"&amp;track=".$this->number."\">Lyrics</a>\n"; 
    }
    $str .= "      </div>\n";

    return $str;
  }

  public function Track($title, $number, $playtime, $previewurl, $lyrics)
  {
    $this->title = $title;
    $this->number = $number;
    $this->playtime = $playtime;
    $this->previewurl = $previewurl;
    $this->lyrics = $lyrics;
  }
}

class Disc {
  public $title;
  public $releasetime;
  public $description;
  public $cover;
  public $releaser;
  public $tracks = array();
  //  public $reviews = array();
  public $refs;

  public function write($fp)
  {
    fwrite($fp, "  <disc title=\"" .
	   htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        releasetime=\"" . $this->releasetime . "\"\n");
    fwrite($fp, "        description=\"" .
	   htmlspecialchars($this->description, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        cover=\"" . $this->cover . "\"\n");
    fwrite($fp, "        releaser=\"" . $this->releaser . "\">\n");
    fwrite($fp, "    <tracks>\n");
    if($this->tracks) {
      foreach($this->tracks as $track) {
	$track->write($fp);
      }
    }
    fwrite($fp, "    </tracks>\n");
    if($this->refs) $this->refs->write($fp, "    ");
    fwrite($fp, "  </disc>\n");
  }

  public function showLyrics($number)
  {
    $str = "";
    if($this->tracks) {
      foreach($this->tracks as $track) {
	if($track->number == $number) {
	  $str .= $track->showLyrics();
	  break;
	}
      }
    }
    return $str;
  }

  public function show()
  {
    $str = "";

    $str .= "  <div class=\"disc\">\n"; 
    $str .= "    <span class=\"record_title\">".$this->title." (".
      date("Y", $this->releasetime).")</span>\n"; 
    $str .= "    <div class=\"cover\">\n"; 
    $str .= "      <a href=\"".$this->cover."\">\n";
    $str .= "        <img alt=\"".$this->title." cover\"\n";
    $str .= "             src=\"?mode=imagecache&amp;uri=" .
      $this->cover . "&amp;mod=discography&amp;cat=cover\"/>\n";
    $str .= "      </a>\n";
    $str .= "    </div>\n";
    $str .= "    <span class=\"label\">";		
    if($this->releasetime > time()) $str .= "To be";
    else $str .= "Was";
    $str .= " released by ".htmlspecialchars_decode($this->releaser)." on ".
      date("F jS Y", $this->releasetime)."</span>\n";
    $str .= "    <span class=\"tracklist_header\">";
    if($this->tracks && sizeof($this->tracks) > 1) {
      $str .= "Tracks:";
    } else {
      $str .= "Track:";
    }
    $str .= "</span>\n";
    $str .= "    <div class=\"tracklist\">\n";
    $total = 0;
    if($this->tracks) {
      foreach($this->tracks as $track) {
				$str .= $track->show($this->releasetime);
				$total += $track->playtime;
      }
    }
    if($this->tracks && sizeof($this->tracks) > 1) {
      $str .= "      <span class=\"total_playtime\">Total playtime: ".
	time2str($total)."</span>\n";
    }
    $str .= "    </div>\n";
    $str .= "    <span class=\"description\">".
      Markdown(htmlspecialchars_decode($this->description))."</span>\n";
    
    if($this->refs) $str .= $this->refs->show();

    $str .= "  </div>\n";

    return $str;
  }

  public function showCompact()
  {
    $str = "";

    $str .= "  <div class=\"disc\">\n"; 
    /*
    $str .= "    <span class=\"record_title\">".$this->title." (".
      date("Y", $this->releasetime).")</span>\n"; 
    */
    $str .= "    <div class=\"cover\">\n"; 
    $str .= "      <a href=\"?page=discography\">\n";
    $str .= "        <img alt=\"".$this->title." cover\"\n";
    $str .= "             src=\"?mode=imagecache&amp;uri=" .
      $this->cover . "&amp;mod=discography&amp;cat=compactcover\"/>\n";
    $str .= "      </a>\n";
    $str .= "    </div>\n";
    $str .= "    <span class=\"label\">";		
    if($this->releasetime > time()) $str .= "Coming ".date("F jS Y", $this->releasetime);
    else $str .= "Out now!";
    $str .= "</span>\n";
    $str .= "  </div>\n";
    $str .= "</div>\n";

    return $str;
  }
	
  public function addTrack($track)
  {
    $key = $track->number;
    $this->tracks[$key] = $track;
  }
  /*
  public function addReview($review)
  {
    $key = $review->id;
    $this->reviews[$key] = $review;
  }
  */

  public function addRefs($refs)
  {
    $this->refs = $refs;
  }

  public function Disc($title, $releasetime, $description, $cover, $releaser)
  {
    $this->title = $title;
    $this->releasetime = $releasetime;
    $this->description = $description;
    $this->cover = $cover;
    $this->releaser = $releaser;
  }
}

class Discography {

  private $file;
  private $discs = array();

  // Admin config
  public $admin_title = "Discography";
  public $admin_submodules = array("Add Disc" => "add",
				   "Edit Disc" => "edit",
				   "Delete Disc" => "delete");
  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "add":
      break;
    case "edit":
      break;
    case "delete":
      break;
    }
  }

  public function run($params)
  {
    global $GLOBALS;


    $str = "<div class=\"discography\">\n";

    foreach($params as $param => $value) {
      switch($param) {
      case "latest":
	$id = -1;
	foreach($this->discs as $disc) {
	  if($disc->releasetime > $id) $id = $disc->releasetime;
	}
	if($id != -1) {
	  $disc = $this->discs[$id];
	  return $str .= $disc->showCompact();
	}
	break;

      default:
	$lyrics = $GLOBALS["lyrics"];
	$number = $GLOBALS["track"];

	//foreach($params as $param => $value) {}
			  
	if($lyrics && $number) {
	  if($this->discs) {
	    foreach($this->discs as $disc) {
	      if($disc->releasetime == $lyrics) {
		$str .= $disc->showLyrics($number);
		break;
	      }
	    }
	  }
	} else {
	  if($this->discs) {
	    foreach($this->discs as $disc) {
	      $str .= $disc->show();
	    }
	  }
	}

	break;
      }
    }


    $str .= "</div>\n";

    return $str;
  }

  public function add($disc) {
    $key = $disc->releasetime;
    $this->discs[$key] = $disc;
  }

  public function getImageSize($cat)
  {
    switch($cat) {
    case "cover":
      return new ImageSize($this->coverwidth, $this->coverheight, true);
    case "compactcover":
      //      return new ImageSize($this->compactcoverwidth, $this->compactcoverheight, true);
      return new ImageSize(150, 150, true);
    default:
      die(404);
    }
  }
	
  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    fwrite($fp, "<discography>\n");
    foreach($this->discs as $disc) {
      $disc->write($fp);
    }
    fwrite($fp, "</discography>\n");

    fclose($fp);
  }
	
  private function read()
  {
    $reviews = loadModule("reviews");

    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);

    $discography = $dom->documentElement;
		
    $this->coverwidth = $discography->getAttribute('coverwidth');
    $this->coverheight = $discography->getAttribute('coverheight');

    $discs = $dom->getElementsByTagName('disc');

    foreach($discs as $d) {
      $description = "";
      $dess = $d->getElementsByTagName('description');
      foreach($dess as $des) {
				$description = $des->textContent;
      }

      $disc = new Disc($d->getAttribute('title'),
		       $d->getAttribute('releasetime'),
		       $description,
		       $d->getAttribute('cover'),
		       $d->getAttribute('releaser'));

      $tracks = $d->getElementsByTagName('track');
      foreach($tracks as $t) {
				$lyrics = "";
				$ls = $t->getElementsByTagName('lyrics');
				foreach($ls as $l) {
					$lyrics = $l->textContent;
				}
				
				$track = new Track($t->getAttribute('title'),
													 $t->getAttribute('number'),
													 $t->getAttribute('playtime'),
													 $t->getAttribute('previewurl'),
													 $lyrics);
				
				$disc->addTrack($track);
      }

      $rs = $d->getElementsByTagName('refs');
      foreach($rs as $r) {
        $refs = new Refs($r);
        $disc->addRefs($refs);
        break;
      }

      $this->add($disc);
    }
  }

  public function Discography($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }

}

function discography_init()
{
  global $DATA_DIR;
  return new Discography($DATA_DIR . "/discography.xml");
}

?>