import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { MainHeaderService } from '../main-header/service/main-header.service';
import { ToastrService } from 'ngx-toastr';
import { Location } from '@angular/common';
import { debounceTime, lastValueFrom, Subject } from 'rxjs';
import { UserService } from '../user/service/user.service';
import { error } from 'jquery';

@Component({
    selector: 'app-change-password',
    templateUrl: './change-password.component.html',
    styleUrls: ['./change-password.component.css'],
    standalone: false
})
export class ChangePasswordComponent implements OnInit{
  user = {
    userName: '',
    userId: '',
    encryptedPassword: '',
    mobileNumber: '',
    confirmPassword: '',
    newPassword:'',
    role:'',
    organisationId: 0,
    oldPassword:''
  
  }
  value: boolean;
  constructor(private router:Router,
    private passwordService:MainHeaderService,
    private toastr: ToastrService,
  private location: Location,
    private userService:UserService){
      this.passwordInputSubject.pipe(debounceTime(300)).subscribe(password => {
        this.validateOldPassword(password);
      });
    }
  ngOnInit(): void {
     if((localStorage.getItem("jwtToken")==null||localStorage.getItem("jwtToken")=="") &&
     (localStorage.getItem("tempTok")==null || localStorage.getItem("tempTok")=="")){
      this.navigateToSessionTimeout();
    }
  }
  isPasswordVisible: boolean = false;
  isNewPasswordVisible:boolean=false;
  isConfirmPasswordVisible: boolean = false;
  passwordErrorMessage: string = '';
  passwordNameInput: boolean = true;
  // newpasswordChange() {
  //   const pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@!#$%^&*])[A-Za-z\d@!#$%^&*]{8,}/;
  //   if (!this.user.newPassword) {
  //     this.passwordNameInput = false;
  //     this.passwordErrorMessage = 'Password should not be empty';
  //   } else if (!pattern.test(this.user.newPassword)) {
  //     this.passwordNameInput = false;
  //     this.passwordErrorMessage = 'Password should not be empty';
  //   } else {
  //     this.passwordNameInput = true;
  //     this.passwordErrorMessage = '';
  //   }
  // }
  CurrentpasswordErrorMessage: string = '';
  CurrentpasswordNameInput: boolean = true;
  CurrentpasswordChange() {
    const pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@!#$%^&*])[A-Za-z\d@!#$%^&*]{8,}/;
    if (!this.user.oldPassword) {
      this.CurrentpasswordNameInput = false;
      this.CurrentpasswordErrorMessage = 'Password should not be empty';
    } else if (!pattern.test(this.user.oldPassword)) {
      this.CurrentpasswordNameInput = false;
      this.CurrentpasswordErrorMessage = 'Password should not be empty';
    } else {
      this.CurrentpasswordNameInput = true;
      this.CurrentpasswordErrorMessage = '';
    }
  }

  confirmPasswordErrorMessage: string = '';
  confirmPasswordInput: boolean = true;
 
  newpasswordChange() {
    // Remove spaces from the password input
    this.user.newPassword = this.user.newPassword.replace(/\s+/g, '');
  
    // Regex pattern for strong password
    const pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@!#$%^&*])[A-Za-z\d@!#$%^&*]{8,}/;
  
    // Check if the password is empty
    if (!this.user.newPassword) {
      this.passwordNameInput = false;
      this.passwordErrorMessage = 'Password should not be empty';
    }
    // Check for spaces in the password
    else if (/\s/.test(this.user.newPassword)) {
      this.passwordNameInput = false;
      this.passwordErrorMessage = 'Password cannot contain spaces';
    }
    // Check for password complexity (uppercase, lowercase, digit, special char)
    else if (!pattern.test(this.user.newPassword)) {
      this.passwordNameInput = false;
      this.passwordErrorMessage = 'Password must be at least 8 characters long, include at least one uppercase letter, one lowercase letter, one digit, and one special character';
    } else {
      this.passwordNameInput = true;
      this.passwordErrorMessage = '';
    }
    // Check for matching password if both are entered
    this.checkPasswordsMatch();
  }
 
  
  confirmPasswordChange() {
    // Remove spaces from the confirm password input
    this.user.confirmPassword = this.user.confirmPassword.replace(/\s+/g, '');
  
    // Check if confirm password is empty
    if (!this.user.confirmPassword) {
      this.confirmPasswordInput = false;
      this.confirmPasswordErrorMessage = 'Confirm password should not be empty';
    } 
    // Check for spaces in the confirm password
    else if (/\s/.test(this.user.confirmPassword)) {
      this.confirmPasswordInput = false;
      this.confirmPasswordErrorMessage = 'Confirm password cannot contain spaces';
    } else {
      this.confirmPasswordInput = true;
      this.confirmPasswordErrorMessage = '';
    }
  
    // Check if the passwords match
    this.checkPasswordsMatch();
  }
  newPasswordErrorMessage:string='';
  checkPasswordsMatch() {
    // Check if newPassword and confirmPassword match
    if (this.user.newPassword && this.user.confirmPassword && this.user.newPassword !== this.user.confirmPassword) {
        this.confirmPasswordInput = false;
        this.confirmPasswordErrorMessage = 'Passwords do not match';
    } else if (this.user.newPassword && this.user.confirmPassword && this.user.newPassword === this.user.confirmPassword) {
        this.confirmPasswordInput = true;
        this.confirmPasswordErrorMessage = ''; // Clear error if they match
    }
 
    // Check if newPassword and oldPassword are the same
    if (this.user.newPassword && this.user.oldPassword && this.user.newPassword === this.user.oldPassword) {
        this.newPasswordErrorMessage = 'New password cannot be the same as the old password';
    } else if (this.user.newPassword && this.user.oldPassword && this.user.newPassword !== this.user.oldPassword) {
        // Clear the error if they are not the same
        this.newPasswordErrorMessage = '';
    }
}

