//--- OBJECT WRITE BEGIN ---
new GuiChunkedBitmapCtrl(JoinServerGui) {
   profile = "GuiContentProfile";
   horizSizing = "width";
   vertSizing = "height";
   position = "0 0";
   extent = "640 480";
   minExtent = "8 8";
   visible = "1";
   helpTag = "0";
   bitmap = "./background.jpg";
   useVariable = "0";
   tile = "0";

   new GuiControl() {
      profile = "GuiWindowProfile";
      horizSizing = "center";
      vertSizing = "center";
      position = "92 71";
      extent = "455 318";
      minExtent = "8 8";
      visible = "1";
      helpTag = "0";

      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "14 42";
         extent = "24 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Pass";
         maxLength = "255";
      };
      new GuiButtonCtrl(JS_queryMaster) {
         profile = "GuiButtonProfile";
         horizSizing = "right";
         vertSizing = "top";
         position = "233 281";
         extent = "100 23";
         minExtent = "8 8";
         visible = "1";
         command = "Canvas.getContent().query();";
         helpTag = "0";
         text = "Query Master";
         groupNum = "-1";
         buttonType = "PushButton";
      };
      new GuiButtonCtrl(JS_queryLan) {
         profile = "GuiButtonProfile";
         horizSizing = "right";
         vertSizing = "top";
         position = "121 281";
         extent = "100 23";
         minExtent = "8 8";
         visible = "1";
         command = "Canvas.getContent().queryLan();";
         helpTag = "0";
         text = "Query LAN";
         groupNum = "-1";
         buttonType = "PushButton";
      };
      new GuiButtonCtrl(JS_joinServer) {
         profile = "GuiButtonProfile";
         horizSizing = "right";
         vertSizing = "top";
         position = "345 281";
         extent = "100 23";
         minExtent = "8 8";
         visible = "1";
         command = "Canvas.getContent().join();";
         helpTag = "0";
         text = "Join Server";
         groupNum = "-1";
         buttonType = "PushButton";
            active = "0";
      };
      new GuiScrollCtrl() {
         profile = "GuiScrollProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "10 75";
         extent = "437 186";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         willFirstRespond = "1";
         hScrollBar = "dynamic";
         vScrollBar = "alwaysOn";
         constantThumbHeight = "0";
         childMargin = "0 0";
            defaultLineHeight = "15";

         new GuiTextListCtrl(JS_serverList) {
            profile = "GuiTextArrayProfile";
            horizSizing = "right";
            vertSizing = "bottom";
            position = "2 2";
            extent = "415 8";
            minExtent = "8 8";
            visible = "1";
            helpTag = "0";
            enumerate = "0";
            resizeCell = "1";
            columns = "0 40 195 260 325 385";
            fitParentWidth = "1";
            clipColumnText = "0";
               noDuplicates = "false";
         };
      };
      new GuiTextEditCtrl() {
         profile = "GuiTextEditProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "98 15";
         extent = "134 18";
         minExtent = "8 8";
         visible = "1";
         variable = "pref::Player::Name";
         helpTag = "0";
         maxLength = "255";
         historySize = "0";
         password = "0";
         tabComplete = "0";
         sinkAllKeyEvents = "0";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "12 11";
         extent = "63 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Player Name:";
         maxLength = "255";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "269 42";
         extent = "36 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Players";
         maxLength = "255";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "335 42";
         extent = "38 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Version";
         maxLength = "255";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "412 42";
         extent = "28 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Game";
         maxLength = "255";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "212 42";
         extent = "20 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Ping";
         maxLength = "255";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "72 42";
         extent = "63 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Server Name";
         maxLength = "255";
      };
      new GuiButtonCtrl() {
         profile = "GuiButtonProfile";
         horizSizing = "right";
         vertSizing = "top";
         position = "10 281";
         extent = "100 23";
         minExtent = "8 8";
         visible = "1";
         command = "Canvas.getContent().exit();";
         helpTag = "0";
         text = "<< Back";
         groupNum = "-1";
         buttonType = "PushButton";
      };
      new GuiControl(JS_queryStatus) {
         profile = "GuiWindowProfile";
         horizSizing = "center";
         vertSizing = "center";
         position = "72 134";
         extent = "310 50";
         minExtent = "8 8";
         visible = "0";
         helpTag = "0";

         new GuiButtonCtrl(JS_cancelQuery) {
            profile = "GuiButtonProfile";
            horizSizing = "right";
            vertSizing = "bottom";
            position = "9 15";
            extent = "64 20";
            minExtent = "8 8";
            visible = "1";
            command = "Canvas.getContent().cancel();";
            helpTag = "0";
            text = "Cancel";
            groupNum = "-1";
            buttonType = "PushButton";
         };
         new GuiProgressCtrl(JS_statusBar) {
            profile = "GuiProgressProfile";
            horizSizing = "right";
            vertSizing = "bottom";
            position = "84 15";
            extent = "207 20";
            minExtent = "8 8";
            visible = "1";
            helpTag = "0";
         };
         new GuiTextCtrl(JS_statusText) {
            profile = "GuiProgressTextProfile";
            horizSizing = "right";
            vertSizing = "bottom";
            position = "85 14";
            extent = "205 20";
            minExtent = "8 8";
            visible = "1";
            helpTag = "0";
            maxLength = "255";
         };
      };
   };
};
//--- OBJECT WRITE END ---

