import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthenticationService } from '../authentication.service';
import { HttpClientModule } from '@angular/common/http';
import { HttpModule } from '@angular/http';
import { MatSnackBar } from '@angular/material/snack-bar';
import { environment } from '../../environments/environment';
@Component({
    moduleId: module.id,
    templateUrl: 'login.component.html',
    styleUrls: ['login.component.css']
})

export class LoginComponent implements OnInit {

    public isLogin: Boolean = false;
    model: any = {};
    loading = false;
    error = '';
    private outh_token = '';
    private outh_token_secret = '';

    constructor(
        private router: Router,
        private authenticationService: AuthenticationService,
        public snackBar: MatSnackBar) { }

    ngOnInit() {
    }

    login() {
        this.loading = true;
        this.authenticationService.login(this.model.username, this.model.password).subscribe(
            result => {
                if (result === true) {
                    // login successful
                    this.isLogin = true;
                    this.router.navigate(['/my-groups']);
                    this.snackBar.open('Zalogowano pomyślnie.', null,
                        { duration: 3000, verticalPosition: 'top', panelClass: ['snackbar-success'] });
                } else {
                    // login failed
                    this.error = 'Username or password is incorrect';
                    this.loading = false;
                    this.snackBar.open('Niepoprawne hasło lub login.', null,
                        { duration: 3000, verticalPosition: 'top', panelClass: ['snackbar-error'] });
                }
            }, error => {
                this.loading = false;
                this.error = error;
                this.snackBar.open('Niepoprawne hasło lub login.', null,
                    { duration: 3000, verticalPosition: 'top', panelClass: ['snackbar-error'] });
            }
        );
    }

    loginWithUsos() {
        let array = [] as string[];
        this.authenticationService.getUSOSTokens().subscribe(el => {
            array = el.url.split('?');
            array = array[1].split('&');
            this.outh_token = array[0];
            this.outh_token = this.outh_token.replace('oauth_token=', '');
            this.outh_token_secret = array[1];
            this.outh_token_secret = this.outh_token_secret.replace('oauth_token_secret=', '');
            localStorage.setItem('outh_token', array[0].replace('oauth_token=', ''));
            localStorage.setItem('outh_token_secret', array[1].replace('oauth_token_secret=', ''));
            window.location.href = el.url;
        });
    }
}
