/*
	BATTLEPASS
*/

#include <YSI_Coding\y_hooks>

#define PASS_PRIZE_MONEY 				(1)
#define PASS_PRIZE_EXP 					(2)
#define PASS_PRIZE_ROB 					(3)
#define PASS_PRIZE_PP 					(4)
#define PASS_PRIZE_VEHICLE 				(5)
#define PASS_PRIZE_SKIN 				(6)
#define PASS_PRIZE_PREMIUM 				(7)
#define PASS_PRIZE_SPECIAL_TOKENS	    (8)
#define PASS_PRIZE_VEHICLE_SLOT 		(9)

#define PASS_XP_NEEDED 					(200)
#define PASS_PP_PRICE					(500)

#define PASS_BUTTON_LEFT				(BattlepassTD[4])
#define PASS_BUTTON_RIGHT				(BattlepassTD[5])
#define PASS_BUTTON_BUY 				(BattlepassTD[6])

new Text:BattlepassTD[7], PlayerText:BattlepassPTD[MAX_PLAYERS][37];

new BattlepassPage[MAX_PLAYERS];

// 0 - free, 1 - paid
new BattlepassRewardsCollected[MAX_PLAYERS][2][100];

enum E_FREE_BATTLEPASS_PRIZES
{
	free_prize_type,
	free_prize_amount,
	free_prize_prop_id
}

new FreeBattlepassPrizes[][E_FREE_BATTLEPASS_PRIZES] = {
	{PASS_PRIZE_MONEY, 50000, 1274},
	{PASS_PRIZE_PP, 20, 1867},
	{PASS_PRIZE_EXP, 100, 1247},
	{PASS_PRIZE_SKIN, 72, 72},
	{PASS_PRIZE_ROB, 10, 1866},

	{PASS_PRIZE_VEHICLE_SLOT, 1, 11738},
	{PASS_PRIZE_MONEY, 70000, 1274},
	{PASS_PRIZE_EXP, 125, 1247},
	{PASS_PRIZE_ROB, 15, 1866},
	{PASS_PRIZE_PP, 30, 1867},

	{PASS_PRIZE_VEHICLE, 412, 0},
	{PASS_PRIZE_MONEY, 100000, 1274},
	{PASS_PRIZE_PP, 20, 1867},
	{PASS_PRIZE_EXP, 150, 1247},
	{PASS_PRIZE_PREMIUM, 3, 19054},

	{PASS_PRIZE_MONEY, 150000, 1274},
	{PASS_PRIZE_EXP, 200, 1247},
	{PASS_PRIZE_PREMIUM, 5, 19054},
	{PASS_PRIZE_ROB, 20, 1866},
	{PASS_PRIZE_SPECIAL_TOKENS, 100, 19058}
};

enum E_PAID_BATTLEPASS_PRIZES
{
	paid_prize_type,
	paid_prize_amount,
	paid_prize_prop_id
}

new PaidBattlepassPrizes[][E_PAID_BATTLEPASS_PRIZES] = {
	{PASS_PRIZE_PP, 50, 1867},
	{PASS_PRIZE_ROB, 15, 1866},
	{PASS_PRIZE_EXP, 200, 1247},
	{PASS_PRIZE_VEHICLE_SLOT, 1, 11738},
	{PASS_PRIZE_PREMIUM, 7, 19054},

	{PASS_PRIZE_SPECIAL_TOKENS, 50, 19058},
	{PASS_PRIZE_EXP, 200, 1247},
	{PASS_PRIZE_ROB, 15, 1866},
	{PASS_PRIZE_VEHICLE, 415, 0},
	{PASS_PRIZE_PP, 40, 1867},

	{PASS_PRIZE_MONEY, 200000, 1274},
	{PASS_PRIZE_PP, 50, 1867},
	{PASS_PRIZE_EXP, 150, 1247},
	{PASS_PRIZE_SPECIAL_TOKENS, 50, 19058},
	{PASS_PRIZE_PREMIUM, 7, 19054},

	{PASS_PRIZE_EXP, 200, 1247},
	{PASS_PRIZE_VEHICLE_SLOT, 1, 11738},
	{PASS_PRIZE_MONEY, 300000, 1274},
	{PASS_PRIZE_ROB, 20, 1866},
	{PASS_PRIZE_SPECIAL_TOKENS, 200, 19058}
};

hook OnGameModeInit()
{
	CreateBattlepassTextdraws();
}

hook OnPlayerConnect(playerid)
{
	CreateBattlepassTextdraws(playerid);
}

