import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { PcdUserDto } from 'src/app/_dto/pcduser.dto';
import { apiUrl } from 'src/app/_providers/_resources/api-url.properties';
import { ErrorTranslateService } from 'src/app/_providers/_services/error-translate.service';
import { MasterService } from 'src/app/_providers/_services/master.service';
import { UiService } from 'src/app/_providers/_services/ui.service';
import Swal from 'sweetalert2'
@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})


export class RegisterComponent implements OnInit {


  loading = true

  verifiedOtp: boolean = false;

  dialogRef: any;

  userDetails = new PcdUserDto()

  constructor(
    private dialog: MatDialog,
    private _masterService: MasterService,
    private _uiService: UiService,
    private errorTranslateService: ErrorTranslateService,
    private router: Router
  ) { }

  ngOnInit(): void {
    this.loading = false
  }

  sendOTP(template) {
    
    if (this.userDetails.email && this._uiService.emailValidation(this.userDetails.email)) {
      let body = { email: this.userDetails.email };
      this._masterService.saveAllMasterData(apiUrl.registerOtp, body).then(res => {
      this._uiService.toastMessage(this.errorTranslateService.translate("otpSendSuccessfully"));

        this.openDialogbox(template)
      },err=>{
        this._uiService.toastMessage((err.error.message) ? err.error.message : this.errorTranslateService.translate('errorInApi'));

      })
    }
    else {
      this._uiService.toastMessage(this.errorTranslateService.translate("enterAValidEmailID"));
    }
  }

  openDialogbox(templateRef) {
    // console.log('this');
    this.dialogRef = this.dialog.open(templateRef, {
      width: '400px',
      disableClose: false
    });
    // this.verifiedOtp = true
  }


  validation() {

    if (this.userDetails.otp) {
      let body = {
        email: this.userDetails.email,
        otp: this.userDetails.otp
      }
      this._masterService.saveAllMasterData(apiUrl.validateRegisterOtp, body).then(res => {
        if (res['data'] == 'success') {
          Swal.fire(
            'Verified',
            'OTP Verified Successfully',
            'success'
          )
          this.dialogRef.close()
          this._uiService.toastMessage(this.errorTranslateService.translate("otpVerifiedSuccess"));
          this.verifiedOtp = true
        }
        else {
          this.dialogRef.close();
          this._uiService.toastMessage(this.errorTranslateService.translate("otpMismatching"));
        }
      },err=>{

        this._uiService.toastMessage((err.error.message) ? err.error.message : this.errorTranslateService.translate('errorInApi'));

      })
    }
    else {
      this._uiService.toastMessage(this.errorTranslateService.translate("enterOTP"));
    }

  }
  navigateTo(destination) {
    this.router.navigate([destination]);
  }

  preSave() {
    if (!this.userDetails.userName || this.userDetails.userName == null || this.userDetails.userName == "" ||this.userDetails.userName == undefined) {
      this._uiService.toastMessage(this.errorTranslateService.translate('userNameCantbeBlank'), 3000);
      return false;
    }
    if (!this.userDetails.mobileNumber || this.userDetails.mobileNumber == null || this.userDetails.mobileNumber == "" ||this.userDetails.mobileNumber == undefined) {
      this._uiService.toastMessage(this.errorTranslateService.translate('phonecantBeBlank'), 3000);
      return false;
    }
    if (!this._uiService.phoneNoValidation(this.userDetails.mobileNumber) ) {
      this._uiService.toastMessage(this.errorTranslateService.translate('enterAValidPhoneNumber'), 3000);
      return false;
    }
    if (!this.userDetails.pass || this.userDetails.pass == null || this.userDetails.pass == "" ||this.userDetails.pass == undefined) {
      this._uiService.toastMessage(this.errorTranslateService.translate('passwordCantbeBlank'), 3000);
      return false;
    }
    if (this.userDetails.pass.length < 6) {
      this._uiService.toastMessage(this.errorTranslateService.translate('minimumPasswordLenght'), 3000);
      return false;
    }
    if (!this.userDetails.confPass || this.userDetails.confPass == null || this.userDetails.confPass == "" ||this.userDetails.confPass == undefined) {
      this._uiService.toastMessage(this.errorTranslateService.translate('confirmPasswordCantbeBlank'), 3000);
      return false;
    }
    if (this.userDetails.pass != this.userDetails.confPass) {
      this._uiService.toastMessage(this.errorTranslateService.translate('passwordMismatch'), 3000);
      return false;
    }
    return true;
  }

  saveData(){
    if (!this.preSave())
      return;

      delete this.userDetails.confPass
      this.loading = true;
      this._masterService.saveMasterData(apiUrl.signup,this.userDetails).then(res=>{
        this.loading = false
      this._uiService.toastMessage(this.errorTranslateService.translate('saveSuccess'), 3000);
      this.verifiedOtp = false
        this.userDetails = new PcdUserDto();
        this.navigateTo('/auth/login')
      },err=>{
        this.loading = false;
        this._uiService.toastMessage((err.error.message) ? err.error.message : this.errorTranslateService.translate('errorInApi'));
        this.verifiedOtp = false
      })
  }


}
