
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Staff,StaffSearch, StaffView, ResultModel,ConfigureUrl,ResetPassword, Person, PersonContainer, RelationShip }  from '../models';
import { AppSettingService } from '../shares';
import { TreeNode } from 'primeng/api';

@Injectable({ providedIn: 'root' })
export class PersonService {
	public searchCriteria: StaffSearch;
	public parentList: Person[] =[];
	constructor(private http: HttpClient,

		private appSetting :AppSettingService
		) {
			this.searchCriteria = {
				email:'',
				firstName: '',
				lastName:''
			};
		
		}
	searchPersons(criteria: StaffSearch): Observable<ResultModel<Person[]>> {
		let config = { headers : { 'Content-Type': 'application/json' } };
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.personUrl + "/SearchPerson";
		return this.http.post<ResultModel<Person[]>>(baseUrl, criteria, config);
	}
	
	loadPersonById(id:number): Observable<ResultModel<Person>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.personUrl;
		/*
		const params = new HttpParams().set("id", ""+id);
		const headers = new HttpHeaders().set('Content-Type', 'application/json');
		const options = {
			headers: headers,
			params: params
			}; 
			*/
		
		return this.http.get<ResultModel<Person>>(baseUrl + "/GetById/" + id);
	}
	loadPersonFamily(id: number): Observable<ResultModel<TreeNode>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.personUrl;
		return this.http.get<ResultModel<TreeNode>>(baseUrl + "/GetByPersonFamily/" + id);
	}

	loadPersonFamilyTree(useFather: boolean, useMother: boolean): Observable<ResultModel<TreeNode[]>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.personUrl;
		const data = {useFather,useMother};
		return this.http.post<ResultModel<TreeNode[]>>(baseUrl + "/GetFamilyTreeBy", data);
	}

	loadChildrenById(fatherId:number, motherId: number): Observable<ResultModel<Person[]>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.personUrl;
		const data = {fatherId,motherId};
		
		return this.http.post<ResultModel<Person[]>>(baseUrl + "/GetChildress/" ,data);
	}
	loadRelationshipById(personId:number): Observable<ResultModel<RelationShip[]>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.relationShipUrl;
		return this.http.get<ResultModel<RelationShip[]>>(baseUrl + "/GetByPersonId/" +personId);
	}
	savePerson(data:PersonContainer): Observable<ResultModel<number>> { //insert Adminuser
		let config = { headers : { 'Content-Type': 'application/json' } };
		console.log("save family", data);
		const baseUrl = this.appSetting.appSetting.baseUrl  + "/"+ ConfigureUrl.personUrl +"/SavePerson";
		return this.http.post<ResultModel<number>>(baseUrl, data, config);
	}
	
	deletePerson(id:number): Observable<ResultModel<number>>{
		const baseUrl = this.appSetting.appSetting.baseUrl  + "/"+ ConfigureUrl.personUrl;
		const data = {id};
		return this.http.post<ResultModel<number>>(baseUrl + "/DeleteById" ,data);
	}
	uploadFile(data: any): Observable<ResultModel<string>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/" + ConfigureUrl.personUrl + "/UploadImage";
		return this.http.post<ResultModel<string>>(baseUrl, data);
	}
	downloadFile(imageName: string): Observable<ResultModel<string>> {
		const baseUrl = this.appSetting.appSetting.baseUrl + "/" + ConfigureUrl.FileUploadUrl + "/DownloadFile";
		const data = {
			fileName: imageName
		};
		return this.http.post<ResultModel<string>>(baseUrl, data);
	}
	deleteUploadFile(data: any): Observable<ResultModel<number>> {
		//data ={filename};
		const baseUrl = this.appSetting.appSetting.baseUrl + "/" + ConfigureUrl.personUrl + "/DeleteUploadFile";
		return this.http.post<ResultModel<number>>(baseUrl, data);
	}
}
