import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { UserService } from '@app/services/user.service';
import Swal from 'sweetalert2';
import { ValidatorComponent } from '../plugins/validator/validator.component';



@Component({
  selector: 'app-confirm-account',
  templateUrl: './confirm-account.component.html',
  styleUrls: ['./confirm-account.component.scss']
})
export class ConfirmAccountComponent implements OnInit {
  token:string;
  validToken:boolean;
  activateForm: FormGroup;
  errorMessage: string;
  userActivated: boolean;
  successActivation: boolean;
  activateMessage: string;
  invalidToken: boolean;
  submitted: boolean = false;

  constructor(private formBuilder: FormBuilder, private route: ActivatedRoute, private userService: UserService) {
    this.route.queryParams.subscribe(params => {
      this.token = params['token'];
    });
  }

  ngOnInit() {
    if (this.token !== null){
      
      this.userService.validateUserToken(
        {
          token: this.token
        }
      ).subscribe(res => {
        
        let userData = res["data"].user;

        this.validToken = true;
        this.activateForm = this.formBuilder.group({
          // Load information
          email: [(userData.email)],
          first_name: [(userData.first_name),Validators.required],
          last_name: [(userData.last_name),Validators.required],
          password: ['',[Validators.required, Validators.minLength(6)]],
          confirm_password: ['',Validators.required]
        },{
            validator: ValidatorComponent('password', 'confirm_password')
        });
      }, (err) => {
       
        this.userActivated = true;
        this.invalidToken = true;
        this.errorMessage = err.message;
      });
    
    }
    else {
      this.invalidToken = true;
      this.errorMessage = "No existe el token";
    }
  }

  get f() { return this.activateForm.controls; }


  activateAccount(){

    this.submitted = true;

    // stop here if form is invalid
    if (this.activateForm.invalid) {
      return;
    }

    this.userService.activateUser(
      {
        email: this.f.email.value,
        first_name: this.f.first_name.value,
        last_name: this.f.last_name.value,
        password: this.f.password.value,
        confirm_password: this.f.confirm_password.value,
      }
    ).subscribe(res => {
      this.successActivation = true;
      this.validToken = false;
      this.activateMessage = "Usuario registrado con exito";

    }, (err) => {
      this.validToken = false;
      this.errorMessage = err.message;
    });

    //

  }

}


/*

Swal.fire({
      allowOutsideClick: false,
      type: 'info',
      text: 'Espere por favor...'
    });
    Swal.showLoading();

    this.authService.login(
      {
        email: this.f.email.value,
        password: this.f.password.value
      }
    )
    .subscribe(success => {
      if (success) {
        window.location.href="#/dashboard";
      }
      else {
        Swal.fire({
          type: 'error',
          title: 'No se pudo auntenticar',
          text: "Email o contraseña incorrecta"
        })  
      }
    },(err) => {
      Swal.fire({
        type: 'error',
        title: 'Error en el servidor',
        text: "No su pudo obtener la informacion"
      });
    });

    */