hook OnPlayerClickPlayerTD(playerid, PlayerText:playertextid)
{
	if(BattlepassPage[playerid])
	{
		for(new x = 0; x <= 4; x++)
		{
			if(playertextid == BattlepassPTD[playerid][x + 25])
			{
				new prize_id = ((BattlepassPage[playerid] - 1) * 5) + x;
				OnPlayerCollectBattlepassPrize(playerid, prize_id);
				break;
			}
		}

		for(new x = 0; x <= 4; x++)
		{
			if(playertextid == BattlepassPTD[playerid][x + 30])
			{
				new prize_id = ((BattlepassPage[playerid] - 1) * 5) + x + 100;
				OnPlayerCollectBattlepassPrize(playerid, prize_id);
				break;
			}
		}
	}
}

hook OnPlayerClickTextDraw(playerid, Text:clickedid)
{
	// if page is >= 1, the player has the UI active
	if(BattlepassPage[playerid])
	{
		if(clickedid == Text:INVALID_TEXT_DRAW)
		{
			// after hiding the UI, the page is set to 0
			HideBattlepassUI(playerid);
		}	

		else if(clickedid == PASS_BUTTON_BUY)
		{
			if(HasPremiumBattlepass(playerid)) return;

			if(playerVariables[playerid][pAmethysts] < PASS_PP_PRICE)
			{
				SCM(playerid, COLOR_ERROR, "Eroare: Ai nevoie de %d ametiste pentru a cumpara battlepass-ul!", PASS_PP_PRICE);
				return;
			}

			HideBattlepassUI(playerid);
		
			new confirm[200];
			format(confirm, sizeof(confirm), "Esti sigur ca vrei sa cumperi varianta premium a battlepass-ului pentru %d ametiste?\nAi %d ametiste.", PASS_PP_PRICE, playerVariables[playerid][pAmethysts]);
			Dialog_ShowCallback(playerid, using public buy_battlepass<iiiis>, DIALOG_STYLE_MSGBOX, "SERVER: Buy Premium Battlepass", confirm, "Confirm", "Exit");
		} 

		else if(clickedid == PASS_BUTTON_LEFT)
		{
			if(BattlepassPage[playerid] > 1) 
			{
				BattlepassPage[playerid]--;
			}
			else 
			{
				BattlepassPage[playerid] = sizeof(FreeBattlepassPrizes) / 5;
			}

			UpdateBattlepassPageItems(playerid);
			UpdateBattlepassCollectUI(playerid);
		}

		else if(clickedid == PASS_BUTTON_RIGHT)
		{
			if(BattlepassPage[playerid] < sizeof(FreeBattlepassPrizes) / 5)
			{
				BattlepassPage[playerid]++;
			}
			else 
			{
				BattlepassPage[playerid] = 1;
			}

			UpdateBattlepassPageItems(playerid);
			UpdateBattlepassCollectUI(playerid);
		}
	}
}

CB:LoadBattlepassPrizes(playerid)
{
	for(new x; x < 2; x++)
	{
		for(new y; y < 100; y++)
		{
			BattlepassRewardsCollected[playerid][x][y] = 0;
		}
	}

	new prize_id;
	for(new x; x < cache_num_rows(); x++)
	{
		cache_get_value_name_int(x, "prize_id", prize_id);

		if(prize_id >= 100) BattlepassRewardsCollected[playerid][1][prize_id - 100] = 1;
		else BattlepassRewardsCollected[playerid][0][prize_id] = 1;
	}
}

CB:buy_battlepass(playerid, dialogid, response, listitem, string:inputtext[])
{
	if(!response)
	{
		return 1;
	}

	playerVariables[playerid][pBattlepassPremium] = 1;
	playerVariables[playerid][pAmethysts] -= PASS_PP_PRICE;

	SCM(playerid, COLOR_YELLOW, "Felicitari! Ai cumparat varianta premium a battlepass-ului cu %d ametiste.", PASS_PP_PRICE);

	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "UPDATE `server_accounts` SET `playerBattlepassPremium` = '1', `playerAmethysts` = '%d' WHERE `playerID` = '%d'", playerVariables[playerid][pAmethysts], playerVariables[playerid][pSQLID]);
	mysql_tquery(SQL, gQuery, "", "");
	return 1;
}

