import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Event } from '../models/event.model';
import { from, Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { AuthService } from '../services/auth.service';
import { LoginUserRespose } from '../models/user.model';
import { EventSearch } from "src/app/models/event-search";
import { TopSearch } from 'src/app/models/top-search';

@Injectable({
  providedIn: 'root'
})
export class SearchService {
  // state
  authheader = {};
  loggeduser: LoginUserRespose;
  headers: HttpHeaders;

  categories= Array;

  // setup
  constructor(private http: HttpClient, user: AuthService) { 
    this.loggeduser = user.ValidatedUser;
    this.headers = new HttpHeaders();
    this.headers = this.headers.set('Authorization', this.loggeduser.BasicAuthKey);
  }


  basicSearch(subject:string, date:string = null, sender:string = null, organicEntity:string = null, docTypeId:string = null, applicationType:string): Observable<EventSearch>{
    // Endpoint
    const geturl = environment.apiURL + 'search';
    // store params
    let params = new HttpParams();
    // set https params
    console.log(subject);

    params = params.set("assunto", subject);
    params = params.set("data", date);
    params = params.set("remetente", sender);
    params = params.set("entidadeOrganica", organicEntity);
    params = params.set("docTypeId", docTypeId);
    params = params.set("applicationType", applicationType);


    const options = { 
      headers: this.headers, 
      params: params 
    };

    return this.http.get<EventSearch>(`${geturl}`, options);
  }

  mostSeachWord(size:string):Observable<any>{
    // Endpoint

    let UserId = (this.loggeduser.UserId).toString();

    const geturl = environment.apiURL + 'Processes/GetHitsByUserId';
    // store params
    let params = new HttpParams();
    // set https params

    params = params.set("userId", UserId);
    params = params.set("size", size);

    const options = {
      headers: this.headers, 
      params: params 
    };

    return this.http.get<any>(`${geturl}`, options);
  }

  searchForDoc(subject:string, date:string, sender:string, organicEntity:string, docTypeId:string, applicationType:string): Observable<EventSearch> {
    // Endpoint
    const geturl = environment.apiURL + 'search/attachment?';
    // store params
    let params = new HttpParams();
    // set https params
    console.log(subject);

    params = params.set("assunto", subject);
    params = params.set("data", date);
    params = params.set("remetente", sender);
    params = params.set("entidadeOrganica", organicEntity);
    params = params.set("docTypeId", docTypeId);
    params = params.set("applicationType", applicationType);


    const options = { 
      headers: this.headers, 
      params: params 
    };

    return this.http.get<EventSearch>(`${geturl}`, options);
  }

}
