/*
 * Electronic Logistics Management Information System (eLMIS) is a supply chain management system for health commodities in a developing country setting.
 *
 * Copyright (C) 2015  John Snow, Inc (JSI). This program was produced for the U.S. Agency for International Development (USAID). It was prepared under the USAID | DELIVER PROJECT, Task Order 4.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openlmis.web.controller.vaccine;

import org.openlmis.core.domain.RightName;
import org.openlmis.core.domain.User;
import org.openlmis.core.service.FacilityService;
import org.openlmis.core.service.ProgramService;
import org.openlmis.core.service.UserService;
import org.openlmis.core.web.OpenLmisResponse;
import org.openlmis.core.web.controller.BaseController;
import org.openlmis.vaccine.service.reports.VaccineReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
public class VaccineProgramController extends BaseController {

    @Autowired
    VaccineReportService service;

    @Autowired
    ProgramService programService;

    @Autowired
    UserService userService;

    @Autowired
    FacilityService facilityService;

    @RequestMapping(value = "/vaccine/report/programs.json", method = RequestMethod.GET)
    public ResponseEntity<OpenLmisResponse> getProgramsForConfiguration() {
        return OpenLmisResponse.response("programs", programService.getAllIvdPrograms());
    }

    @RequestMapping(value = "/vaccine/report/ivd-form/programs.json", method = RequestMethod.GET)
    @PreAuthorize("@permissionEvaluator.hasPermission(principal,'CREATE_IVD,APPROVE_IVD, VIEW_IVD')")
    public ResponseEntity<OpenLmisResponse> getProgramForIvdFormHomeFacility(HttpServletRequest request) {
        Long userId = loggedInUserId(request);
        User user = userService.getById(userId);
        return OpenLmisResponse.response("programs", programService.getIvdProgramsSupportedByUserHomeFacilityWithRights(user.getFacilityId(), userId, "CREATE_REQUISITION", "AUTHORIZE_REQUISITION"));
    }

    @RequestMapping(value = "/vaccine/report/ivd-form/supervised-programs")
    @PreAuthorize("@permissionEvaluator.hasPermission(principal,'CREATE_IVD,APPROVE_IVD, VIEW_IVD')")
    public ResponseEntity<OpenLmisResponse> getProgramForIvdFormSupervisedFacilities(HttpServletRequest request) {
        return OpenLmisResponse.response("programs", programService.getIvdProgramForSupervisedFacilities(loggedInUserId(request), "CREATE_REQUISITION", "AUTHORIZE_REQUISITION"));
    }

    @RequestMapping(value = "/vaccine/report/ivd-form/facilities/{programId}.json", method = RequestMethod.GET)
    @PreAuthorize("@permissionEvaluator.hasPermission(principal,'CREATE_IVD,APPROVE_IVD, VIEW_IVD')")
    public ResponseEntity<OpenLmisResponse> getFacilities(@PathVariable Long programId, HttpServletRequest request) {
        Long userId = loggedInUserId(request);
        //TODO: make sure this method also supports home facility.
        return OpenLmisResponse.response("facilities", facilityService.getUserSupervisedFacilities(userId, programId, RightName.CREATE_IVD, RightName.APPROVE_IVD));
    }

    @RequestMapping(value = "/vaccine/demographics/programs.json", method = RequestMethod.GET)
    @PreAuthorize("@permissionEvaluator.hasPermission(principal,'MANAGE_DEMOGRAPHIC_ESTIMATES')")
    public ResponseEntity<OpenLmisResponse> getProgramsForDemographicEstimates(HttpServletRequest request) {
        return OpenLmisResponse.response("programs", programService.getProgramsForUserByRights(loggedInUserId(request), RightName.MANAGE_DEMOGRAPHIC_ESTIMATES));
    }


}

@@ -2997,53 +2997,71 @@
 Id, 
-%22CREATE_REQUISITION%22, %22AUTHORIZE_REQUISITION%22
+RightName.CREATE_IVD, RightName.APPROVE_IVD, RightName.VIEW_IVD
 ));%0A
@@ -3211,32 +3211,33 @@
 pal,'CREATE_IVD,
+ 
 APPROVE_IVD, VIE
@@ -3495,53 +3495,71 @@
 t), 
-%22CREATE_REQUISITION%22, %22AUTHORIZE_REQUISITION%22
+RightName.CREATE_IVD, RightName.APPROVE_IVD, RightName.VIEW_IVD
 ));%0A
@@ -3669,32 +3669,32 @@
 uestMethod.GET)%0A
-
     @PreAuthoriz
@@ -3745,32 +3745,33 @@
 pal,'CREATE_IVD,
+ 
 APPROVE_IVD, VIE

Change permission check to the new permission schemes.