import { Injectable, Inject , Output} from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable, of } from 'rxjs';
import { ConfigureUrl,Lookup,LookupEdit,ResultModel
   } from '../models';
import { Utils,AppSettingService } from '../shares';


@Injectable({ providedIn: 'root' })
export class LookupService {
  msg: string = "[lookupService] ";  
    _lookupByStatus:any;
    _staffList:any;
    _clientList:any;
    _jobList:any;
  constructor(private http: HttpClient,   
    private appSetting :AppSettingService
  ) {
      // this default when load look for previous login information
        // if no need then comment out downthere  
    this.setAllNull();
  }
  setAllNull() :void {
    // this._lhdList = undefined;
    this._lookupByStatus = {};
    this._staffList = null;
    this._clientList = null;
    this._jobList = null;
  }
  loadLookupEditBystatus(type:string): Observable<ResultModel<LookupEdit[]>> {			
  	const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/LoadLookupEdit";
    const params = new HttpParams().append('type', type);
    console.log(this.msg + "go server this loadLookupEditBystatus is in cache ", type);
    return this.http.get<ResultModel<LookupEdit[]>>(baseUrl, {params});
}
loadLookupById(id:number, type:string): Observable<ResultModel<LookupEdit>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl;
     const params = new HttpParams()
                    .append('id', id)
                    .append('type', type);
		return this.http.get<ResultModel<LookupEdit>>(baseUrl,{params});
	}
	saveLookup(item:LookupEdit): Observable<ResultModel<number>> { //insert Lookup
    this.setAllNull();
		let config = { headers : { 'Content-Type': 'application/json' } };
		const baseUrl = this.appSetting.appSetting.baseUrl  + "/"+ ConfigureUrl.lookupUrl;
		return this.http.post<ResultModel<number>>(baseUrl, item, config);
	}
	deleteLookup(id:number): Observable<ResultModel<number>>{
		const baseUrl = this.appSetting.appSetting.baseUrl  + "/"+ ConfigureUrl.lookupUrl;
		return this.http.delete<ResultModel<number>>(baseUrl + "/" + id );
	}

  loadLookupBystatus(type:string): Observable<ResultModel<Lookup[]>> {			
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/LoadJobs";
    console.log(this.msg + "loadLookupBystatus", this._lookupByStatus);
		if (this._lookupByStatus[type])
    {
      console.log(this.msg + "Not go server this LoadJobs is in cache ",type);
      return of(this._lookupByStatus[type]);
    }
    else
    {
       const params = new HttpParams().append('type', type);
      console.log(this.msg + "go server this loadLookupBystatus is in cache ", type);
    return this.http.get<ResultModel<Lookup[]>>(baseUrl, {params})
    .pipe(map(resultModel => {
      this._lookupByStatus[type] = resultModel;
      return resultModel;
     })      
    ); 
    }   
	}
  loadStaffs(): Observable<ResultModel<Lookup[]>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/GetStaffs";
   /*  const params = new HttpParams()
                    .append('id', id)
                    .append('type', type);
		return this.http.get<ResultModel<LookupEdit>>(baseUrl,{params});
    */
   if (this._staffList != null)
   {
    return of(this._staffList);
   }
   else
   {
    return this.http.get<ResultModel<Lookup[]>>(baseUrl)
    .pipe(map( x => {
      this._staffList = x;
      return x;
    }));
   }
	}
  loadClients(): Observable<ResultModel<Lookup[]>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/GetClients";
   /*  const params = new HttpParams()
                    .append('id', id)
                    .append('type', type);
		return this.http.get<ResultModel<LookupEdit>>(baseUrl,{params});
    */
   if (this._clientList != null)
   {
      return of(this._clientList);
   }
   else
   {
      return this.http.get<ResultModel<Lookup[]>>(baseUrl)
      .pipe(map(resultModel => {
      this._clientList = resultModel;
      return resultModel;
     })
      );   
   }
	}
  loadJobs(): Observable<ResultModel<Lookup[]>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/GetJobs";
   /*  const params = new HttpParams()
                    .append('id', id)
                    .append('type', type);
		return this.http.get<ResultModel<LookupEdit>>(baseUrl,{params});
    */
   if (this._jobList != null)
   {
    return of(this._jobList);
   }
   else 
   {
    return this.http.get<ResultModel<Lookup[]>>(baseUrl)
    .pipe(map(x => {
      this._jobList = x;
      return x;
    }))
   }
	}
}
