import { DatePipe } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { apiUrl } from '../_resources/api-url.properties';
import { PurchaseDto } from 'src/app/_dto/purchase.dto';

/**************************  
 Created by Ajith A Pradeep
 Created on 07/05/2022
 **************************/

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

  constructor(
    private http: HttpClient,
    private _datePipe:DatePipe
    ) { }
  
    dateFormatting(dateValue) {
      let inputDate = new Date(dateValue)
      let today = new Date()
      let formattedDate
      if (inputDate < today && this._datePipe.transform(inputDate, 'dd/MM/yyyy') != this._datePipe.transform(today, 'dd/MM/yyyy')) {
        formattedDate = this._datePipe.transform(new Date(inputDate), 'yyyy/MM/dd') + " 23:59:59"
      }
      else if (inputDate > today && this._datePipe.transform(inputDate, 'dd/MM/yyyy') != this._datePipe.transform(today, 'dd/MM/yyyy')) {
        formattedDate = this._datePipe.transform(new Date(inputDate), 'yyyy/MM/dd') + " 00:00:01"
      }
      else {
        formattedDate = this._datePipe.transform(new Date(), 'yyyy/MM/dd HH:mm:ss')
      }
      return formattedDate
    }

  async getTransactionData(apiUrl, filter?: string): Promise<any> {
    if (!filter)
      filter = ''
    return await this.http.get<any>(`${environment.apiUrl}/${apiUrl}${filter}`).toPromise();
  }

  async getTransactionDatabyId(apiUrl, id): Promise<any> {
    return await this.http.get<any>(`${environment.apiUrl}/${apiUrl}/${id}`).toPromise();
  }

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

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

  async deleteTransactionData(apiUrl, id): Promise<any[]> {
    return await this.http.delete<any>(`${environment.apiUrl}/${apiUrl}/${id}`).toPromise();
  }

  // async getFormNo(prefix): Promise<any> {
  //   return await this.http.get<any>(`${environment.apiUrl}/${apiUrl.admissionFormNo}?where[prefix]=${prefix}`).toPromise();
  // }

  // async getAdmissionNo(filter): Promise<any> {
  //   return await this.http.get<any>(`${environment.apiUrl}/${apiUrl.admissionNo}${filter}`).toPromise();
  // }

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

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

  async encryptedCode(code): Promise<any> {
    return await this.http.post<any[]>(`${environment.apiUrl}/${apiUrl.cryptoEncrypt}`, { data: code }).toPromise();
  }

  async decryptedCode(code): Promise<any> {
    return await this.http.post<any[]>(`${environment.apiUrl}/${apiUrl.cryptoDecrypt}`, { data: code }).toPromise();
  }

  async sendOtp(email, FR): Promise<any> {
    return await this.http.post<any>(`${environment.apiUrl}/${"sms-api/otp"}/${FR}`, email).toPromise();
  }

  async verifyOtp(body, FR): Promise<any> {
    return await this.http.post<any>(`${environment.apiUrl}/${"sms-api/verifyOtp"}/${FR}`, body).toPromise();
  }

  async createPayment(paymentRequest: PurchaseDto): Promise<any> {
    return await this.http.post(`${environment.apiUrl}/${apiUrl.paymentGateway}`, paymentRequest).toPromise();
  }

  // async createPublicPayment(paymentRequest: PurchaseDto): Promise<any> {
  //   return await this.http.post(`${environment.apiUrl}/${apiUrl.publicPaymenGateway}`, paymentRequest).toPromise();
  // }

  async cancelPayment(id: number): Promise<any> {
    return await this.http.post(`${environment.apiUrl}/${apiUrl.feePaymentCancel}`, { id: id }).toPromise();
  }

}