stock CreateBattlepassTextdraws(playerid = INVALID_PLAYER_ID)
{
	if(playerid == INVALID_PLAYER_ID)
	{
		BattlepassTD[0] = TextDrawCreate(320.000000, 169.000000, "_");
		TextDrawFont(BattlepassTD[0], 1);
		TextDrawLetterSize(BattlepassTD[0], 0.600000, 18.100021);
		TextDrawTextSize(BattlepassTD[0], 298.500000, 308.500000);
		TextDrawSetOutline(BattlepassTD[0], 1);
		TextDrawAlignment(BattlepassTD[0], 2);
		TextDrawColor(BattlepassTD[0], -1);
		TextDrawBackgroundColor(BattlepassTD[0], 255);
		TextDrawBoxColor(BattlepassTD[0], 128);
		TextDrawUseBox(BattlepassTD[0], 1);
		TextDrawSetProportional(BattlepassTD[0], 1);

		BattlepassTD[1] = TextDrawCreate(320.000000, 166.000000, "_");
		TextDrawFont(BattlepassTD[1], 1);
		TextDrawLetterSize(BattlepassTD[1], 0.600000, 0.450002);
		TextDrawTextSize(BattlepassTD[1], 298.500000, 308.500000);
		TextDrawSetOutline(BattlepassTD[1], 1);
		TextDrawAlignment(BattlepassTD[1], 2);
		TextDrawColor(BattlepassTD[1], -1);
		TextDrawBackgroundColor(BattlepassTD[1], 255);
		TextDrawBoxColor(BattlepassTD[1], -1029514497);
		TextDrawUseBox(BattlepassTD[1], 1);
		TextDrawSetProportional(BattlepassTD[1], 1);

		BattlepassTD[2] = TextDrawCreate(320.000000, 335.000000, "_");
		TextDrawFont(BattlepassTD[2], 1);
		TextDrawLetterSize(BattlepassTD[2], 0.600000, 0.450002);
		TextDrawTextSize(BattlepassTD[2], 298.500000, 308.500000);
		TextDrawSetOutline(BattlepassTD[2], 1);
		TextDrawAlignment(BattlepassTD[2], 2);
		TextDrawColor(BattlepassTD[2], -1);
		TextDrawBackgroundColor(BattlepassTD[2], 255);
		TextDrawBoxColor(BattlepassTD[2], -1029514497);
		TextDrawUseBox(BattlepassTD[2], 1);
		TextDrawSetProportional(BattlepassTD[2], 1);

		BattlepassTD[3] = TextDrawCreate(320.000000, 154.000000, "Battlepass");
		TextDrawFont(BattlepassTD[3], 0);
		TextDrawLetterSize(BattlepassTD[3], 0.391665, 1.649999);
		TextDrawTextSize(BattlepassTD[3], 400.000000, 227.500000);
		TextDrawSetOutline(BattlepassTD[3], 1);
		TextDrawAlignment(BattlepassTD[3], 2);
		TextDrawColor(BattlepassTD[3], -1);
		TextDrawBackgroundColor(BattlepassTD[3], 255);
		TextDrawBoxColor(BattlepassTD[3], 50);
		TextDrawUseBox(BattlepassTD[3], 0);
		TextDrawSetProportional(BattlepassTD[3], 1);

		BattlepassTD[4] = TextDrawCreate(261.000000, 344.000000, "ld_beat:left");
		TextDrawFont(BattlepassTD[4], 4);
		TextDrawLetterSize(BattlepassTD[4], 0.600000, 2.000000);
		TextDrawTextSize(BattlepassTD[4], 18.000000, 21.000000);
		TextDrawSetOutline(BattlepassTD[4], 1);
		TextDrawAlignment(BattlepassTD[4], 1);
		TextDrawColor(BattlepassTD[4], -1);
		TextDrawBackgroundColor(BattlepassTD[4], 255);
		TextDrawBoxColor(BattlepassTD[4], 50);
		TextDrawUseBox(BattlepassTD[4], 1);
		TextDrawSetProportional(BattlepassTD[4], 1);
		TextDrawSetSelectable(BattlepassTD[4], 1);

		BattlepassTD[5] = TextDrawCreate(362.000000, 344.000000, "ld_beat:right");
		TextDrawFont(BattlepassTD[5], 4);
		TextDrawLetterSize(BattlepassTD[5], 0.600000, 2.000000);
		TextDrawTextSize(BattlepassTD[5], 18.000000, 21.000000);
		TextDrawSetOutline(BattlepassTD[5], 1);
		TextDrawSetShadow(BattlepassTD[5], 0);
		TextDrawAlignment(BattlepassTD[5], 1);
		TextDrawColor(BattlepassTD[5], -1);
		TextDrawBackgroundColor(BattlepassTD[5], 255);
		TextDrawBoxColor(BattlepassTD[5], 50);
		TextDrawUseBox(BattlepassTD[5], 1);
		TextDrawSetProportional(BattlepassTD[5], 1);
		TextDrawSetSelectable(BattlepassTD[5], 1);

		BattlepassTD[6] = TextDrawCreate(320.000000, 371.000000, "BUY BATTLEPASS~n~500 AMETHYSTS");
		TextDrawFont(BattlepassTD[6], 2);
		TextDrawLetterSize(BattlepassTD[6], 0.166666, 1.250000);
		TextDrawTextSize(BattlepassTD[6], 50.000000, 104.000000);
		TextDrawSetOutline(BattlepassTD[6], 1);
		TextDrawSetShadow(BattlepassTD[6], 0);
		TextDrawAlignment(BattlepassTD[6], 2);
		TextDrawColor(BattlepassTD[6], -1);
		TextDrawBackgroundColor(BattlepassTD[6], 255);
		TextDrawBoxColor(BattlepassTD[6], -1029514497);
		TextDrawUseBox(BattlepassTD[6], 1);
		TextDrawSetProportional(BattlepassTD[6], 1);
		TextDrawSetSelectable(BattlepassTD[6], 1);
	}

	else
	{
		// 0-4: page levels
		for(new x; x <= 4; x++)
		{
			BattlepassPTD[playerid][x] = CreatePlayerTextDraw(playerid, 196.000000 + (x * 62), 175.000000, "1");
			PlayerTextDrawFont(playerid, BattlepassPTD[playerid][x], 3);
			PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][x], 0.270832, 1.149999);
			PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][x], 400.000000, 58.500000);
			PlayerTextDrawSetOutline(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][x], 2);
			PlayerTextDrawColor(playerid, BattlepassPTD[playerid][x], -1);
			PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][x], 255);
			PlayerTextDrawBoxColor(playerid, BattlepassPTD[playerid][x], -1029514704);
			PlayerTextDrawUseBox(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][x], 1);
		}
	
		// 5-9: free levels preview
		for(new x = 5; x <= 9; x++)
		{
			BattlepassPTD[playerid][x] = CreatePlayerTextDraw(playerid, 165.333343 + ((x - 5) * 62), 188.000000, "Preview_Model");
			PlayerTextDrawFont(playerid, BattlepassPTD[playerid][x], 5);
			PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][x], 0.600000, 2.000000);
			PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][x], 61.250000, 71.500000);
			PlayerTextDrawSetOutline(playerid, BattlepassPTD[playerid][x], 0);
			PlayerTextDrawSetShadow(playerid, BattlepassPTD[playerid][x], 0);
			PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawColor(playerid, BattlepassPTD[playerid][x], -1);
			PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][x], -1029514704);
			PlayerTextDrawBoxColor(playerid, BattlepassPTD[playerid][x], -225);
			PlayerTextDrawUseBox(playerid, BattlepassPTD[playerid][x], 0);
			PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawSetSelectable(playerid, BattlepassPTD[playerid][x], 0);
			PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][x], 1274);
			PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][x], 0.000000, 0.000000, 180.000000, 1.000000);
			PlayerTextDrawSetPreviewVehCol(playerid, BattlepassPTD[playerid][x], 1, 1);
		}
		
		// 10-14: free levels text
		for(new x = 10; x <= 14; x++)
		{
			BattlepassPTD[playerid][x] = CreatePlayerTextDraw(playerid, 196.000000 + ((x - 10) * 62), 216.000000, "prize");
			PlayerTextDrawFont(playerid, BattlepassPTD[playerid][x], 2);
			PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][x], 0.158333, 1.149999);
			PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][x], 400.000000, 58.500000);
			PlayerTextDrawSetOutline(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][x], 2);
			PlayerTextDrawColor(playerid, BattlepassPTD[playerid][x], -1);
			PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][x], 255);
			PlayerTextDrawBoxColor(playerid, BattlepassPTD[playerid][x], -1029514704);
			PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][x], 1);
		}

		// 15-19: paid levels preview
		for(new x = 15; x <= 19; x++)
		{
			BattlepassPTD[playerid][x] = CreatePlayerTextDraw(playerid, 165.333343 + ((x - 15) * 62), 260.666656, "Preview_Model");
			PlayerTextDrawFont(playerid, BattlepassPTD[playerid][x], 5);
			PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][x], 0.600000, 2.000000);
			PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][x], 61.250000, 71.500000);
			PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawColor(playerid, BattlepassPTD[playerid][x], -1);
			PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][x], -2125094096);
			PlayerTextDrawBoxColor(playerid, BattlepassPTD[playerid][x], -225);
			PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][x], 1274);
			PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][x], 0.000000, 0.000000, 180.000000, 1.000000);
			PlayerTextDrawSetPreviewVehCol(playerid, BattlepassPTD[playerid][x], 1, 1);
		}

		// 20-24: paid levels text
		for(new x = 20; x <= 24; x++)
		{
			BattlepassPTD[playerid][x] = CreatePlayerTextDraw(playerid, 196.000000 + ((x - 20) * 62), 290.000000, "paid_prize");
			PlayerTextDrawFont(playerid, BattlepassPTD[playerid][x], 2);
			PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][x], 0.158333, 1.149999);
			PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][x], 400.000000, 58.500000);
			PlayerTextDrawSetOutline(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][x], 2);
			PlayerTextDrawColor(playerid, BattlepassPTD[playerid][x], -1);
			PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][x], 255);
			PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][x], 1);
		}

		// 25-34: locked levels / collect level
		// 25-29 free, 30-34 paid
		new Float:x_coords = 165.333343, Float:y_coords = 188.0;
		for(new x = 25; x <= 34; x++)
		{
			BattlepassPTD[playerid][x] = CreatePlayerTextDraw(playerid, x_coords, y_coords, "Preview_Model");
			PlayerTextDrawFont(playerid, BattlepassPTD[playerid][x], 5);
			PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][x], 0.600000, 2.000000);
			PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][x], 15.750000, 19.000000);
			PlayerTextDrawSetOutline(playerid, BattlepassPTD[playerid][x], 0);
			PlayerTextDrawSetShadow(playerid, BattlepassPTD[playerid][x], 0);
			PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawColor(playerid, BattlepassPTD[playerid][x], -65281);
			PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][x], -2125094144);
			PlayerTextDrawBoxColor(playerid, BattlepassPTD[playerid][x], -225);
			PlayerTextDrawUseBox(playerid, BattlepassPTD[playerid][x], 0);
			PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawSetSelectable(playerid, BattlepassPTD[playerid][x], 1);
			PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][x], 1247);
			PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][x], 0.000000, 0.000000, 180.000000, 1.000000);
			PlayerTextDrawSetPreviewVehCol(playerid, BattlepassPTD[playerid][x], 1, 1);
		
			if(x == 29)
			{
				x_coords = 165.333343;
				y_coords = 260.666656;
			}

			else x_coords += 62;
		}

		// 35: progress
		BattlepassPTD[playerid][35] = CreatePlayerTextDraw(playerid, 166.000000, 343.000000, "Level: ~p~1~n~~w~~h~XP: ~p~0~w~~h~/~p~100");
		PlayerTextDrawFont(playerid, BattlepassPTD[playerid][35], 2);
		PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][35], 0.145833, 1.000000);
		PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][35], 400.000000, 17.000000);
		PlayerTextDrawSetOutline(playerid, BattlepassPTD[playerid][35], 1);
		PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][35], 1);
		PlayerTextDrawColor(playerid, BattlepassPTD[playerid][35], -1);
		PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][35], 255);
		PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][35], 1);

		// 36: current page
		BattlepassPTD[playerid][36] = CreatePlayerTextDraw(playerid, 320.000000, 347.000000, "page: 1/10");
		PlayerTextDrawFont(playerid, BattlepassPTD[playerid][36], 3);
		PlayerTextDrawLetterSize(playerid, BattlepassPTD[playerid][36], 0.308331, 1.599997);
		PlayerTextDrawTextSize(playerid, BattlepassPTD[playerid][36], 400.000000, 76.000000);
		PlayerTextDrawSetOutline(playerid, BattlepassPTD[playerid][36], 1);
		PlayerTextDrawAlignment(playerid, BattlepassPTD[playerid][36], 2);
		PlayerTextDrawColor(playerid, BattlepassPTD[playerid][36], -1);
		PlayerTextDrawBackgroundColor(playerid, BattlepassPTD[playerid][36], 255);
		PlayerTextDrawBoxColor(playerid, BattlepassPTD[playerid][36], -1029514497);
		PlayerTextDrawSetProportional(playerid, BattlepassPTD[playerid][36], 1);
	}
}

