// base class, not vanilla
class NiceLobbyFooter extends ButtonFooter;


const MidGameMenuString="NicePack.NiceInvasionLoginMenu";

var automated GUIButton b_Menu, b_MapVote, b_KickVote, b_Ready, b_ViewMap, b_Cancel;
var string ReadyString, UnreadyString;


function bool InternalOnPreDraw(Canvas C)
{
  if (PlayerOwner().GameReplicationInfo != none)
  {
    // disable view map if its not a lobby state
    if (!PlayerOwner().GameReplicationInfo.bMatchHasBegun)
      b_ViewMap.EnableMe();
    else
      b_ViewMap.DisableMe();
  }

  // ready-unready text switch
  if (PlayerOwner().PlayerReplicationInfo != none && PlayerOwner().PlayerReplicationInfo.bReadyToPlay)
    b_Ready.Caption = UnreadyString;
  else
    b_Ready.Caption = ReadyString;

  return super.InternalOnPreDraw(C);
}


// took poosh ScrnLobbyFooter as a reference
// 1. overrided to position buttons by TabOrder
// 2. moved some buttons to left side
function PositionButtons(Canvas C)
{
  local int j;
  local GUIButton b;
  local array<GUIButton> buttonsLEFT, buttonsRIGHT;
  local float x, s, m;

  s = GetSpacer();
  m = GetMargin() / 2;
  x = ActualLeft() + ActualWidth() - m;
  // position the Disconnect button on the left, others on the right

  // right buttons
  buttonsRIGHT = GetButtons(false);
  for (j = buttonsRIGHT.length - 1; j >= 0; --j)
  {
    b = buttonsRIGHT[j];
    x -= b.ActualWidth();
    b.WinLeft = b.RelativeLeft(x, true);
    x -= s;
  }

  // left buttons
  buttonsLEFT = GetButtons(true);
  for (j = 0; j < buttonsLEFT.length; j++)
  {
    b = buttonsLEFT[j];
    b.WinLeft = b.RelativeLeft(m, true);
    m += b.ActualWidth();
    m += s;
  }
}


final private function array<GUIButton> GetButtons(optional bool bLEFTSIDED)
{
  local int i, j;
  local GUIButton b;
  local array<GUIButton> buttons;

  if (bLEFTSIDED)
  {
    for (i = 0; i < Controls.Length; ++i)
    {
      b = GUIButton(Controls[i]);
      if (b != none && bIsButtonLeft(b) && b.bVisible)
      {
        // compare tab orders and kepp the array sorted
        for (j = 0; j < buttons.length; ++j) 
        {
          if (buttons[j].TabOrder >= b.TabOrder)
            break;
        }
        // fill the array
        buttons.insert(j, 1);
        buttons[j] = b;
      }
    }
  }
  else
  {
    for (i = 0; i < Controls.Length; ++i)
    {
      b = GUIButton(Controls[i]);
      if (b != none && !bIsButtonLeft(b) && b.bVisible)
      {
        // compare tab orders and kepp the array sorted
        for (j = 0; j < buttons.length; ++j) 
        {
          if (buttons[j].TabOrder >= b.TabOrder)
            break;
        }
        // fill the array
        buttons.insert(j, 1);
        buttons[j] = b;
      }
    }
  }

  return buttons;
}


// which buttons are left sided
final private function bool bIsButtonLeft(GUIButton b)
{
  // mapvote, kickvote and main menu must be on left side
  if (b == b_MapVote || b == b_KickVote || b == b_Menu)
    return true;

  return false;
}


