import { NotificationsComponent } from './../core/notifications/notifications.component';
import { Component } from '@angular/core';
import { User } from '../core/entities';
import { SigninService } from './signin.service';
import { Router } from '@angular/router';

@Component({
	selector: 'app-signin',
	templateUrl: './signin.component.html',
	styles: [`
		#form {
			padding-bottom: 10px;
		}

		.submitFormArea {
			line-height: 50px;
		}
	`]
})
export class SigninComponent {
	model: User = new User('', '', '', '', '', null, null, '');
	confirmPassword: string;

	constructor(
		private signinService: SigninService,
		private router: Router
	) {}

	onSubmit(): void {
		if (this.confirmPassword && this.confirmPassword === this.model.password) {
			this.signinService.signin(this.model).subscribe(() => {
				NotificationsComponent.success('Inscription réussie.');
				this.router.navigate(['/login']);
			}, error => {
				// FIXME: Type the error to get the status.
				switch (error.status) {
				case 409:
					NotificationsComponent.error('L\'adresse mail saisie n\'est pas disponible');
					break;
				default:
					NotificationsComponent.error('Une erreur est survenue lors de l\'inscription, veuillez réessayer plus tard');
					break;
				}
			});
		} else {
			NotificationsComponent.error('Les mots de passe saisis ne correspondent pas');
		}
	}
}