// prize_id: 0-99 free, 100-199 paid
stock IsPrizeCollectedByPlayer(playerid, prize_id)
{
	if(prize_id >= 100)
	{
		return BattlepassRewardsCollected[playerid][1][prize_id - 100];
	}

	return BattlepassRewardsCollected[playerid][0][prize_id];
}

stock OnPlayerCollectBattlepassPrize(playerid, prize_id)
{
	if(IsPrizeCollectedByPlayer(playerid, prize_id)) return 1;

	new prize_type, prize_amount, prize_prop_id, paid = 0;
	GetBattlepassPrizeInfo(prize_id, prize_type, prize_amount, prize_prop_id);

	if(prize_id >= 100)
	{
		if(!HasPremiumBattlepass(playerid))
		{
			return SendClientMessage(playerid, COLOR_ERROR, "Eroare: Nu poti colecta acest premiu deoarece nu ai varianta premium a battlepass-ului!");
		}
		
		prize_id -= 100;

		paid = 1;
	}

	if(GetBattlepassLevel(playerid) < prize_id + 1)
	{
		SCM(playerid, COLOR_ERROR, "Eroare: Ai nevoie de level %d pentru a colecta acest premiu.", prize_id + 1);
		return 1;
	}
	
	BattlepassRewardsCollected[playerid][paid][prize_id] = 1;

	switch(prize_type)
	{
		case PASS_PRIZE_MONEY:
		{
			GivePlayerCash(playerid, prize_amount);
		}

		case PASS_PRIZE_PP:
		{
			playerVariables[playerid][pAmethysts] += prize_amount;
		}

		case PASS_PRIZE_ROB:
		{
			playerVariables[playerid][pRobPoints] += prize_amount;
		}
	
		case PASS_PRIZE_EXP:
		{
			GivePlayerXP(playerid, prize_amount);
		}

		case PASS_PRIZE_VEHICLE:
		{
			new rand = randomEx(0, 20);
			GivePlayerVehicle(playerid, prize_amount, unstuck_pos[rand][0], unstuck_pos[rand][1], unstuck_pos[rand][2], unstuck_pos[rand][3], 0, prize_prop_id);
		}

		case PASS_PRIZE_VEHICLE_SLOT:
		{
			playerVariables[playerid][pMaxVehicles] += prize_amount;
		
			OnSeasonQuestsProgress(playerid, SQ_TYPE_VEH_SLOT, 0, prize_amount);
		}

		case PASS_PRIZE_SKIN:
		{
			GivePlayerSkin(playerid, prize_amount);
		}

		case PASS_PRIZE_SPECIAL_TOKENS:
		{
			playerVariables[playerid][pSpecialTokens] += prize_amount;
		}
	
		case PASS_PRIZE_PREMIUM:
		{
			if(playerVariables[playerid][pPremiumAccount] != 1337) 
			{
				if(!playerVariables[playerid][pPremiumAccount]) 
				{
					playerVariables[playerid][pPremiumAccount] = gettime() + (86400 * prize_amount);
				}

				else 
				{
					playerVariables[playerid][pPremiumAccount] = playerVariables[playerid][pPremiumAccount] + (86400 * prize_amount); 
				}
			}
		}
	}

	SCM(playerid, COLOR_MONEY, "(+) Premiu colectat! Ai primit %s.", GetBattlepassPrizeName(prize_id + (paid * 100)));

	UpdateBattlepassCollectUI(playerid);

	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "INSERT INTO `bp_collected_prizes` (`user_id`, `prize_id`) VALUES ('%d', '%d')", playerVariables[playerid][pSQLID], prize_id + (paid * 100));
	mysql_tquery(SQL, gQuery, "", "");
	return 1;
}

