import { CommonModule } from '@angular/common';
import { Component, OnInit, Input, inject } from '@angular/core';
import { NgbActiveModal, NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { FormGroup, FormBuilder, Validators, FormsModule, ReactiveFormsModule } from '@angular/forms';

import { EqualValidator } from '../customValidators';
import { UserService } from '../../providers/user/user.service.';
import { FunctionService } from '../components/models/function.service';

@Component({
  standalone: true,
  selector: 'login-change-pwd',
  templateUrl: './changepwd.component.html',
  imports: [FormsModule, ReactiveFormsModule, CommonModule, NgbModule]
})
export class ChangePwdComponent implements OnInit
{
  private readonly service = inject(UserService);
  private readonly fnSvc = inject(FunctionService);
  private readonly formBuilder = inject(FormBuilder);
  private readonly activeModal = inject(NgbActiveModal);

  @Input() fromParent;

  public submitted = false;
  public userId: number = 0;
  public changePwdForm: FormGroup;
  public fieldTextType1: boolean = false;
  public fieldTextType2: boolean = false;
  public fieldTextType3: boolean = false;
  public errorMessage: string = undefined;

  ngOnInit()
  {
    this.errorMessage = undefined;
    this.userId = this.fromParent.userId;
    this.changePwdForm = this.formBuilder.group(
      {
        loginId: this.fromParent.loginId,
        userName: this.fromParent.userName,
        oldPassword: ['', Validators.required],
        password: ['', Validators.required],
        confirmPassword: ['', Validators.required],
      },
      {
        validators: EqualValidator('password', 'confirmPassword')
      }
    );
  }

  public clearMessage(): void
  {
    this.errorMessage = undefined;
  }

  public toggleFieldType(controlId): void
  {
    if (controlId === 1)
      this.fieldTextType1 = !this.fieldTextType1;
    else if (controlId === 2)
      this.fieldTextType2 = !this.fieldTextType2;
    else if (controlId === 3)
      this.fieldTextType3 = !this.fieldTextType3;
  }

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

  public changeNow(): void
  {
    this.submitted = true;
    this.errorMessage = undefined;
    if (this.changePwdForm.invalid)
    {
      return;
    }

    const oldPwd = this.fnSvc.cipherData(this.f.oldPassword.value);
    const newPwd = this.fnSvc.cipherData(this.f.password.value);
    const cnfPwd = this.fnSvc.cipherData(this.f.confirmPassword.value);
    this.service.changePassword(this.userId, oldPwd, newPwd, cnfPwd).subscribe(
      {
        next: (resp: any) =>
        {
          if (resp.returnStatus === 200 && resp.value)
          {
            this.service.authService.logout().subscribe(
              {
                next: () =>
                {
                  this.service.authService.loggedout();
                  this.activeModal.close(true);
                },
                error: () =>
                {
                  this.service.authService.loggedout();
                  this.activeModal.close(true);
                }
              });
          }
          else
          {
            this.errorMessage = resp.returnMessage;
          }
        },
        error: (errMessage: string) =>
        {
          this.errorMessage = errMessage;
        }
      });
  }

  public close(): void
  {
    this.activeModal.close(false);
  }
}