  togglePasswordVisibility() {
    this.isPasswordVisible = !this.isPasswordVisible;
    //const passwordField = document.getElementById('oldPassword') as HTMLInputElement;
   // passwordField.type = this.isPasswordVisible ? 'text' : 'password';
  }
  toggleNewPasswordVisibility() {
    this.isNewPasswordVisible = !this.isNewPasswordVisible;
    // const passwordField = document.getElementById('newPassword') as HTMLInputElement;
    // passwordField.type = this.isNewPasswordVisible ? 'text' : 'password';
  }
  toggleConfirmPasswordVisibility() {
    this.isConfirmPasswordVisible = !this.isConfirmPasswordVisible;
    // const confirmPasswordField = document.getElementById('confirmPassword') as HTMLInputElement;
    // confirmPasswordField.type = this.isConfirmPasswordVisible ? 'text' : 'password';
  }
   validateEmail(email: string): boolean {
    const emailPattern = /^[a-zA-Z][a-zA-Z0-9.]*@[a-zA-Z0-9.]+\.[a-zA-Z]{2,4}$/
    return emailPattern.test(email);
  }

  validatePassword(password: string): boolean {
    const passwordPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
    return passwordPattern.test(password);
  }
  successMessage: string | null = null;
  errorMessage: string | null = null;

email:string='';
passwordUpdated: boolean = false;

  changePasswordButton(): void {
  //console.log(this.user);
  //console.log(this.user.oldPassword)
  if (this.email || !this.user.newPassword || !this.user.confirmPassword || !this.user.oldPassword) {
    this.toastr.error('All fields are required.', 'Error');
    return;
  }
  if (!this.isOldPasswordCorrect) {
    this.toastr.error('Old password is incorrect.', 'Error');
    return;
  }
  if (!this.validatePassword(this.user.newPassword)) {
    this.errorMessage = 'Password must be at least 8 characters long and include uppercase, lowercase, numbers, and special characters.';
    return;
  }

  if (this.user.newPassword !== this.user.confirmPassword) {
    this.errorMessage = 'Passwords do not match.';
    return;
  }
  if (this.user.newPassword === this.user.oldPassword) {
    this.toastr.error('New password cannot be the same as the old password.', 'Error');
    return;
  }
 
  this.updateNewPassword();
}

  // Declare error message property
  oldPasswordErrorMessage: string = '';
  oldPasswordSuccessMessage: string = '';
  newPasswordSuccessMessage:string='';
  isOldPasswordCorrect:boolean=false;
  isNewPasswordCorrect:boolean=false;

  private passwordInputSubject = new Subject<string>();
  checkOldPassword() {
    this.passwordInputSubject.next(this.user.oldPassword);
  }
  
  validateOldPassword(password: string) {
    const email = localStorage.getItem('email');
    //console.log(email);
  
    // Clear previous messages
    this.oldPasswordErrorMessage = '';
    this.oldPasswordSuccessMessage = '';
  
    // Check if the old password is provided and if the email is available
    if (!email || !password) {
      //console.error('User ID or Old Password is missing.');
      this.oldPasswordErrorMessage = 'Please provide the old password.';
      return;
    }
    const role=localStorage.getItem("userRole");
    // Call the service to validate the old password
    if(password.length>=8){
    
      if(role!="IDRBTADMIN"){
        this.passwordService.validateOldPassword(email, password).subscribe({
          next: (isValid: string) => {
            if (isValid) {
              this.isOldPasswordCorrect = true;
              this.oldPasswordSuccessMessage = 'Old password is correct.';
              this.oldPasswordSuccessMessage=''
            } else {
              this.isOldPasswordCorrect = false;
              this.oldPasswordErrorMessage = 'Old password is incorrect.';
              this.oldPasswordSuccessMessage=''
            }
          },
          error: (error) => {
            //console.error('Error occurred:', error);
            this.isOldPasswordCorrect = false;
            this.oldPasswordErrorMessage = 'Old password is incorrect.';
            this.oldPasswordSuccessMessage=''
          }
        });
      }else{
      this.passwordService.validateRgtrOldPassword(email, password).subscribe({
        next: (isValid: string) => {
          if (isValid) {
            this.isOldPasswordCorrect = true;
            this.oldPasswordSuccessMessage = 'Old password is correct.';
            this.oldPasswordSuccessMessage=''
          } else {
            this.isOldPasswordCorrect = false;
            this.oldPasswordErrorMessage = 'Old password is incorrect.';
            this.oldPasswordSuccessMessage=''
          }
        },
        error: (error) => {
          //console.error('Error occurred:', error);
          this.isOldPasswordCorrect = false;
          this.oldPasswordErrorMessage = 'Old password is incorrect.';
          this.oldPasswordSuccessMessage=''
        }
      });
      }
      
    
    }
  }

cancelButton(){
document.getElementById('clear').click();


}

forgetPasswordPage(){
  this.router.navigateByUrl('forgot-password-reset');
}