stock ResetBattlepassUI(playerid)
{
	BattlepassPage[playerid] = 1;

	return 1;
}

stock GetBattlepassLevel(playerid)
{
	return playerVariables[playerid][pBattlepassXP] / PASS_XP_NEEDED;
}

stock GetBattlepassXP(playerid)
{
	return playerVariables[playerid][pBattlepassXP];
}

stock GiveBattlepassXP(playerid, amount)
{
	new old = GetBattlepassLevel(playerid);
	playerVariables[playerid][pBattlepassXP] += amount;

	if(GetBattlepassLevel(playerid) > old)
	{
		SCM(playerid, COLOR_MONEY, "BATTLEPASS: Ai avansat la nivelul %d deoarece ai acumulat experienta necesara. Felicitari!", GetBattlepassLevel(playerid));
	}

	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "UPDATE `server_accounts` SET `playerBattlepassXP` = '%d' WHERE `playerID` = '%d'", GetBattlepassXP(playerid), playerVariables[playerid][pSQLID]);
	mysql_tquery(SQL, gQuery, "", "");
	return 1;
}

stock UpdateBattlepassProgressUI(playerid)
{
	gString[0] = EOS;
	if(GetBattlepassLevel(playerid) >= sizeof(FreeBattlepassPrizes))
	{
		format(gString, sizeof(gString), "Level: ~p~MAX");
	}
	else
	{
		format(gString, sizeof(gString), "Level: ~p~%d~n~~w~~h~XP: ~p~%d~w~~h~/~p~%d", GetBattlepassLevel(playerid), GetBattlepassXP(playerid), (GetBattlepassLevel(playerid) * PASS_XP_NEEDED) + PASS_XP_NEEDED);
	}

	PlayerTextDrawSetString(playerid, BattlepassPTD[playerid][35], gString);
	return 1;
}

