import { Component, Input } from '@angular/core';
import { LocalStorageService } from 'angular-2-local-storage';
import { Router } from '@angular/router';
import { environment } from '../../environments/environment.dev';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { AuthService } from '../services/auth.service';
@Component({
    selector: 'forgot-password',
    templateUrl: './forgot-password.component.html',
    styleUrls: ['./forgot-password.component.css'],
})

// exports the class here..!
export class ForgotPasswordComponent {

    @Input() email: string = '';
    @Input() password: string = '';
    @Input() confirm_password: string = '';
    imgUrl = environment.imageUrl;
    error: any;
    isEmailError = false;
    isError = false;
    isSuccess = false;
    showPasswordFields = false;
    emailFormControl = new FormControl('', [
        Validators.required,
        Validators.email,
    ]);
    successMessage = '';
    emailValidationFormGroup;
    updatePasswordFormGroup;
    hide = true;
    isConfirmPwdError = false;
    comPwdErrMsg = '';
    pwdLengthErrorMsg = 'Password is minimum 6 characters';
    otpLengthErrorMsg = 'OTP has 8 characters';
    constructor(private AuthService: AuthService, private router: Router) {
        this.emailValidationFormGroup = new FormGroup({
            email: new FormControl('', [Validators.required, Validators.email]),
        })
        this.updatePasswordFormGroup = new FormGroup({
            otp: new FormControl('', [Validators.required, Validators.minLength(8)]),
            password: new FormControl('', [Validators.required, Validators.minLength(6)]),
            confirm_password: new FormControl('', [Validators.required, Validators.minLength(6)]),
        })
    }

    getVfCode() {
            this.email = this.emailValidationFormGroup.value.email;
            this.AuthService.getEmailVerification(this.emailValidationFormGroup.value.email).then(resp => {
                console.log("email verify resp -- ", resp);
                if (resp.error == null) {
                    this.showPasswordFields = true;
                    this.isSuccess = true;
                    this.successMessage = resp.message;
                    setTimeout(() => {
                        this.isError = false;
                        this.isSuccess = false;
                    }, 3000);
                }
                else {
                    this.isError = true;
                    this.error = resp.errorMessage;
                }
            })
   }

    updatePassword() {
        // if (this.confirm_password !== this.password) {
        //   this.isConfirmPwdError = true;
        //   this.comPwdErrMsg = "Password mismatched.";
        // }
        // if (this.confirm_password.length <= 5) {
        //   this.isConfirmPwdError = true;
        //   this.comPwdErrMsg = "Confirm Password is minimum 6 characters.";
        // }
        // if (this.confirm_password == null || this.confirm_password == '' || this.confirm_password == undefined) {
        //   this.isConfirmPwdError = true;
        //   this.comPwdErrMsg = "Please enter a valid confirm password .";
        // }
        // if (this.password.length <= 5) {
        //   this.isPwdError = true;
        //   this.PwdErrMsg = "Password is minimum 6 characters.";
        // }
        // if (this.password == null || this.password == '' || this.password == undefined) {
        //   this.isPwdError = true;
        //   this.PwdErrMsg = "Please enter a valid password.";
        // }
        // , verificationCode: this.verificationCode
        this.AuthService.updateUserPassword({ userName: this.email, password: this.updatePasswordFormGroup.value.password, verificationCode: this.updatePasswordFormGroup.value.otp }).then(resp => {
            console.log("password update resp -- ", resp);
            if (resp.error == null) {
                this.password = '';
                this.confirm_password = '';
                this.isError = false;
                // this.isLoading = false;
                this.isSuccess = true
                this.successMessage = "Password updated Successfully"
                setTimeout(() => {
                    this.router.navigate(['']);
                }, 3000);
            }
            else {
                this.isError = true;
                this.error = resp.error.message;
            }
        })
    }

    validateConfirmPassword(){
        if(this.updatePasswordFormGroup.value.password !== this.updatePasswordFormGroup.value.confirm_password) {
            console.log('password does not match');
            this.isConfirmPwdError = true;
            this.comPwdErrMsg = "Password mismatched.";
          }else {
            this.isConfirmPwdError = false;
            this.comPwdErrMsg = "";
          }
    }

    goBackToLogin() {
        this.router.navigate(['']);
    }
}