#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.00"
#define g_dIndex 65
#define g_dLength 256
#include <sourcemod>
#include <sdktools>
#include <system2>
#pragma newdecls required
//64 maps in one event cuz ofc
char g_cEventMaps[g_dIndex][g_dLength];
char g_cEventContent[g_dLength][g_dLength];
int g_iIndexCounter;
Database g_dDatabase;
public Plugin myinfo = 
{
	name = "schedule announcer",
	author = PLUGIN_AUTHOR,
	description = "informs players when next event takes place",
	version = PLUGIN_VERSION,
	url = "www.unloze.com"
};
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	RegConsoleCmd("sm_event", Cmd_EventNotifier);
	RegConsoleCmd("sm_events", Cmd_EventNotifier);
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public Action Cmd_EventNotifier(int client, any args)
{
	EventNotifierMenu(client);
	return Plugin_Handled;
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	SQL_StartConnection();
	SQL_CreateDBIFNotExist();
	EventInfo();
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
static void EventNotifierMenu(int client)
{
	Menu EventMenu = new Menu(MenuHandler1);
	char l_cEventTimeleft[g_dLength][1];
	EventMenu.SetTitle("UNLOZE Event Information");
	CalculateTimeToNextEvent(g_cEventContent[3], g_cEventContent[4], l_cEventTimeleft[0], sizeof(l_cEventTimeleft));
	// 0 event title
	EventMenu.AddItem("", g_cEventContent[0], ITEMDRAW_DISABLED);
	for (int i = 0; i < g_iIndexCounter; i++)
	{
		if (StrContains(g_cEventMaps[i], "Map:", false) == -1)
		{
			Format(g_cEventMaps[i], sizeof(g_cEventMaps), "Map: %s", g_cEventMaps[i]);
		}
		EventMenu.AddItem("", g_cEventMaps[i], ITEMDRAW_DISABLED);
	}
	// 1 rewards
	if (StrContains(g_cEventContent[1], "Rewards:", false) == -1)
	{
		TrimString(g_cEventContent[1]);
		Format(g_cEventContent[1], sizeof(g_cEventContent), "Rewards: %s", g_cEventContent[1]);
	}
	EventMenu.AddItem("", g_cEventContent[1], ITEMDRAW_DISABLED);
	// 2 leaders
	if (StrContains(g_cEventContent[2], "Leaders:", false) == -1)
	{
		Format(g_cEventContent[2], sizeof(g_cEventContent), "Leaders: %s", g_cEventContent[2]);
	}
	EventMenu.AddItem("", g_cEventContent[2], ITEMDRAW_DISABLED);
	// 3 timeleft
	if (StrContains(l_cEventTimeleft[0], "Timeleft:", false) == -1)
	{
		Format(l_cEventTimeleft[0], sizeof(l_cEventTimeleft), "Timeleft: %s", l_cEventTimeleft[0]);
	}
	EventMenu.AddItem("", l_cEventTimeleft[0], ITEMDRAW_DISABLED);
	EventMenu.ExitButton = true;
	EventMenu.ExitBackButton = true;
	EventMenu.Display(client, 0);
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public int MenuHandler1(Menu menu, MenuAction action, int param1, int param2)
{
	if (action == MenuAction_End)
	{
		delete menu;
	}
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void CalculateTimeToNextEvent(char[] content, char[] content1, char[]str, int maxsize)
{
	//content = 24/11/2018
	//content1 = : 6
	char sPart[2][526];
	char sYear[24];
	char sMonth[24];
	char sDay[24];
	char sHour[24];
	char sMinute[24];
	int i_Year;
	int i_Month;
	int i_Day;
	int i_Hour;
	int i_Minute;
	int year;
	int months;
	int days;
	int l_hours;
	int minutes;
	FormatTime(sYear, sizeof(sYear), "%Y");
	FormatTime(sMonth, sizeof(sMonth), "%m");
	FormatTime(sDay, sizeof(sDay), "%d");
	FormatTime(sHour, sizeof(sHour), "%H");
	FormatTime(sMinute, sizeof(sMinute), "%M");
	TrimString(content);
	TrimString(content1);
	ExplodeString(content, "/", sPart, 2, maxsize);
	i_Day = StringToInt(sDay);
	days = StringToInt(sPart[0]);
	i_Month = StringToInt(sMonth);
	ExplodeString(sPart[1], "/", sPart, 2, maxsize);
	months = StringToInt(sPart[0]);
	i_Hour = StringToInt(sHour);
	l_hours = StringToInt(content1);
	char sPart1[3][526];
	ExplodeString(content, "/", sPart1, 3, maxsize);
	year = StringToInt(sPart1[2]);
	i_Year = StringToInt(sYear);
	i_Minute = StringToInt(sMinute);
	if (days >= i_Day || months > i_Month || year > i_Year)
	{
		if (i_Year <= year)
		{
			if (year > i_Year)
			{
				i_Month = 1;
			}
			if (days >= i_Day)
			{
				days -= i_Day;
			}
			else if (i_Month == 1 || i_Month == 3 || i_Month == 5 || i_Month == 7 || i_Month == 8 || i_Month == 10 || i_Month == 12)
			{
				days += (31 - i_Day);
			}
			else if (i_Month == 2)
			{
				days += (28 - i_Day);
			}
			else
			{
				days += (30 - i_Day);
			}
			//from 12 to 24 or from 7 to 19 etc etc
			l_hours += 12;
			if (i_Hour > l_hours)
			{
				days -= 1;
				l_hours = 24 - (i_Hour - l_hours);
				//PrintToChatAll("if statement l_hours: %i", l_hours);
			}
			else
			{
				l_hours -= i_Hour;
				//PrintToChatAll("else statement: l_hours: %i", l_hours);
			}
			if (l_hours != 0)
			{
				l_hours -= 1;
			}
			minutes = 60 - i_Minute;
			Format(str, maxsize, "Taking Place in: %i Days, %i hours, %i Minutes", days, l_hours, minutes);
		}
		else
		{
			Format(str, maxsize, "EVENT OVER");
		}
	}
	else
	{
		Format(str, maxsize, "EVENT OVER");
	}
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void EventInfo()
{
	MYSQLGetEventContent();
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void MYSQLGetEventContent()
{
	MYSQLGetContent();
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void MYSQLGetContent()
{
	char sQuery[g_dLength];
	Format(sQuery, sizeof(sQuery), "SELECT * FROM `EventContent`");
	SQL_TQuery(g_dDatabase, TqueryThreadCallback, sQuery);
	Format(sQuery, sizeof(sQuery), "SELECT * FROM `Maps`");
	SQL_TQuery(g_dDatabase, TqueryThreadCallbackmaps, sQuery);
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void TqueryThreadCallback(Handle db, Handle rs, const char[] error, any data)
{
	if (SQL_GetRowCount(rs) > 0 && SQL_FetchRow(rs))
	{
		// 0 event title
		SQL_FetchString(rs, 0, g_cEventContent[0], sizeof(g_cEventContent));
		// 1 rewards
		SQL_FetchString(rs, 1, g_cEventContent[1], sizeof(g_cEventContent));
		// 2 leaders
		SQL_FetchString(rs, 2, g_cEventContent[2], sizeof(g_cEventContent));
		// 3 timeleft (24/11/2018)
		SQL_FetchString(rs, 3, g_cEventContent[3], sizeof(g_cEventContent));
		//4 timeleft = : 5 pm GMT+0 // 6 pm GMT+1 // 7 pm GMT+2
		SQL_FetchString(rs, 4, g_cEventContent[4], sizeof(g_cEventContent));
	}
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void TqueryThreadCallbackmaps(Handle owner, Handle rs, const char[] error, any data)
{
	g_iIndexCounter = 0;
	if (SQL_GetRowCount(rs) > 0 && SQL_FetchRow(rs))
	{
		g_iIndexCounter++;
		int index;
		SQL_FetchString(rs, 0, g_cEventMaps[index], sizeof(g_cEventMaps));
		index++;
		while (SQL_FetchRow(rs))
		{
			SQL_FetchString(rs, 0, g_cEventMaps[index], sizeof(g_cEventMaps));
			index++;
			g_iIndexCounter++;
		}
	}
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void SQL_StartConnection()
{
	char error[g_dLength];
	if (SQL_CheckConfig("eventscss")) 
		g_dDatabase = SQL_Connect("eventscss", true, error, sizeof(error));
	if (g_dDatabase == null)
	{
		PrintToChatAll("{green}[UNLOZE] {white}Error! Could not connect to MYSQL-DB!");
	}
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void SQL_CreateDBIFNotExist()
{
	char sQuery[g_dLength];
	Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `eventscss`.`EventContent` (`title` VARCHAR(256) NOT NULL, `Rewards` VARCHAR(256) NULL, `Leaders` VARCHAR(256) NULL, `datum` VARCHAR(128) NULL, `hours` VARCHAR(128) NULL, PRIMARY KEY (`title`)) ENGINE = InnoDB;");
	SQL_FastQuery(g_dDatabase, sQuery);
	Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `eventscss`.`Maps` (`MapNames` VARCHAR(45) NOT NULL, PRIMARY KEY (`MapNames`)) ENGINE = InnoDB;");
	SQL_FastQuery(g_dDatabase, sQuery);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock bool IsValidClient(int client)
{
	if (client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client))
	{
		return true;
	}
	return false;
}