stock GetBattlepassPageInfo(playerid, &page, &min_item, &max_item)
{
 	page = BattlepassPage[playerid] - 1;

	min_item = page * 5;
	max_item = min_item + 4;
}

stock UpdateBattlepassPageItems(playerid)
{
	new page, min_item, max_item;
	GetBattlepassPageInfo(playerid, page, min_item, max_item);

	for(new x = min_item; x <= max_item; x++)
	{
		// top level
		gString[0] = EOS;
		format(gString, sizeof(gString), "%d", x + 1);
		PlayerTextDrawSetString(playerid, BattlepassPTD[playerid][x - min_item], gString);		
	}

	UpdateBattlepassPagePrizes(playerid, min_item);

	gString[0] = EOS;
	format(gString, sizeof(gString), "Page: %d/%d", page + 1, sizeof(FreeBattlepassPrizes) / 5);
	PlayerTextDrawSetString(playerid, BattlepassPTD[playerid][36], gString);
	return 1;
}

stock UpdateBattlepassPagePrizes(playerid, start)
{
	new prize_id;
	for(new x; x < 5; x++)
	{
		// update the free ones
		prize_id = start + x;
		UpdateBattlepassPrize(playerid, x + 5, prize_id);

		// update the paid ones
		prize_id += 100;
		UpdateBattlepassPrize(playerid, x + 15, prize_id);
	}

	return 1;
}

