import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { environment } from 'src/environments/environment';
import { apiUrl } from '../_resources/api-url.properties';
import { ToWords } from 'to-words';
import { AuthService } from './auth.service';
import { ErrorTranslateService } from './error-translate.service';

/**************************  
 Created by Ajith A Pradeep
 Created on 21/03/2022
 **************************/

@Injectable({
  providedIn: 'root'
})
export class UiService {

  isCapitalized: boolean = true;

  toWords = new ToWords({
    localeCode: 'en-IN',
    converterOptions: {
      currency: true,
      ignoreDecimal: false,
      ignoreZeroCurrency: false,
      doNotAddOnly: false,
    }
  });

  numberToWords = new ToWords();

  constructor(
    public _snackBar: MatSnackBar,
    private http: HttpClient,
    private authService: AuthService,
    private _errorTranslateService: ErrorTranslateService
  ) {

  }

  parseDate(dateString: string): Date {
    if (dateString) {
      return new Date(dateString);
    }
    return null;
  }

  toastMessage(message: string, timeDuration?: number, error?: boolean, green?: boolean, button?: string) {
    let defaultButton = "Close";
    let defaultTime = 3000;
    if (button && button != null)
      defaultButton = button;
    if (timeDuration && timeDuration != null)
      defaultTime = timeDuration
    if (error)
      this._snackBar.open(message, defaultButton, { duration: defaultTime, panelClass: ['mat-toolbar', 'mat-warn'] });
   
    else if (error && green)
    this._snackBar.open(message, defaultButton, { duration: defaultTime, panelClass: ['mat-toolbar', 'mat-warn'] });
    else if (green && !error)
      this._snackBar.open(message, defaultButton, { duration: defaultTime, panelClass: ['custom-green-snackbar'] });
    else
      this._snackBar.open(message, defaultButton, { duration: defaultTime });

  }


  openSnackBar(message: string, action: string) {
    this._snackBar.open(message, action);
  }

  admissionNoValidation(number) {
    let regex = new RegExp(/^\d{4}$/i)
    return regex.test(number)
  }

  phoneNoValidation(number) {
    let regex = new RegExp(/^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[6789]\d{9}$/);
    return regex.test(number)
  }

  emailValidation(email) {
    let regex = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
    return regex.test(email)
  }

  alphabetsOnlyValidation(stringValue) {
    let regex = new RegExp(/^[a-zA-Z ]*$/);
    return regex.test(stringValue);
  }

  async getUserInfo(body): Promise<any> {
    return await this.http.post<any>(`${environment.apiUrl}/${apiUrl.userInfo}`, body).toPromise();
  }

  getInstitution() {
    let institution = {
      name: '',
      logo: '',
      fullName: ''
    }

    institution.name = localStorage.getItem('institute');
    if (institution.name) {
      switch (institution.name) {
        case 'salesapp':
          institution.logo = "./../../../../../assets/accurate/images/salesapp.jpg";
          break;
        case 'dav':
          institution.logo = "./../../../../../assets/accurate/images/dav.png";
          break;
        default:
          institution.logo = './../../../../../assets/accurate/images/institute.png';
          break;
      }
    } else {
      institution.logo = './../../../../../assets/accurate/images/institute.png';
    }

    return institution;
  }

  async userValidation() {
    // await this.getUserInfo().then((res) => {

    // }, (err) => {
    //   console.log("err==", err);
    //   this.toastMessage((err.error.message) ? err.error.message : this._errorTranslateService.translate('errorInApi'));
    // });
  }

  async capitalizeInputs() {

    if (this.isCapitalized) {
      let inputs = document.querySelectorAll(".capitalise");
      inputs.forEach(item => {
        item.addEventListener("input", this.capitalize, true);
      });
    }
  }

  capitalize(e) {
    e.target.value = e.target.value.toUpperCase();
  }


}