use strict;
$^W = 0;                        # Avoid redefined sub msgs

# Musica objects
my $mobj = $Musica;
my $router = $audio_router;
my @sources = ( $music_source1, $music_source2, $music_source3, $music_source4 );
my %zones = ( 
   'Kitchen' => $music_kitchen,
   'Master Bedroom' => $music_mb,
   'Patio' => $music_patio
);
my @virtual_sources = $router->get_virtual_sources();

# AlsaPlayers
my @alsaplayers = ( $player1, $player2, $player3, $player4 );

my $html = &html_header('Music Info/Control');

$html .= "Musica ADC Version " . $mobj->get_adc_version() . " (Musica.pm version " . $mobj->get_object_version . ")\n";
$html .= "<P /><table border=1><tr><th>Zone</th>";
foreach (keys %zones) {
   $html .= "<th>$_ (" . $zones{$_}->get_zone_num() . ")</th>\n";
}

$html .= "</tr><tr><th>Source</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_source();
   $html .= "<form action='/SET;referer' name='source_$_'><select onchange='source_$_.submit()' name='$zones{$_}->{object_name}'><option></option>\n";
   $html .= "<option value='off'>Off</option>\n";
   $html .= "<option value='1'>Source 1</option>\n";
   $html .= "<option value='2'>Source 2</option>\n";
   $html .= "<option value='3'>Source 3</option>\n";
   $html .= "<option value='4'>Source 4</option>\n";
   $html .= "<option value='E'>External Input</option>\n";
   $html .= "</form>";
   $html .= "</th>\n";
}

$html .= "</tr><tr><th>Volume</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_volume();
   if ($zones{$_}->get_mute()) {
      $html .= "(<B>MUTED</B>: <a href='/SET;referer?$zones{$_}->{object_name}=unmute'>unmute</a>)";
   } else {
      $html .= "(<a href='/SET;referer?$zones{$_}->{object_name}=mute'>mute</a>)";
   }
   $html .= "<form action='/SET;referer' name='volume_$_'><select onchange='volume_$_.submit()' name='$zones{$_}->{object_name}'><option></option>\n";
   for (my $i = 0; $i <= 35; $i++) {
      $html .= "<option value='volume$i'>$i</option>\n";
   }
   $html .= "</form>";
   $html .= "</th>\n";
}

$html .= "</tr><tr><th>Keypad Version</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_keypad_version() . "</th>\n";
}

$html .= "</tr><tr><th>Bass Level</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_bass_level() . "</th>\n";
}

$html .= "</tr><tr><th>Treble Level</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_treble_level() . "</th>\n";
}

$html .= "</tr><tr><th>Balance</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_balance() . "</th>\n";
}

$html .= "</tr><tr><th>Amplifier Selected</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_amp() . "</th>\n";
}

$html .= "</tr><tr><th>Backlight</th>\n";
foreach (keys %zones) {
   $html .= '<th>' . $zones{$_}->get_brightness() . ' (' . $zones{$_}->get_blcolor() . ")</th>\n";
}

$html .= "</tr><tr><th>Misc</th>\n";
foreach (keys %zones) {
   $html .= '<th>';
   if ($zones{$_}->get_loudness()) {
      $html .= '&nbsp; Loudness';
   }
   if ($zones{$_}->get_audioport()) {
      $html .= '&nbsp; EIM';
   }
   if ($zones{$_}->get_locked()) {
      $html .= '&nbsp; Locked';
   }
   if ($zones{$_}->get_overheat()) {
      $html .= '&nbsp; Overheated';
   }
   $html .= "</th>\n";
}

if ($router) {
   $html .= "</tr><tr><th>Virtual Audio Source</th>";
   foreach my $zone (keys %zones) {
      $html .= '<th>' . $router->get_virtual_source_name_for_zone($zones{$zone}->get_zone_num());
      $html .= '<br />';
      foreach (@virtual_sources) {
         $html .= "<a href=\"/RUN;referer?&selectvsource($zones{$zone}->{object_name},'$$_{name}')\">$$_{name}</a>\n";
      }
      $html .= "</th>\n";
   }
}

