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

@RestController
@RequestMapping(value = "")
public class UserObjectController {
	@Autowired
    UserObjectService userobjectservice;


	@RequestMapping(method = RequestMethod.POST, value = "/userobjects/checkkey")
    @PreAuthorize("@userobject_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserObject userobject) {
        boolean b = userobjectservice.checkKey(userobject);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userobjects/{userobject_id}/save")
    @PreAuthorize("@userobject_pms.check(#userobject_id,'')")
    public ResponseEntity<UserObject> save(@PathVariable("userobject_id") String userobject_id, @RequestBody UserObject userobject) {
        UserObject userobject2 = userobjectservice.save(userobject_id, userobject);
        return ResponseEntity.status(HttpStatus.OK).body(userobject2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userobjects")
    @PreAuthorize("@userobject_pms.check('CREATE')")
    public ResponseEntity<UserObject> create(@RequestBody UserObject userobject) {
        UserObject userobject2 = userobjectservice.create(userobject);
        return ResponseEntity.status(HttpStatus.OK).body(userobject2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userobjects/getdraft")
    @PreAuthorize("@userobject_pms.check('CREATE')")
    public ResponseEntity<UserObject> getDraft() {
        //UserObject userobject = userobjectservice.getDraft( userobject_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserObject());
    }

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

	@RequestMapping(method = RequestMethod.GET, value = "/userobjects/{userobject_id}")
    @PreAuthorize("@userobject_pms.check(#userobject_id,'READ')")
    public ResponseEntity<UserObject> get(@PathVariable("userobject_id") String userobject_id) {
        UserObject userobject = userobjectservice.get( userobject_id);
        return ResponseEntity.status(HttpStatus.OK).body(userobject);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/userobjects/{userobject_id}")
    @PreAuthorize("@userobject_pms.check(#userobject_id,'UPDATE')")
    public ResponseEntity<UserObject> update(@PathVariable("userobject_id") String userobject_id, @RequestBody UserObject userobject) {
        UserObject userobject2 = userobjectservice.update(userobject_id, userobject);
        return ResponseEntity.status(HttpStatus.OK).body(userobject2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userobjects/{userobject_id}")
    @PreAuthorize("@userobject_pms.check(#userobject_id,'UPDATE')")
    public ResponseEntity<UserObject> api_update(@PathVariable("userobject_id") String userobject_id, @RequestBody UserObject userobject) {
        UserObject userobject2 = userobjectservice.update(userobject_id, userobject);
        return ResponseEntity.status(HttpStatus.OK).body(userobject2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/userobjects/fetchdefault")
    @PreAuthorize("@userobject_pms.check('READ')")
	public ResponseEntity<List<UserObject>> fetchDefault(UserObjectSearchContext searchContext,Pageable pageable) {
        
        Page<UserObject> page = userobjectservice.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());
    }


}