function bool ButtonsSized(Canvas C)
{
  local int i;
  local GUIButton b;
  local bool bResult;
  local string str;
  local float T, AH, AT;

  if (!bPositioned)
    return false;

  bResult = true;
  str = GetLongestCaption(C);

  AH = ActualHeight();
  AT = ActualTop();

  for (i = 0; i < Controls.Length; i++)
  {
    b = GUIButton(Controls[i]);
    if (b != none)
    {
      if (bAutoSize && bFixedWidth)
      {
        if (b.Caption == "")
          b.SizingCaption = Left(str, Len(str) / 2);
        else
          b.SizingCaption = str;
      }
      else
        b.SizingCaption = "";

      bResult = bResult && b.bPositioned;
      if (bFullHeight)
        b.WinHeight = b.RelativeHeight(AH, true);
      else
        b.WinHeight = b.RelativeHeight(ActualHeight(ButtonHeight), true);

      switch (Justification)
      {
        case TXTA_Left:
          T = ClientBounds[1];
          break;

        case TXTA_Center:
          T = (AT + AH / 2) - (b.ActualHeight() / 2);
          break;

        case TXTA_Right:
          T = ClientBounds[3] - b.ActualHeight();
          break;
      }

      //b.WinTop = AT + ((AH - ActualHeight(ButtonHeight)) / 2);
      b.WinTop = b.RelativeTop(T, true) + ((WinHeight - ButtonHeight) / 2);
    }
  }

  return bResult;
}


function float GetButtonLeft()
{
  local int i;
  local GUIButton b;
  local float TotalWidth, AW, AL;
  local float FooterMargin;

  AL = ActualLeft();
  AW = ActualWidth();
  FooterMargin = GetMargin();

  for (i = 0; i < Controls.Length; i++)
  {
    b = GUIButton(Controls[i]);
    if (b != none)
    {
      if (TotalWidth > 0)
        TotalWidth += GetSpacer();

      TotalWidth += b.ActualWidth();
    }
  }

  if (Alignment == TXTA_Center)
    return (AL + AW) / 2 - FooterMargin / 2 - TotalWidth / 2;

  if (Alignment == TXTA_Right)
    return (AL + AW - FooterMargin / 2) - TotalWidth;

  return AL + (FooterMargin / 2);
}


// Finds the longest caption of all the buttons
function string GetLongestCaption(Canvas C)
{
  local int i;
  local float XL, YL, LongestW;
  local string str;
  local GUIButton b;

  if (C == none)
    return "";

  for (i = 0; i < Controls.Length; i++)
  {
    b = GUIButton(Controls[i]);
    if (b != none)
    {
      if (b.Style != none)
        b.Style.TextSize(C, b.MenuState, b.Caption, XL, YL, b.FontScale);
      else
        C.StrLen( b.Caption, XL, YL );

      if (LongestW == 0 || XL > LongestW)
      {
        str = b.Caption;
        LongestW = XL;
      }
    }
  }

  return str;
}


function bool OnFooterClick(GUIComponent Sender)
{
  local GUIController C;
  local PlayerController PC;

  PC = PlayerOwner();
  C = Controller;

  // midgame menu
  if (Sender == b_Menu)
  {
    PC.ClientOpenMenu(MidGameMenuString, false);
  }
  // ready-unready us
  else if (Sender == b_Ready)
  {
    if (PC.Level.NetMode == NM_Standalone || !PC.PlayerReplicationInfo.bReadyToPlay)
    {
      if (KFPlayerController(PC) != none)
        KFPlayerController(PC).SendSelectedVeterancyToServer(true);

      // Set Ready
      PC.ServerRestartPlayer();
      PC.PlayerReplicationInfo.bReadyToPlay = True;
      if (PC.Level.GRI.bMatchHasBegun)
        PC.ClientCloseMenu(true, false);

      b_Ready.Caption = UnreadyString;
    }
    else
    {
      if (KFPlayerController(PC) != none)
      {
        KFPlayerController(PC).ServerUnreadyPlayer();
        PC.PlayerReplicationInfo.bReadyToPlay = false;
        b_Ready.Caption = ReadyString;
      }
    }
  }
  // Kill Window and exit game / disconnect from server
  else if (Sender == b_Cancel)
  {
    // change class to ours
    NiceLobbyMenu(PageOwner).bAllowClose = true;
    C.ViewportOwner.Console.ConsoleCommand("DISCONNECT");
    // Marco SP
    PC.ClientCloseMenu(true, false);
    C.AutoLoadMenus();
  }
  // Spectate map while waiting for players to get ready
  else if (Sender == b_ViewMap)
  {
    // change class to ours
    NiceLobbyMenu(PageOwner).bAllowClose = true;
    PC.ClientCloseMenu(true, false);
  }
  // open kick vote
  else if (Sender == b_KickVote)
  {
    Controller.OpenMenu(Controller.KickVotingMenu);
  }
  // open map vote
  else if (Sender == b_MapVote)
  {
    PC.ShowVoteMenu();
  }

  return false;
}