$html .= "</tr></table>";

$html .= "<P /><table border=1><tr><th>Real Source</th>";
foreach (@sources) {
   $html .= '<th>' . $_->get_source_num() . "</th>\n";
}

$html .= "</tr><tr><th>Label</th>";
foreach (@sources) {
   $html .= '<th>' . $_->get_label() . "</th>\n";
}

$html .= "</tr><tr><th>Zones Listening</th>";
foreach (@sources) {
   $html .= '<th>';
   foreach ($_->get_zones()) {
      $html .= '&nbsp;' . $_;
   }
   $html .= '</th>';
}

if ($router) {
   $html .= "</tr><tr><th>Virtual Audio Source</th>";
   foreach (@sources) {
      $html .= '<th>' . $router->get_virtual_source_name_for_real_source($_->get_source_num()) . "</th>\n";
   }
}

$html .= "</tr></table>";

$html .= "<P /><table border=1><tr><th>AlsaPlayer Session</th>";
foreach (@alsaplayers) {
   $html .= "<th>$$_{session_name} ($$_{session})</th>\n";
}

$html .= "</tr><tr><th>ALSA Channel</th>";
foreach (@alsaplayers) {
   $html .= "<th>$$_{channel}</th>\n";
}

$html .= "</tr><tr><th>Paused?</th>";
foreach (@alsaplayers) {
   if ($_->is_paused()) {
      $html .= "<th><text style='color:red'>Yes</text></th>\n";
   } else {
      $html .= "<th><text style='color:green'>No</text></th>\n";
   }
}

$html .= "</tr><tr><th>Busy?</th>";
foreach (@alsaplayers) {
   if ($_->is_busy()) {
      $html .= "<th><text style='color:red'>Yes</text></th>\n";
   } else {
      $html .= "<th><text style='color:green'>No</text></th>\n";
   }
}

$html .= "</tr><tr><th>Playlist Length</th>\n";
foreach (@alsaplayers) {
   $html .= '<th>' . $_->get_playlist_length() . "</th>\n";
}

$html .= "</tr><tr><th>Title</th>\n";
foreach (@alsaplayers) {
   $html .= '<th>' . $_->get_title() . "</th>\n";
}

$html .= "</tr><tr><th>Artist</th>\n";
foreach (@alsaplayers) {
   $html .= '<th>' . $_->get_artist() . "</th>\n";
}

$html .= "</tr><tr><th>Album</th>\n";
foreach (@alsaplayers) {
   $html .= '<th>' . $_->get_album() . "</th>\n";
}

$html .= "</tr><tr><th>Actions</th>\n";
foreach (@alsaplayers) {
   $html .= '<th>';
   $html .= "<a href='/SET;referer?$$_{object_name}=previous'>previous</a>&nbsp;";
   $html .= "<a href='/SET;referer?$$_{object_name}=next'>next</a>&nbsp;";
   $html .= "<a href='/SET;referer?$$_{object_name}=pause'>pause</a>&nbsp;";
   $html .= "<a href='/SET;referer?$$_{object_name}=unpause'>unpause</a>";
   $html .= "</th>\n";
}

$html .= "</tr></table>";

$html .= "<P /><table border=1>\n";
$html .= "<tr><th>Virtual Source</th><th>Label</th><th>Real Source</th><th>Zones Listening</th></tr>\n";
foreach (@virtual_sources) {
   $html .= "<tr><th>$$_{name}</th>\n";
   $html .= "<td>" . $_->get_data('label') . "</td>\n";
   $html .= "<td>" . $router->get_real_source_number_for_vsource($_) . "</td>\n";
   my @zones_listening = $router->get_zones_listening_to_vsource($_);
   $html .= "<td>@zones_listening</td>\n";
   $html .= "</tr>\n";
}

$html .= "</tr></table>";
$html .= "</body></html>";
return &html_page('', $html);


