import { Injectable } from '@angular/core';
import { HttpRequestService } from '../http-request.service';
import { map, Observable } from 'rxjs';
import { RequestDataForm, RequestDynamicForm, RequestForm, ResponseDataGetFormById, ResponseDataGetForms, ResponseDataGetInformationDataSelect, ResponseDataGetTables } from '../../interfaces/forms-management/form-management.interface';
import { ResponseData } from '../resources.service';

@Injectable({
  providedIn: 'root'
})
export class DynamicFormService {
  private _url: string;

  constructor( private _httpRequestService: HttpRequestService ) {
    this._url = 'form';
  }

  public getForms(idUser: string, line: number): Observable<ResponseDataGetForms> {
    return this._httpRequestService.getQuery(`${this._url}/get-forms/${idUser}/${line}`).pipe(map((data:any) => data));
  }
  public getInformationDataSelect(tableName: string, columnValue: string, columnName: string, user: string, line: number): Observable<ResponseDataGetInformationDataSelect> {
    return this._httpRequestService.getQuery(`${this._url}/get-information-data-select/${tableName}/${columnValue}/${columnName}/${user}/${line}`).pipe(map((data:any) => data));
  }
  public getFormById(idForm: string, idUser: string, line: number): Observable<ResponseDataGetFormById> {
    return this._httpRequestService.getQuery(`${this._url}/get-form-by-id/${idForm}/${idUser}/${line}`).pipe(map((data:any) => data));
  }
  
  public registerForms(requestForm: RequestForm): Observable<ResponseData> {
    return this._httpRequestService.postQuery(`${this._url}/register-form`, requestForm).pipe(map((data:any) => data));
  }
  public updateForms(idForm: string, requestForm: RequestForm): Observable<ResponseData> {
    return this._httpRequestService.postQuery(`${this._url}/update-form/${idForm}`, requestForm).pipe(map((data:any) => data));
  }

  public registerDynamicForm(requestDynamicForm: RequestDynamicForm) {
    return this._httpRequestService.postQuery(`${this._url}/register-dynamic-form`, requestDynamicForm).pipe(map((data:any) => data));
  }
  public updateDynamicForm(requestDynamicForm: RequestDynamicForm) {
    return this._httpRequestService.postQuery(`${this._url}/update-dynamic-form`, requestDynamicForm).pipe(map((data:any) => data));
  }

  public getFormDataById(requestDataForm: RequestDataForm, idForm: string, user: string, line: number) {
    return this._httpRequestService.postQuery(`${this._url}/get-form-data-by-id/${idForm}/${user}/${line}`, requestDataForm).pipe(map((data:any) => data));
  }

  public getTables(idUser: string, line: number): Observable<ResponseDataGetTables> {
    return this._httpRequestService.getQuery(`${this._url}/get-tables/${idUser}/${line}`).pipe(map((data:any) => data));
  }
  public registerTable(table: any): Observable<ResponseData> {
    return this._httpRequestService.postQuery(`${this._url}/register-table`, table).pipe(map((data:any) => data));
  }
}