  updateNewPassword(): void {
 
  const email = localStorage.getItem('email');
  this.newPasswordErrorMessage = '';
    this.newPasswordSuccessMessage = '';
  if ( !email || !this.user.newPassword || !this.user.confirmPassword) {
    this.toastr.error('New password and confirmation are required.', 'Error');
    return;
  }
 
 const role =localStorage.getItem("userRole")
 if(role!="IDRBTADMIN"){
  this.passwordService.resetPassword(email, this.user.newPassword, this.user.confirmPassword).subscribe({
    next: (isUpdated: string) => {
      if (isUpdated) {
        this.toastr.success('Password updated successfully.', 'Success');
        this.passwordUpdated = true;
        this.clearInputFields();
        this.passwordUpdated = false;
        if(localStorage.getItem('tempTok')!=null){
          localStorage.setItem('jwtToken',localStorage.getItem('tempTok'));
          localStorage.setItem('tempTok',null);
          this.getUserDetails();
          this.router.navigateByUrl('/login');
          
        }
       
      } else {
        this.toastr.error('Failed to update password. Please try again.', 'Error');
      }
    },
    error: (error) => {
      //console.error('Error occurred:', error);
      this.toastr.error('An error occurred while resetting your password. Please try again.', 'Error');
    }
  });
 }else{
  this.passwordService.resetRegistrarPassword(email, this.user.newPassword, this.user.confirmPassword).subscribe({
    next: (isUpdated: string) => {
      if (isUpdated) {
        this.toastr.success('Password updated successfully.', 'Success');
        this.passwordUpdated = true;
        this.clearInputFields();
        this.passwordUpdated = false;
        if(localStorage.getItem('tempTok')!=null){
          localStorage.setItem('jwtToken',localStorage.getItem('tempTok'));
          localStorage.setItem('tempTok',null);
          this.getUserDetails();
          this.router.navigateByUrl('/login');
          
        }
       
      } else {
        this.toastr.error('Failed to update password. Please try again.', 'Error');
      }
    },
    error: (error) => {
      //console.error('Error occurred:', error);
      this.toastr.error('An error occurred while resetting your password. Please try again.', 'Error');
    }
  });
}
}
clearInputFields(): void {
  this.user.oldPassword = '';
  this.user.newPassword = '';
  this.user.confirmPassword = '';
  this.oldPasswordErrorMessage = '';
  this.newPasswordErrorMessage = '';
  this.confirmPasswordErrorMessage = '';
  this.oldPasswordSuccessMessage = '';
  this.newPasswordSuccessMessage = '';
}


// conformButton(){
//   setTimeout(() => {
//     this.router.navigateByUrl('mainHeader');
//   }, 100);
  
// }
loggedInUser
getUserDetails(){
 
     lastValueFrom(this.userService.getUserByEmailId(localStorage.getItem('email'))).then(
      response => {
        if(response.status === HttpStatusCode.Ok){
       this.loggedInUser=response.body;
       this.loggedInUser.initialLoginStatus=true;
        this.userService.updateUser(this.loggedInUser).subscribe({
          next:(response)=>{
            this.loggedInUser=response.body;
            localStorage.clear();
            sessionStorage.clear();
          },error:(error)=>{
            if(error.status===HttpStatusCode.ServiceUnavailable){
              this.toastr.error("Server down please try after some time");
            }
          }
        })
        }
      }, error => {
        if(error.status === HttpStatusCode.Unauthorized){
          this.navigateToSessionTimeout();
        }
      }
    )
  }
  navigateToSessionTimeout(){
    this.router.navigateByUrl("/session-timeout");
  }

goBack(){
  //console.log('executed')
  document.getElementById('clear')?.click();
 this.location.back();
 
}
goBackButton(){
  this.location.back();
}

disableEvent(event: Event){
  event.preventDefault();
}


}
