package cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.service.ApplicationService;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.domain.Application;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;

@RestController
@RequestMapping(value = "")
public class ApplicationController {
	@Autowired
    ApplicationService applicationservice;


	@RequestMapping(method = RequestMethod.DELETE, value = "/applications/{application_id}")
    @PreAuthorize("@application_pms.check(#application_id,'DELETE')")
    public ResponseEntity<Boolean> remove(@PathVariable("application_id") String application_id) {
        boolean b = applicationservice.remove( application_id);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/applications/checkkey")
    @PreAuthorize("@application_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody Application application) {
        boolean b = applicationservice.checkKey(application);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/applications/{application_id}/save")
    @PreAuthorize("@application_pms.check(#application_id,'')")
    public ResponseEntity<Application> save(@PathVariable("application_id") String application_id, @RequestBody Application application) {
        Application application2 = applicationservice.save(application_id, application);
        return ResponseEntity.status(HttpStatus.OK).body(application2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/applications/getdraft")
    @PreAuthorize("@application_pms.check('CREATE')")
    public ResponseEntity<Application> getDraft() {
        //Application application = applicationservice.getDraft( application_id);
        return ResponseEntity.status(HttpStatus.OK).body(new Application());
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/applications/{application_id}")
    @PreAuthorize("@application_pms.check(#application_id,'UPDATE')")
    public ResponseEntity<Application> update(@PathVariable("application_id") String application_id, @RequestBody Application application) {
        Application application2 = applicationservice.update(application_id, application);
        return ResponseEntity.status(HttpStatus.OK).body(application2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/applications/{application_id}")
    @PreAuthorize("@application_pms.check(#application_id,'UPDATE')")
    public ResponseEntity<Application> api_update(@PathVariable("application_id") String application_id, @RequestBody Application application) {
        Application application2 = applicationservice.update(application_id, application);
        return ResponseEntity.status(HttpStatus.OK).body(application2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/applications/{application_id}")
    @PreAuthorize("@application_pms.check(#application_id,'READ')")
    public ResponseEntity<Application> get(@PathVariable("application_id") String application_id) {
        Application application = applicationservice.get( application_id);
        return ResponseEntity.status(HttpStatus.OK).body(application);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/applications")
    @PreAuthorize("@application_pms.check('CREATE')")
    public ResponseEntity<Application> create(@RequestBody Application application) {
        Application application2 = applicationservice.create(application);
        return ResponseEntity.status(HttpStatus.OK).body(application2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/applications/fetchdefault")
    @PreAuthorize("@application_pms.check('READ')")
	public ResponseEntity<List<Application>> fetchDefault(ApplicationSearchContext searchContext,Pageable pageable) {
        
        Page<Application> page = applicationservice.fetchDefault(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }


}