stock UpdateBattlepassPrize(playerid, index, prize_id)
{
	new prize_type, prize_amount, prize_prop_id;
	GetBattlepassPrizeInfo(prize_id, prize_type, prize_amount, prize_prop_id);
	PlayerTextDrawHide(playerid, BattlepassPTD[playerid][index]);
	PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][index], prize_prop_id);
	PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][index], 0.0, 0.0, 0.0, 1.0);

	if(prize_prop_id >= 0 && prize_prop_id <= 611)
	{
		PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][index], 0.0, 0.0, 30.0, 1.0);
	}

	else
	{
		switch(prize_prop_id)
		{
			// money
			case 1274: PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][index], 0.0, 0.0, 180.0, 1.0);

			// exp, pp, rob
			case 1862, 1866, 1867: PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][index], 90.0, 0.0, 0.0, 1.0);

			//
			case 0..9: 
			{
				PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][index], 2972);
				PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][index], 0.0, 0.0, 45.0, 1.0); 
			}
		}
	}

	PlayerTextDrawSetString(playerid, BattlepassPTD[playerid][index + 5], GetBattlepassPrizeName(prize_id));
	PlayerTextDrawShow(playerid, BattlepassPTD[playerid][index]);
}

stock UpdateBattlepassCollectUI(playerid)
{
    new page, min_item, max_item;
	GetBattlepassPageInfo(playerid, page, min_item, max_item);

	for(new x = 25; x <= 34; x++) 
	{
		PlayerTextDrawHide(playerid, BattlepassPTD[playerid][x]);
	}

	new prize_id = 0, flag = 0;

	for(new x = min_item; x <= max_item; x++)
	{
		prize_id = x;

		if(!IsPrizeCollectedByPlayer(playerid, prize_id)) 
		{
			if(GetBattlepassLevel(playerid) >= (prize_id + 1))
			{
				PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][flag + 25], 1247);
			}	
			else
			{
				PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][flag + 25], 19804);
			}

			PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][flag + 25], 0.000000, 0.000000, 180.000000, 1.000000);
			PlayerTextDrawSetPreviewVehCol(playerid, BattlepassPTD[playerid][flag + 25], 1, 1);
			PlayerTextDrawShow(playerid, BattlepassPTD[playerid][flag + 25]);
		}

   		flag++;
	}

	flag = 0;
	for(new x = min_item; x <= max_item; x++)
	{
		prize_id = x + 100;
		
		if(!IsPrizeCollectedByPlayer(playerid, prize_id)) 
		{
			prize_id -= 100;
			if(GetBattlepassLevel(playerid) >= (prize_id + 1) && HasPremiumBattlepass(playerid))
			{
				PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][flag + 30], 1247);
			}	
			else
			{
				PlayerTextDrawSetPreviewModel(playerid, BattlepassPTD[playerid][flag + 30], 19804);
			}

			PlayerTextDrawSetPreviewRot(playerid, BattlepassPTD[playerid][flag + 30], 0.000000, 0.000000, 180.000000, 1.000000);
			PlayerTextDrawSetPreviewVehCol(playerid, BattlepassPTD[playerid][flag + 30], 1, 1);
			PlayerTextDrawShow(playerid, BattlepassPTD[playerid][flag + 30]);
		}

		flag++;
	}

    return 1;
}

stock ShowBattlepassUI(playerid)
{
	// global textdraws
	for(new x; x < 6; x++)
	{
		TextDrawShowForPlayer(playerid, BattlepassTD[x]);
	}

	if(!HasPremiumBattlepass(playerid))
	{
		TextDrawShowForPlayer(playerid, PASS_BUTTON_BUY);
	}

	BattlepassPage[playerid] = 1;
	UpdateBattlepassProgressUI(playerid);
	UpdateBattlepassPageItems(playerid);

	// player textdraws
	for(new x; x <= 36; x++) 
	{
		if(x >= 25 && x <= 34) continue;
		
		PlayerTextDrawShow(playerid, BattlepassPTD[playerid][x]);
	}

	UpdateBattlepassCollectUI(playerid);

	SelectTextDraw(playerid, 0xFFFFFFFF);
	return 1;
}

