<?php
    $page = 4;
    require("inc_header.php");

    if(!isset($_GET['id']))
    {
      header("Location: songs.php");
    }

    $songinstance_id = $_GET['id'];

    // ======================================================
    // GET SONG DETAILS
    // ======================================================
    
    $query = "SELECT songinstance.`id`, `song`, `enemies`,  `difficulty`, `title`, `author`, 
              (SELECT COUNT(1) FROM `playdata` WHERE `songinstance`=$songinstance_id) as sis 
              FROM `songinstance`, `song` 
              WHERE songinstance.song=song.id AND songinstance.`id`=$songinstance_id";

    $result_temp = mysqli_query($connection, $query);
    $result = mysqli_fetch_assoc($result_temp);
    $song_title = $result['title'];
    $song_enemies = $result['enemies'];
    $song_difficulty = $result['difficulty'];
    $song_played = $result['sis'];
    $song_id = $result['song'];



    // ======================================================
    // DATA POINTS FOR MOST PLAYED GRAPH
    // DATA POINTS FOR AVERAGE PLAY TIME GRAPH
    // ======================================================
    
    $most_played_array = array();
    $average_play_time_array = array();

    $query = "SELECT DATE(`start_time`) AS date, COUNT(*) AS data, AVG(`play_time`) AS average 
              FROM `playdata`, `songinstance` 
              WHERE `playdata`.`songinstance` = `songinstance`.`id` 
              AND `songinstance`.`id` = $songinstance_id
              GROUP BY DATE(`start_time`) 
              ORDER BY `start_time` DESC
              LIMIT 0,7";

    $result_temp = mysqli_query($connection, $query);
    while($result = mysqli_fetch_assoc($result_temp))
    {
      $most_played_array[] = "{y: '" . $result['date'] . "', gp: " . $result['data'] . "}";
      $average_play_time_array[] = "{y: '" . $result['date'] . "', pt: " . format($result['average'])['tseconds'] . "}";
    }

    $most_played_array = array_reverse($most_played_array);
    $average_play_time_array = array_reverse($average_play_time_array);


    // ======================================================
    // DATA POINTS FOR SCORES GRAPH
    // ======================================================
    
    $scores_array = array();
    $scores_names_array = array();

    $query = "SELECT `date`, `score`, `username` 
              FROM highscore, songinstance
              WHERE highscore.songinstance = songinstance.id
              AND songinstance.id = $songinstance_id 
              ORDER BY `date` DESC
              LIMIT 0,28";

    $result_temp = mysqli_query($connection, $query);
    while($result = mysqli_fetch_assoc($result_temp))
    {
      $scores_array[] = "{y: '" . $result['date'] . "', sc: " . $result['score'] . "}";
      $scores_names_array[] = "'" . $result['username'] . "'";
    }

    $scores_array = array_reverse($scores_array);
    $scores_names_array = array_reverse($scores_names_array);


    // ======================================================
    // LATEST GAMES
    // ======================================================
    
    $table = "";

    $query = "SELECT `title`, `difficulty`, songinstance.id as siid, song.id as sid, (`enemies_hit` / (`enemies_missed` + `enemies_hit`)) * 100 as ratio, `buttons_pressed`, `joystick_moved`, `start_time`, `play_time` 
              FROM `playdata`, `songinstance`, `song`
              WHERE playdata.songinstance = songinstance.id AND songinstance.song = song.id AND songinstance.id = $songinstance_id
              ORDER BY `start_time` DESC";

    $result_temp = mysqli_query($connection, $query);
    while($result = mysqli_fetch_assoc($result_temp))
    {
      $ratio = round($result['ratio']);
      $label = "danger";
      if($ratio >= 40)
        $label = "warning";
      if($ratio >= 80)
        $label = "success";

      $table .= "<tr>";
      $table .= "<td><a href='" . returnurl('song.php?id=' . $result['sid']) . "'>" . $result['title'] . "</a></td>";
      $table .= "<td><a href='" . returnurl('songinstance.php?id=' . $result['siid']) . "'>" . $result['difficulty'] . "</a></td>";
      $table .= "<td><span class='label label-" . $label . "'>" . $ratio . "<small>%</small></span></td>";
      $table .= "<td>" . $result['buttons_pressed'] . "</td>";
      $table .= "<td>" . $result['joystick_moved'] . "</td>";
      $table .= "<td>" . format($result['play_time'])['string'] . "</td>";
      $table .= "<td>" . $result['start_time'] . "</td>";
      $table .= "</tr>";
    }  


    // ======================================================
    // LATEST GAMES
    // ======================================================
    
    $table2 = "";

    $query = "SELECT `title`, `difficulty`, songinstance.id as siid, song.id as sid, `username`, `score`, `date` 
              FROM `highscore`, `songinstance`, `song`
              WHERE highscore.songinstance = songinstance.id AND songinstance.song = song.id
              AND songinstance.id = $songinstance_id
              ORDER BY `date` DESC";

    $result_temp = mysqli_query($connection, $query);
    while($result = mysqli_fetch_assoc($result_temp))
    {
      $table2 .= "<tr>";
      $table2 .= "<td><a href='" . returnurl('song.php?id=' . $result['sid']) . "'>" . $result['title'] . "</a></td>";
      $table2 .= "<td><a href='" . returnurl('songinstance.php?id=' . $result['siid']) . "'>" . $result['difficulty'] . "</a></td>";
      $table2 .= "<td>" . $result['username'] . "</td>";
      $table2 .= "<td>" . $result['score'] . "</td>";
      $table2 .= "<td>" . $result['date'] . "</td>";
      $table2 .= "</tr>";
    }  


