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.SystemService;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.domain.System;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;

@RestController
@RequestMapping(value = "")
public class SystemController {
	@Autowired
    SystemService systemservice;


	@RequestMapping(method = RequestMethod.POST, value = "/systems")
    @PreAuthorize("@system_pms.check('CREATE')")
    public ResponseEntity<System> create(@RequestBody System system) {
        System system2 = systemservice.create(system);
        return ResponseEntity.status(HttpStatus.OK).body(system2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/systems/{system_id}")
    @PreAuthorize("@system_pms.check(#system_id,'UPDATE')")
    public ResponseEntity<System> update(@PathVariable("system_id") String system_id, @RequestBody System system) {
        System system2 = systemservice.update(system_id, system);
        return ResponseEntity.status(HttpStatus.OK).body(system2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/systems/{system_id}")
    @PreAuthorize("@system_pms.check(#system_id,'UPDATE')")
    public ResponseEntity<System> api_update(@PathVariable("system_id") String system_id, @RequestBody System system) {
        System system2 = systemservice.update(system_id, system);
        return ResponseEntity.status(HttpStatus.OK).body(system2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/systems/{system_id}")
    @PreAuthorize("@system_pms.check(#system_id,'READ')")
    public ResponseEntity<System> get(@PathVariable("system_id") String system_id) {
        System system = systemservice.get( system_id);
        return ResponseEntity.status(HttpStatus.OK).body(system);
    }

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

	@RequestMapping(method = RequestMethod.POST, value = "/systems/{system_id}/save")
    @PreAuthorize("@system_pms.check(#system_id,'')")
    public ResponseEntity<System> save(@PathVariable("system_id") String system_id, @RequestBody System system) {
        System system2 = systemservice.save(system_id, system);
        return ResponseEntity.status(HttpStatus.OK).body(system2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/systems/checkkey")
    @PreAuthorize("@system_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody System system) {
        boolean b = systemservice.checkKey(system);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/systems/getdraft")
    @PreAuthorize("@system_pms.check('CREATE')")
    public ResponseEntity<System> getDraft() {
        //System system = systemservice.getDraft( system_id);
        return ResponseEntity.status(HttpStatus.OK).body(new System());
    }

    @RequestMapping(method= RequestMethod.GET , value="/systems/fetchdefault")
    @PreAuthorize("@system_pms.check('READ')")
	public ResponseEntity<List<System>> fetchDefault(SystemSearchContext searchContext,Pageable pageable) {
        
        Page<System> page = systemservice.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());
    }


}
