<?php

class Member extends Controller {

	public function Index(IUserSettingsRepository $userSettingsRepo, $permission_error, $submit_form, $email, $password, $confirm_password) {
		if (Session::IsUserLoggedIn())
			return $this->Manage($userSettingsRepo);
		else
			return $this->Login($permission_error, $submit_form, $email, $password);
	}

	public function Login($permission_error, $submit_form, $email, $password) {
		if (Session::IsUserLoggedIn()) {
			header("location:/member/manage");
			return;
		}

		if (!isset($email))
			$email="";

		$errors=array();

		if (isset($permission_error)&&$permission_error==true)
			$errors[]="You don't have permission to access this page.";

		if (isset($submit_form)&&$email!="") {
			$user=new User($email);
			if ($user->UserId!=null&&$user->ValidatePassword($password)) {
				Session::SetLoggedInUser($user);
				header("location:/member/manage");
				return;
			}

			$errors[]="Unable to log in. Please check your login details and try again.";
		}

		return new View("Member/login.view", array(
			"errors"=>$errors,
			"loginEmail"=>$email
		));
	}

	public function Logout() {
		Session::Destroy();
		header("location:/member");
	}

	public function Register($email, $password) {
		if (!isset($email, $password))
			return $this->Login(null, null, $email, $password);

		$errors=array();
		if ($email=="")
			$errors[]="Email blank";
		if ($password=="")
			$errors[]="Password blank";

		if (!Utils::IsValidEmail($email))
			$errors[]="Invalid email address";

		$user=new User($email);
		if ($user->UserId!=0)
			$errors[]="Email already in use";

		if (count($errors)>0)
			return new View("Member/login.view", array(
				"errors"=>$errors,
				"registerEmail"=>$email
			));

		$user=new User();
		$user->UserEmail=$email;
		$user->UserPassword=$password;
		$user->UserCreated=time();
		$user->Save();

		Session::SetLoggedInUser($user);

		header("location:/member/manage");
	}

	public function Manage(IUserSettingsRepository $userSettingsRepo, $errors=array()) {
		if (!Session::IsUserLoggedIn()) {
			header("location:/member/");
			return;
		}

		$user=Session::GetLoggedInUser();
		$settingsToLoad=array(
			"height",
			"weight_graph_duration"
		);
		
		$settings=array();
		foreach ($settingsToLoad as $key){
			$settings[$key]=$userSettingsRepo->GetSetting($user, $key)->Value;
		}
		
		$user=Session::GetLoggedInUser();
		return new View("Member/manage.view", array("user"=>$user, "errors"=>$errors, "settings"=>$settings));
	}

	public function SavePassword($submit_form, $new_password, $confirm_password) {
		if (!Session::IsUserLoggedIn()) {
			header("location:/member/");
			return;
		}

		$user=Session::GetLoggedInUser();
		$errors=array();

		if (isset($submit_form) && $new_password!="") {
			if ($new_password==$confirm_password) {
				$user->UserPassword=$new_password;
				$user->Save();
				header("location:/member/manage");
				return;
			} else
				$errors[]="Passwords did not match";
		}

		if (count($errors)==0){
			header("location:/member/");
			return;
		}
		
		return $this->Manage($errors);
	}

	private function SaveSetting(IUserSettingsRepository $userSettingsRepo, User $user, $key, $value){
		$setting=$userSettingsRepo->GetSetting($user, $key);
		$setting->Value=$value;
		$setting->Save();
	}
	
	public function SaveSettings($submit_form, $height, $weight_graph_duration, IUserSettingsRepository $userSettingsRepo) {
		if (!isset($submit_form) || !Session::IsUserLoggedIn()) {
			header("location:/member/");
			return;
		}

		$user=Session::GetLoggedInUser();
		$errors=array();
		
		$this->SaveSetting($userSettingsRepo, $user, "height", (int)$height);
		$this->SaveSetting($userSettingsRepo, $user, "weight_graph_duration", (int)$weight_graph_duration);

		if (count($errors)==0){
			header("location:/member/");
			return;
		}

		return $this->Manage($errors);
	}

}
