
#Include "TextLib" as TL
#Include "AnimLib" as AL

main() {
  declare Toolbar_Tooltip_Quad <=> (Page.GetFirstChild("player_toolbar_bar_tooltip_quad") as CMlQuad);
  declare Toolbar_Tooltip_Label <=> (Page.GetFirstChild("player_toolbar_bar_tooltip_text") as CMlLabel);
  declare Toolbar_Visibility_Toggle_Label <=> (Page.GetFirstChild("player_toolbar_bar_button_visibility_toggle") as CMlLabel);

  declare CMlFrame Player_Toolbar_Frame <=> (Page.GetFirstChild("player_toolbar_bar_frame") as CMlFrame);

  declare Boolean IsPlayerToolbarActive = False;
  declare Integer HidePlayerBarAt;

  Player_Toolbar_Frame.Hide();

  declare Text[Text] Tooltips = [
    "player_toolbar_bar_button_list" => "View list of Server Maps",
    "player_toolbar_bar_button_mf" => "View list of Map Folders",
    "player_toolbar_bar_button_jb" => "View the Juke Box List",
    "player_toolbar_bar_button_extend" => "Vote for extending the Time Limit",
    "player_toolbar_bar_button_replay" => "Vote to replay the map",
    "player_toolbar_bar_button_skip" => "Vote for a skip map",

	  "player_toolbar_bar_button_topdons" => "View list of top Donators",
	  "player_toolbar_bar_button_topsums" => "View Server Rank List",
	  "player_toolbar_bar_button_topactive" => "View Top Active List",
	  "player_toolbar_bar_button_mxinfo" => "Info about current map",
	  "player_toolbar_bar_button_players" => "View list of Players",
	  "player_toolbar_bar_button_visibility_toggle" => "Toggle UI visibility (F8)",
	  "player_toolbar_bar_button_help" => "List of available commands"
  ];

  while(True) {
    foreach (Event in PendingEvents) {
      if (Event.Type == CMlScriptEvent::Type::MouseClick && Event.ControlId == "player_toolbar_bar_toggle") {
        if (IsPlayerToolbarActive) {
          Player_Toolbar_Frame.Hide();
          IsPlayerToolbarActive = False;
          HidePlayerBarAt = 0;
        } else {
          Player_Toolbar_Frame.Show();
          IsPlayerToolbarActive = True;
          HidePlayerBarAt = Now + 5000;
        }
      }

      if (Event.Type == CMlScriptEvent::Type::MouseClick && Event.ControlId == "player_toolbar_bar_button_visibility_toggle") {
        declare netwrite Boolean Net_DistractionFreeMode for UI;

        if (Net_DistractionFreeMode == True) {
          Net_DistractionFreeMode = False;
          Toolbar_Visibility_Toggle_Label.SetText("");
        } else {
          Net_DistractionFreeMode = True;
          Toolbar_Visibility_Toggle_Label.SetText("");
        }
      }

      if (Event.Type == CMlScriptEvent::Type::MouseOver && TL::Find("player_toolbar_bar_button", Event.ControlId, True, True)) {
        Toolbar_Tooltip_Label.SetText(Tooltips[Event.ControlId]);
        Toolbar_Tooltip_Label.Show();
        Toolbar_Tooltip_Quad.Show();
        HidePlayerBarAt = Now + 5000;
      }
      if (Event.Type == CMlScriptEvent::Type::MouseOut && TL::Find("player_toolbar_bar_button", Event.ControlId, True, True)) {
        Toolbar_Tooltip_Label.Hide();
        Toolbar_Tooltip_Quad.Hide();
        HidePlayerBarAt = Now + 5000;
      }
    }

    if (HidePlayerBarAt != 0 && HidePlayerBarAt <= Now) {
      Player_Toolbar_Frame.Hide();
      IsPlayerToolbarActive = False;
      HidePlayerBarAt = 0;
    }

    yield;
  }
}
