import { Component, OnInit, ViewChild, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { AuthenticationService } from 'src/app/hmg-common/services/authentication/authentication.service';
import { Router } from '@angular/router';
import { AlertController } from '@ionic/angular';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { CheckUserAuthenticationResponse } from 'src/app/hmg-common/services/authentication/models/check-user-auth.response';
import { SmsReaderService } from 'src/app/hmg-common/services/sms/sms-reader.service';
import { ForgetPassword } from '../models/forget.password';
import { LoginModel } from '../models/LoginModel';
import { SharedDataService } from 'src/app/hmg-common/services/shared-data-service/shared-data.service';
import {LoginComponent} from "src/app/authentication/login/login.component";
@Component({
    selector: 'app-forgot',
    templateUrl: './forgot.component.html',
    styleUrls: ['./forgot.component.scss']
})
export class ForgotComponent implements OnInit {

    private loginData = new LoginModel();
    public P_NEW_PASSWORD: string;
    public P_Confirm_NEW_PASSWORD : string;
    public P_USER_NAME : string;
    constructor(
        public cs: CommonService,
        public authService: AuthenticationService,
        public router: Router,
        public alertController: AlertController,
        public ts: TranslatorService,
        public smsService: SmsReaderService,
        public changeDetector: ChangeDetectorRef,
        public sharedData: SharedDataService
    ) {
    }

    ngOnInit() {
    }

    public onForgot() {
        this.sendSMSForForgotPassword();
    }

    private checkUserResult: CheckUserAuthenticationResponse;

    private sendSMSForForgotPassword() {
        const request = new ForgetPassword();
        this.authService.sendForgetPassword(
            request,
            () => {
                this.sendSMSForForgotPassword();
            }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => {
                if (this.cs.validResponse(result)) {
                    this.checkUserResult = result;
                }
            });
    }

    public forgotpassword(){
        const data = this.sharedData.getSharedData(AuthenticationService.LOGIN_DATA, false);
        this.loginData.P_USER_NAME = this.P_USER_NAME;
        let request:ForgetPassword = new ForgetPassword();
        request.P_Confirm_NEW_PASSWORD=this.P_NEW_PASSWORD;
        request.P_NEW_PASSWORD=this.P_Confirm_NEW_PASSWORD;
        request.P_USER_NAME = data.P_USER_NAME;
    
        this.authService.submitForgetPassword(
            request,
            () => {
                //this.sendSMSForForgotPassword();
            }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => {
                if (this.cs.validResponse(result)) {
                    this.checkUserResult = result;
                    console.log(result);
                    this.cs.toastPK("changePassword","successChange");
                    this.cs.openLogin();
                }
            });
        
    }
}
