package com.agilex.healthcare.schedulingmanager.resources;

import com.agilex.healthcare.mobilehealthplatform.domain.ResourceDirectory;
import com.agilex.healthcare.schedulingmanager.datalayer.SchedulingDataLayer;
import com.agilex.healthcare.schedulingmanager.datalayer.SpecialtyDataLayer;
import com.agilex.healthcare.schedulingmanager.datalayer.ToolsDataLayer;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingClinics;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingSpecialties;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingSpecialty;
import com.agilex.healthcare.schedulingmanager.linkbuilder.ClinicLinkBuilder;
import com.agilex.healthcare.schedulingmanager.linkbuilder.LocationServiceResourceDirectoryBuilder;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;

@Path("/location-service/")
@Component
@Scope("request")
public class LocationServiceResource {

	@Resource
    LocationServiceResourceDirectoryBuilder locationServiceResourceDirectoryBuilder;

    @Resource
    SpecialtyDataLayer specialtyDataLayer;
    
    @Resource
    ToolsDataLayer toolsDataLayer;

	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("site/{facility-code}/resource-directory")
	public ResourceDirectory fetchResourceDirectory(@Context UriInfo uriInfo, @Context HttpServletRequest request, @Context HttpHeaders headers, @PathParam("facility-code") String facilityCode) {
		ResourceDirectory resourceDirectory = locationServiceResourceDirectoryBuilder.getResourceDirectory(uriInfo.getBaseUri(), facilityCode);
		return resourceDirectory;
	}

//	@GET
//	@Produces({ "application/xml", "application/json" })
//	@Path("site/{facility-code}/wards")
//	public Wards fetchWards(@Context UriInfo uriInfo, @Context HttpServletRequest request, @Context HttpHeaders headers, @PathParam("facility-code") String facilityCode) {
//		WardFilterFactory factory = new WardFilterFactory();
//		WardFilter filter = factory.createFromUri(uriInfo.getRequestUri());
//
//		WardDataService dataService = new WardDataService();
//		return dataService.fetchWards(facilityCode, filter);
//	}
//
//	@GET
//	@Produces({ "application/xml", "application/json" })
//	@Path("site/{facility-code}/clinics")
//	public Clinics fetchClinics(@Context UriInfo uriInfo, @Context HttpServletRequest request, @Context HttpHeaders headers, @PathParam("facility-code") String facilityCode) {
//		ClinicFilterFactory factory = new ClinicFilterFactory();
//		ClinicFilter filter = factory.createFromUri(uriInfo.getRequestUri());
//
//		ClinicDataService dataService = new ClinicDataService();
//		return dataService.fetchClinics(facilityCode, filter);
//	}
//
    @GET
    @Produces({ "application/xml", "application/json" })
    @Path("site/{facility-code}/specialties")
    public SchedulingClinics fetchSpecialties (@Context UriInfo uriInfo, @Context HttpServletRequest request, @Context HttpHeaders headers, @PathParam("facility-code") String facilityCode) {
        SchedulingClinics schedulingClinics = toolsDataLayer.getClinicsWithSpecialties(facilityCode);
    	buildClinicAtomLinks(uriInfo,facilityCode,schedulingClinics);
        return schedulingClinics;
    }
//
//    @GET
//    @Produces({ "application/xml", "application/json" })
//    @Path("site/{facility-code}/specialties/system/{system}")
//    public SchedulingSpecialties fetchSpecialtiesBySystem (@PathParam("facility-code") String facilityCode,
//    											 @PathParam("system") String system,
//     											 @Context UriInfo uriInfo,
//    											 @Context HttpServletRequest request,
//    											 @Context HttpHeaders headers) {
//        SpecialtyDataService dataService = new SpecialtyDataService();
//        SchedulingSpecialties specialties = dataService.fetchSpecialties(facilityCode, system);
//        return specialties;
//    }
//
//    @GET
//    @Produces({ "application/xml", "application/json" })
//    @Path("site/{facility-code}/specialty")
//    public SchedulingSpecialty fetchSpecialty (@Context UriInfo uriInfo, @Context HttpServletRequest request, @Context HttpHeaders headers, @PathParam("facility-code") String facilityCode, @QueryParam("name") String specialtyName) {
//        SpecialtyDataService dataService = new SpecialtyDataService();
//        SchedulingSpecialty specialty = dataService.getSpecialty(facilityCode, specialtyName);
//        buildClinicAtomLinks(uriInfo, facilityCode,specialty.getSpecialtyClinics());
//        return specialty;
//
//    }
//
    private void buildClinicsAtomLinksForAllSpecialties(UriInfo uriInfo, String facilityCode, SchedulingSpecialties specialties){

        for(SchedulingSpecialty specialty: specialties){
            buildClinicAtomLinks(uriInfo, facilityCode,specialty.getSpecialtyClinics());
        }

    }

    private void buildClinicAtomLinks(UriInfo uriInfo, String facilityCode, SchedulingClinics clinics){

        if(uriInfo!=null && clinics!=null){
            ClinicLinkBuilder clinicLinkBuilder = new ClinicLinkBuilder(uriInfo.getBaseUri(), facilityCode);
            clinicLinkBuilder.fillLinks(clinics);
        }
    }
}
