// Special thanks to <TgN>KillTheEnemy - sir_timotheus@yahoo.com
// For all this help on this! ~Joker

init()
{
	if(!level.ex_switch_scopezoom) return;

	// Precache Menu
	game["menu_bind"] = "bind";
	[[level.ex_PrecacheMenuItem]](game["menu_bind"]);
	
	level thread onPlayerConnect();
}

onPlayerConnect()
{
	for(;;)
	{
		level waittill("connecting", player);
		player thread onPlayerSpawned();
	}
}

onPlayerSpawned()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("spawned_player");

		self.is_Zooming = false;
		self thread monitorZoom();
		self thread monitorExtraKeys();
	}
}

monitorExtraKeys()
{
	//self endon("intermission");
	self endon("disconnect");

	if(isDefined(self.monitorExtraKeys)) return;
	self.monitorExtraKeys = true;
	wait(1);
	if(!isDefined(self)) return;

	self setClientCvar("ui_bind_key", "bind m openScriptMenu keysMenu m");
	self openMenu(game["menu_bind"]);
	wait(0.5);
	self closeMenu();

	while(isDefined(self))
	{
		self waittill("menuresponse", menu, response);

		if(menu == "-1" && isAlive(self))
			self notify( "key_" + response);
	}
}

monitorZoom()
{
	while(isPlayer(self) && self.sessionstate == "playing")
	{
		self waittill("key_m");

		weapon = self getCurrentWeapon();
		alterego = getWeaponCounterpart(weapon);
		if(alterego == "none") continue;

		if(weapon == self getWeaponSlotWeapon("primary")) weaponslot = "primary";
			else if(weapon == self getWeaponSlotWeapon("primaryb")) weaponslot = "primaryb";
				else continue;

		self.is_Zooming = true;

		ammo = self getweaponslotammo(weaponslot);
		clipammo = self getweaponslotclipammo(weaponslot);
		self takeWeapon(weapon);
		self setWeaponSlotWeapon(weaponslot, alterego);
		self setweaponslotammo(weaponslot, ammo);
		self setweaponslotclipammo(weaponslot, clipammo);
		self switchToWeapon(alterego);
		wait 1;

		self.is_Zooming = false;
	}
}

getWeaponCounterpart(weapon)
{
	switch(weapon)
	{
		case "springfield_mp": return "springfield_2_mp";
		case "springfield_2_mp": return "springfield_mp";
		case "enfield_scope_mp": return "enfield_scope_2_mp";
		case "enfield_scope_2_mp": return "enfield_scope_mp";
		case "mosin_nagant_sniper_mp": return "mosin_nagant_sniper_2_mp";
		case "mosin_nagant_sniper_2_mp": return "mosin_nagant_sniper_mp";
		case "g43_sniper": return "g43_sniper_2";
		case "g43_sniper_2": return "g43_sniper";
		case "kar98k_sniper_mp": return "kar98k_sniper_2_mp";
		case "kar98k_sniper_2_mp": return "kar98k_sniper_mp";
		default: return "none";
	}
}