?>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      Song
      <small><?php echo $song_title; ?></small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="songs.php"><i class="ion ion-music-note"></i> Songs</a></li>
      <li><a href="song.php?id=<?php echo $song_id; ?>"></i> <?php echo $song_title; ?></a></li>
      <li class="active"><a href="#"></i> <?php echo $song_difficulty; ?></a></li>
    </ol>
  </section>

  <section class="content">

    <div class="row">

      <div class="col-lg-6 col-sm-12 col-xs-12">
        <!-- small box -->
        <div class="small-box bg-aqua">
          <div class="inner">
            <h3><?php echo $song_title; ?></h3>
            <p>Title</p>
          </div>
        </div>
      </div><!-- ./col -->

      <div class="col-lg-2 col-sm-4 col-xs-12">
        <!-- small box -->
        <div class="small-box bg-green">
          <div class="inner">
            <h3><?php echo $song_difficulty; ?></h3>
            <p>Difficulty</p>
          </div>
        </div>
      </div><!-- ./col -->

      <div class="clearfix visible-sm-block"></div>

      <div class="col-lg-2 col-sm-4 col-xs-12">
        <!-- small box -->
        <div class="small-box bg-yellow">
          <div class="inner">
            <h3><?php echo $song_enemies ?></h3>
            <p>Total Enemies</p>
          </div>
        </div>
      </div><!-- ./col -->

      <div class="col-lg-2 col-sm-4 col-xs-12">
        <!-- small box -->
        <div class="small-box bg-red">
          <div class="inner">
            <h3><?php echo $song_played; ?></h3>
            <p>Times Played</p>
          </div>
        </div>
      </div><!-- ./col -->
    </div><!-- /.row -->



    <div class="row">
      <div class="col-lg-4 col-xs-12">
        <!-- games played graph -->
        <div class="box box-solid bg-teal">
          <div class="box-header">
            <i class="fa fa-gamepad"></i>
            <h3 class="box-title">Games played</h3>
          </div>

          <div class="box-body border-radius-none">
            <div class="chart" id="games-played-chart" style="height: 250px;"></div>
          </div><!-- /.box-body -->
        </div><!-- /.box -->
      </div>

      <div class="col-lg-4 col-xs-12">
        <!-- TABLE: LATEST SONGS -->

        <!-- games played graph -->
        <div class="box box-solid bg-purple">
          <div class="box-header">
            <i class="ion ion-ribbon-b"></i>
            <h3 class="box-title">Scores</h3>
          </div>

          <div class="box-body border-radius-none">
            <div class="chart" id="scores-chart" style="height: 250px;"></div>
          </div><!-- /.box-body -->
        </div><!-- /.box -->
      </div><!-- /.col -->

      <div class="col-lg-4 col-xs-12">
        <div class="box box-solid bg-orange">
          <div class="box-header">
            <i class="ion ion-clock"></i>
            <h3 class="box-title">Average Play Time</h3>
          </div>

          <div class="box-body border-radius-none">
            <div class="chart" id="average-play-time-chart" style="height: 250px;"></div>
          </div><!-- /.box-body -->
        </div><!-- /.box -->
      </div>
    </div>

     <div class="row">

      <div class="col-lg-12">
        <!-- TABLE: LATEST SONGS -->
        <div class="box">
          <div class="box-header with-border">
            <i class="fa fa-gamepad"></i>
            <h3 class="box-title">Latest Games</h3>
          </div><!-- /.box-header -->
          <div class="box-body">
              <table id="games-played-table" class="table table-bordered">
                <thead>
                  <tr>
                    <th>Song Name</th>
                    <th>Difficulty</th>
                    <th>Hit Ratio</th>
                    <th>Buttons Pressed</th>
                    <th>Joystick Moved</th>
                    <th>Length</th>
                    <th>Time</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                       echo $table;
                  ?>
                </tbody>
              </table>
          </div><!-- /.box-body -->
        </div><!-- /.box -->
      </div><!-- /.col -->
    </div>

    <div class="row">

      <div class="col-lg-12">
        <!-- TABLE: LATEST SONGS -->
        <div class="box">
          <div class="box-header with-border">
            <i class="ion ion-ribbon-b"></i>
            <h3 class="box-title">Scores</h3>
          </div><!-- /.box-header -->
          <div class="box-body">
              <table id="scores-table" class="table table-bordered">
                <thead>
                  <tr>
                    <th>Song Name</th>
                    <th>Difficulty</th>
                    <th>Name</th>
                    <th>Score</th>
                    <th>Time</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                       echo $table2;
                  ?>
                </tbody>
              </table>
          </div><!-- /.box-body -->
        </div><!-- /.box -->
      </div><!-- /.col -->

    </div><!-- /.row -->


    

  </section><!-- /.content -->
</div><!-- /.content-wrapper -->

<!-- DATA TABES SCRIPT -->
    <script src="plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script>
    <script src="plugins/datatables/dataTables.bootstrap.min.js" type="text/javascript"></script>
<!-- Morris.js charts -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
    <script src="plugins/morris/morris.min.js" type="text/javascript"></script>
<!-- Page specific javascript -->
<script type="text/javascript">
        var most_played_data = [ <?php
             foreach ($most_played_array as $value)
             {
                echo $value . ",\n";
             }
        ?> ];

        var average_play_time_data = [ <?php
             foreach ($average_play_time_array as $value)
             {
                echo $value . ",\n";
             }
        ?> ];

        var scores_data = [ <?php
             foreach ($scores_array as $value)
             {
                echo $value . ",\n";
             }
        ?> ];

        var scores_names_data = [ <?php
             foreach ($scores_names_array as $value)
             {
                echo $value . ",\n";
             }
        ?> ];

    </script>
    <script src="dist/js/pages/songinstance.js" type="text/javascript"></script> 

<?php
    require("inc_footer.php");
?>