<?php

	#Security
	define( 'IN_MALL' , 0 );
	
	#Include config
	require_once( './config.php' );
	
	#Include bootstrap
	require_once( './bootstrap.php' );
	
	#Logged In
	if( isset( $_SESSION['userName'] ) )
	{
		#Lel
		if( $_SESSION['logIP'] != $_SERVER['REMOTE_ADDR'] )
		{
			session_destroy();
			
			exit;
		}
	
		#Trying to buy
		if( isset( $_POST['buy'] ) )
		{
			$Item = Functions::mssql_escape( $_POST['buy'] );

			#Numbers only
			if( preg_match( '/^[0-9][0-9]*$/' , $Item ) == 1 )
			{
				#Get Info
				$ItemInfo = Functions::GetItemInfo( $Item );
				
				#Item doesn't exist
				if( count( $ItemInfo ) == 0 )
				{
					Functions::Error( 'Invalid item' );
				}
				
				#Fucking nest
				$ItemInfo = $ItemInfo[0];
				
				#Get user points
				$Points = Functions::GetUserPoints( $_SESSION['nEMID'] );
				
				#Failed to get user points
				if( count( $Points ) == 0 )
				{
					Functions::Error( 'Server error' );
				}
				
				#Fucking nest
				$Points = $Points[0]['nAGPoints'];

				#Enough points?
				if( $ItemInfo['nPrice'] > $Points )
				{
					Functions::Error( 'You do not have enough points' );
				}
				
				#Deduct points
				Functions::SubtractPoints( $_SESSION['nEMID'] , $ItemInfo['nPrice'] );

				#Give item
				Functions::AddItem( $_SESSION['nEMID'] , $ItemInfo );
				
				Functions::Error( 'Item bought' );
			}
			else
			{
				Functions::Error( "Invalid item" );
			}		
			
		}
		#Coins
		else if( isset( $_GET['coins'] ) )
		{
			$Coins = functions::GetUserPoints( $_SESSION['nEMID'] );
			
			$Coins = $Coins[0]['nAGPoints'];		
			
			echo $Coins;
		}
		#Specific category
		else if( isset( $_GET['cat'] ) )
		{
			$Cat = $_GET['cat'];
		
			#Valid sub-category?
			#Sub-Cats are number indexed
			if( preg_match( '/^[0-9][0-9]*$/' , $Cat ) == 1 )
			{
				$Results = Functions::GetCat( $Cat );
				
				#Category is real
				if( count( $Results ) > 0 )
				{
					$Results = $Results[0]['nCat'];
				
					$Items = Functions::GetItemsByCat( $Results );
					
					#No items in category
					if( count( $Items ) > 0 )
					{
						echo '<ul class="tab-content">';
					
						foreach( $Items as $Key => $Info )
						{
							$Info = (object) $Info;
							
							functions::PrintItem( $Info );
						}	
						
						echo '</ul>';
					}
					else
					{
						Functions::Error( '<script>$("#main").html(\'<div class="alert alert-danger">Sorry! But there is no Item in this Category.<button type="button" class="close" data-dismiss="alert">&times;</button></div>\');</script>' , true );
					}
				}
				else
				{
					Functions::Error( 'Invalid category' , true );
				}
			}
			else if( $Cat == -1 )
			{
				echo '<center><iframe src="Please add: BLABLA to get coins!</iframe></center>';
			}
			else if( $Cat == -2 )
			{
				echo '<center><iframe src="/top.php" width="750" height="975" frameborder="0"></iframe></center>';
			}
			else
			{
				Functions::Error( "Invalid category" , true );
			}
		}
		#Tried to log out?
		else if( isset( $_GET['logout'] ) )
		{
			Functions::LogOut();
			
			Functions::Redirect( 'index.php' );
		}		
		#Default
		else
		{
			$Coins = functions::GetUserPoints( $_SESSION['nEMID'] );
			
			$Coins = $Coins[0]['nAGPoints'];
			
			echo '
			<ul class="nav navbar-nav side-nav ng-scope" style="margin-top: -50px;" ng-controller="navController">
                    <div class="app-user">
                        <div class="app-user-icon"><img style="height:36px;width:93px;" src="./Template/img/logo.png"></div>
                        <div class="app-user-name"><p class="ng-binding">' . sprintf( "Logged in as, <b style=\"color:white;\">%s</b>" , htmlentities( $_SESSION['userName'] ) ) . '</p></div>
                        <div class="app-user-coins"><p class="user-amount ng-binding" ng-bind="userCoins">' . sprintf( "<b style=\"color:white;\">%s</b>" , htmlentities( $Coins ) ) . '</p></div>
                        <button type="button" class="btn btn-default btn-block btn-donate" <a href="#" onclick="loadCat(-1); return false;"><img style="height:15px;width:15px;" src="./Template/img/dollar.png" alt="" /> Buy Coins</a></button>
						<div class="app-signout">
                        <button type="button" class="btn btn-default btn-block btn-signout" <a href="#" onclick="Logout(); return false;">SIGN OUT</button>
                    </div>
						
                    </div>
';
						
			$i = Functions::PrintCategories();
			
				
			
			echo '</div></div>';
			
			if( $i == 0 )
			{
				Functions::Error( "There are no mall items yet!" , true );
			}
			else
			{
				echo '
				<script>$("#main").html(\'<div class="alert alert-danger">Please select a category to get started!<button type="button" class="close" data-dismiss="alert">&times;</button></div>\');</script>'
				;
			}
		}
	}
	#Not logged in
	else
	{
		#Tried to log in?
		if( isset( $_POST['username'] ) && isset( $_POST['password'] ) )
		{
			$Username = Functions::mssql_escape( $_POST['username'] );
			
			$Password = Functions::mssql_escape( $_POST['password'] );
			
			#Validate input
			if( strlen( $Username ) < 3 || strlen( $Username ) > 15 || preg_match( '/[^a-zA-Z0-9]/' , $Username ) > 0 )
			{
				Functions::Error( '3' );
			}
			elseif( strlen( $Password ) < 5 || strlen( $Password ) > 20 )
			{
				Functions::Error( '2' );
			}
			
			#Hash password
			$Password = Functions::MakePw( $Username , $Password );
			
			#User didn't exist
			if( $Password == null )
			{
				Functions::Error( '1' );
			}

			#Validate login
			$Results = Functions::LoginValidate( $Username , $Password );
			
			if( count( $Results ) > 0 )
			{
				$Results = $Results[0];
			
				Functions::SetLogin( $Results );
				
				Functions::Error( '4' );
			}
			#Invalid
			else
			{
				Functions::Error( '1' );
			}
		}
		#Nada
		else
		{
			Functions::LoggedOut();
		}
	}
?>