function OnSteamStatsAndAchievementsReady()
{
  PlayerOwner().ClientOpenMenu("KFGUI.KFProfilePage", false);
}


defaultproperties
{
  ReadyString="Ready"
  UnreadyString="Unready"
  OnPreDraw=NiceLobbyFooter.InternalOnPreDraw

  // LEFT buttons
  Begin Object Class=GUIButton Name=MenuButton
    Caption="Main Menu"
    Hint="Open midgame menu."
    WinTop=0.966146
    WinLeft=0.280000
    WinWidth=0.120000
    WinHeight=0.033203
    RenderWeight=2.000000
    TabOrder=0
    bBoundToParent=true
    ToolTip=none
    OnClick=NiceLobbyFooter.OnFooterClick
    OnKeyEvent=Cancel.InternalOnKeyEvent
  End Object
  b_Menu=MenuButton

  Begin Object Class=GUIButton Name=MapVote
    Caption="Map Vote"
    Hint="Shortcut to map vote."
    WinTop=0.966146
    WinLeft=1.500000 //-0.500000
    WinWidth=0.120000
    WinHeight=0.033203
    RenderWeight=2.000000
    TabOrder=1
    bBoundToParent=true
    ToolTip=none
    OnClick=NiceLobbyFooter.OnFooterClick
    OnKeyEvent=Cancel.InternalOnKeyEvent
  End Object
  b_MapVote=MapVote

  Begin Object Class=GUIButton Name=KickVote
    Caption="Kick Vote"
    Hint="Shortcut to kick vote."
    WinTop=0.966146
    WinLeft=-0.500000
    WinWidth=0.120000
    WinHeight=0.033203
    RenderWeight=2.000000
    TabOrder=2
    bBoundToParent=true
    ToolTip=none
    OnClick=NiceLobbyFooter.OnFooterClick
    OnKeyEvent=Cancel.InternalOnKeyEvent
  End Object
  b_KickVote=KickVote

  // RIGHT buttons
  Begin Object Class=GUIButton Name=ReadyButton
    Caption="Ready"
    Hint="Click to indicate you are ready to play"
    WinTop=0.966146
    WinLeft=0.280000
    WinWidth=0.120000
    WinHeight=0.033203
    RenderWeight=2.000000
    TabOrder=3
    bBoundToParent=true
    ToolTip=none
    OnClick=NiceLobbyFooter.OnFooterClick
    OnKeyEvent=ReadyButton.InternalOnKeyEvent
  End Object
  b_Ready=ReadyButton

  Begin Object Class=GUIButton Name=ViewMap
    Caption="View Map"
    Hint="Hover around while other players afk to death."
    WinTop=0.966146
    WinLeft=-0.500000
    WinWidth=0.120000
    WinHeight=0.033203
    RenderWeight=2.000000
    TabOrder=4
    bBoundToParent=true
    ToolTip=none
    OnClick=NiceLobbyFooter.OnFooterClick
    OnKeyEvent=Cancel.InternalOnKeyEvent
  End Object
  b_ViewMap=ViewMap

  Begin Object Class=GUIButton Name=Cancel
    Caption="Disconnect"
    Hint="Disconnect From This Server"
    WinTop=0.966146
    WinLeft=-0.500000
    WinWidth=0.120000
    WinHeight=0.033203
    RenderWeight=2.000000
    TabOrder=5
    bBoundToParent=true
    ToolTip=none
    OnClick=NiceLobbyFooter.OnFooterClick
    OnKeyEvent=Cancel.InternalOnKeyEvent
  End Object
  b_Cancel=Cancel
}