//----------------------------------------
function JoinServerGui::onWake()
{
   // Double check the status. Tried setting this the control
   // inactive to start with, but that didn't seem to work.
   JS_joinServer.setActive(JS_serverList.rowCount() > 0);
}   

//----------------------------------------
function JoinServerGui::query(%this)
{
   queryMasterServer(
      0,          // Query flags
      $Client::GameTypeQuery,       // gameTypes
      $Client::MissionTypeQuery,    // missionType
      0,          // minPlayers
      100,        // maxPlayers
      0,          // maxBots
      2,          // regionMask
      0,          // maxPing
      100,        // minCPU
      0           // filterFlags
      );
}

//----------------------------------------
function JoinServerGui::queryLan(%this)
{
   queryLANServers(
      28000,      // lanPort for local queries
      0,          // Query flags
      $Client::GameTypeQuery,       // gameTypes
      $Client::MissionTypeQuery,    // missionType
      0,          // minPlayers
      100,        // maxPlayers
      0,          // maxBots
      2,          // regionMask
      0,          // maxPing
      100,        // minCPU
      0           // filterFlags
      );
}

//----------------------------------------
function JoinServerGui::cancel(%this)
{
   cancelServerQuery();
   JS_queryStatus.setVisible(false);
}


//----------------------------------------
function JoinServerGui::join(%this)
{
   cancelServerQuery();
   %id = JS_serverList.getSelectedId();

   // The server info index is stored in the row along with the
   // rest of displayed info.
   %index = getField(JS_serverList.getRowTextById(%id),6);
   if (setServerInfo(%index)) {
      %conn = new GameConnection(ServerConnection);
      %conn.setConnectArgs($pref::Player::Name);
      %conn.setJoinPassword($Client::Password);
      %conn.connect($ServerInfo::Address);
   }
}

//----------------------------------------
function JoinServerGui::exit(%this)
{
   cancelServerQuery();
   Canvas.setContent(mainMenuGui);
}

//----------------------------------------
function JoinServerGui::update(%this)
{
   // Copy the servers into the server list.
   JS_queryStatus.setVisible(false);
   JS_serverList.clear();
   %sc = getServerCount();
   for (%i = 0; %i < %sc; %i++) {
      setServerInfo(%i);
      JS_serverList.addRow(%i,
         ($ServerInfo::Password? "Yes": "No") TAB 
         $ServerInfo::Name TAB
         $ServerInfo::Ping TAB
         $ServerInfo::PlayerCount @ "/" @ $ServerInfo::MaxPlayers TAB
         $ServerInfo::Version TAB
         $ServerInfo::GameType TAB
         %i);  // ServerInfo index stored also
   }
   JS_serverList.sort(0);
   JS_serverList.setSelectedRow(0);
   JS_serverList.scrollVisible(0);

   JS_joinServer.setActive(JS_serverList.rowCount() > 0);
} 

//----------------------------------------
function onServerQueryStatus(%status, %msg, %value)
{
   // Update query status
   // States: start, update, ping, query, done
   // value = % (0-1) done for ping and query states
   if (!JS_queryStatus.isVisible())
      JS_queryStatus.setVisible(true);

   switch$ (%status) {
      case "start":
         JS_joinServer.setActive(false);
         JS_queryMaster.setActive(false);
         JS_statusText.setText(%msg);
         JS_statusBar.setValue(0);
         JS_serverList.clear();

      case "ping":
         JS_statusText.setText("Ping Servers");
         JS_statusBar.setValue(%value);

      case "query":
         JS_statusText.setText("Query Servers");
         JS_statusBar.setValue(%value);

      case "done":
         JS_queryMaster.setActive(true);
         JS_queryStatus.setVisible(false);
         JoinServerGui.update();
   }
}

