<?php
session_start();
if(isset($_SESSION['user'])) {
	header("Location: /home");
}

require_once 'core/classes.php';
require_once 'core/classes/User.php';
require_once 'core/config.php';

$getitstarted = new PartyStarter;
$getitstarted->header();
$usernamespecialchar = false;
$passwordconfirmiswrong = false;
$passwordistooshort = false;
$user = new User($con, 0);
$userexists = false;
if(isset($_POST['submit'])) {
	$username = $_POST['username'];
	$password = $_POST['pass'];
	$passwordconfirm = $_POST['passconfirm'];
	/*echo '<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>';*/
	$user = new User($con, $user->getID($con, $username));
    if(preg_match('/^[a-zA-Z0-9]+$/', $username) == 0) {
        $usernamespecialchar = true;
    } else {
        $usernamespecialchar = false;
    }

	if ($password == $passwordconfirm) {
      $passwordconfirmiswrong = false;
	} else {
	 $passwordconfirmiswrong = true;
	}
	
	if (strlen($password) < 8) {
     $passwordistooshort = true;
	} else {
     $passwordistooshort = false;
	}
	if ($passwordistooshort == false && $passwordconfirmiswrong == false && $usernamespecialchar == false) {
			$password = password_hash($password, PASSWORD_BCRYPT);
			$query = $con->prepare('SELECT COUNT(*) FROM users WHERE username=:username');
			$query->bindParam(':username', $username);
            $query->execute();
			$result = $query->fetchColumn();
			if ($result == 0) {
			$query = $con->prepare('INSERT INTO users (username, password) VALUES (:username, :password)');
			$query->bindParam(':username', $username);
			$query->bindParam(':password', $password);
            $query->execute();
			$_SESSION["user"] = $user->getID($con, $username);	
			header("Location: /home");
  	        } else {
              $userexists = true;
  	        }

	}
}
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<?php
$result = file_get_contents("https://useless-facts.sameerkumar.website/api");
$thefunfact = json_decode($result);
?>
</head>
<title><?php echo $pagename; ?> | <?php echo $sitename; ?></title>
<body>
 <div class="container position-absolute top-50 start-50 translate-middle">
  <div class="row align-items-center">
   <div class="col-lg-6 mb-4">
    <div class="card shadow-sm mb-3 d-flex mx-auto">
     <h3 class="card-header">Random useless fact generator</h3>
     <div class="card-body">
      <p class="card-text"> <?php echo $thefunfact->{"data"}; ?> </p>
     </div>
    </div>
   </div>
   <div class="col-lg-6 mb-4">
    <form action="" method="post">
     <div class="card shadow-sm mb-3 d-flex mx-auto">
      <h3 class="card-header">Registration</h3>
      <div class="card-body">
       <label class="col-form-label col-form-label-sm mt-4 username " for="inputSmall">Username</label>
       <input class="<?php if($userexists == true || $usernamespecialchar == true) { echo "form-control is-invalid"; } else { echo "form-control form-control-sm"; }	?>" type="text" placeholder="Your username" id="inputSmall" for="inputValid" name="username"> <?php if($userexists == true) { echo '<div class="invalid-feedback" bis_skin_checked="1">Username is already taken.</div>'; } elseif($usernamespecialchar == true) { echo '<div class="invalid-feedback" bis_skin_checked="1">Special characters are not allowed.</div>'; } ?> 
        <label class="col-form-label col-form-label-sm mt-4 password " for="inputSmall">Password</label>
       <input class="<?php if($passwordconfirmiswrong == true) { echo "form-control is-invalid"; } else { echo "form-control form-control-sm"; }	?>" type="password" placeholder="Your password (Minmum 8 characters)" id="inputSmall" name="pass"> <?php if($passwordconfirmiswrong == true) { echo '<div class="invalid-feedback" bis_skin_checked="1">Password Confirmation field is invalid.</div>'; } else if ($passwordistooshort == true) { echo '<div class="invalid-feedback" bis_skin_checked="1">Password is too short. (8 Characters minmum)</div>'; } ?> <label class="col-form-label col-form-label-sm mt-4 passwordconfirm" for="inputSmall">Password Confirmation</label>
       <input class="form-control form-control-sm" type="password" placeholder="Enter your password again" id="inputSmall" name="passconfirm">
       <button type="submit" class="btn btn-secondary " style="margin-top: 15px; float: right;" name="submit">Register</button>
      <br>
     <br>
    </div>
   </div>
  </form>
</body>
</html>