stock HideBattlepassUI(playerid)
{
	for(new x; x < 7; x++)
	{
		TextDrawHideForPlayer(playerid, BattlepassTD[x]);
	}

	for(new x; x <= 36; x++)
	{
		PlayerTextDrawHide(playerid, BattlepassPTD[playerid][x]);
	}

	CancelSelectTextDraw(playerid);
	BattlepassPage[playerid] = 0;
	return 1;
}

stock HasPremiumBattlepass(playerid)
{
	return playerVariables[playerid][pBattlepassPremium];
}

stock LoadBattlepassStats(playerid)
{
	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "SELECT * FROM `bp_collected_prizes` WHERE `user_id` = '%d'", playerVariables[playerid][pSQLID]);
	mysql_pquery(SQL, gQuery, "LoadBattlepassPrizes", "i", playerid);
}

stock GetBattlepassPrizeInfo(prize_id, &prize_type, &prize_amount, &prize_prop_id)
{
	if(prize_id >= 100)
	{
		prize_id -= 100;
		prize_type = 1;
	}

	if(prize_type == 1)
	{
		prize_type = PaidBattlepassPrizes[prize_id][paid_prize_type];
		prize_amount = PaidBattlepassPrizes[prize_id][paid_prize_amount];
		prize_prop_id = PaidBattlepassPrizes[prize_id][paid_prize_prop_id];
	}
	else
	{
		prize_type = FreeBattlepassPrizes[prize_id][free_prize_type];
		prize_amount = FreeBattlepassPrizes[prize_id][free_prize_amount];
		prize_prop_id = FreeBattlepassPrizes[prize_id][free_prize_prop_id];
	}

	if(prize_prop_id <= 0) prize_prop_id = 18631;

	if(prize_type == PASS_PRIZE_VEHICLE)
	{
		prize_prop_id = prize_amount;
	}
}

stock GetBattlepassPrizeName(prize_id)
{
	new prize_type, prize_amount, prize_prop_id;
	GetBattlepassPrizeInfo(prize_id, prize_type, prize_amount, prize_prop_id);

	new name[32];
	switch(prize_type)
	{
		case PASS_PRIZE_MONEY:
		{
			format(name, sizeof(name), "$%s", FormatNumber(prize_amount));
		}

		case PASS_PRIZE_EXP:
		{
			format(name, sizeof(name), "%d XP", prize_amount);
		}

		case PASS_PRIZE_PP:
		{
			format(name, sizeof(name), "%d amethysts", prize_amount);
		}
		
		case PASS_PRIZE_VEHICLE_SLOT:
		{
			format(name, sizeof(name), "%dx vehicle slot", prize_amount);
		}
		
		case PASS_PRIZE_ROB:
		{
			format(name, sizeof(name), "%d rob points", prize_amount);
		}

		case PASS_PRIZE_VEHICLE:
		{
			format(name, sizeof(name), vehNames[prize_amount - 400]);
		}
	
		case PASS_PRIZE_SKIN:
		{
			format(name, sizeof(name), "skin %d", prize_prop_id);
		}

		case PASS_PRIZE_SPECIAL_TOKENS:
		{
			format(name, sizeof(name), "%d special tokens", prize_amount);
		}
	
		case PASS_PRIZE_PREMIUM:
		{
			format(name, sizeof(name), "premium for %d days", prize_amount);
		}
	}

	return name;
}

stock IsBattlepassActive()
{
	return serverVariables[serverBattlepassActive];
}

YCMD:pass(playerid, params[], help)
{	
	if(!IsBattlepassActive()) return SCM(playerid, COLOR_ERROR, "Battlepass-ul nu este activ!");
 	
	ResetBattlepassUI(playerid);

	ShowBattlepassUI(playerid);
	return 1;
}

YCMD:startbp(playerid, params[], help)
{	
	check_owner

	if(IsBattlepassActive()) return SCM(playerid, COLOR_ERROR, "Battlepass-ul este deja activ!");

	serverVariables[serverBattlepassActive] = 1;
	mysql_tquery(SQL, "UPDATE `server_settings` SET `serverBattlepassActive` = '1'", "", "");

	SCM(playerid, COLOR_YELLOW, "Battlepass-ul a fost activat!");
	return 1;
}

YCMD:endbp(playerid, params[], help)
{	
	check_owner

	if(!IsBattlepassActive()) return SCM(playerid, COLOR_ERROR, "Battlepass-ul este deja dezactivat!");

	serverVariables[serverBattlepassActive] = 0;
	mysql_tquery(SQL, "UPDATE `server_settings` SET `serverBattlepassActive` = '0'", "", "");

	SCM(playerid, COLOR_YELLOW, "Battlepass-ul a fost deactivat!");
	return 1;
}