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

@RestController
@RequestMapping(value = "")
public class UserGroupController {
	@Autowired
    UserGroupService usergroupservice;


	@RequestMapping(method = RequestMethod.POST, value = "/usergroups")
    @PreAuthorize("@usergroup_pms.check('CREATE')")
    public ResponseEntity<UserGroup> create(@RequestBody UserGroup usergroup) {
        UserGroup usergroup2 = usergroupservice.create(usergroup);
        return ResponseEntity.status(HttpStatus.OK).body(usergroup2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/usergroups/{usergroup_id}/save")
    @PreAuthorize("@usergroup_pms.check(#usergroup_id,'')")
    public ResponseEntity<UserGroup> save(@PathVariable("usergroup_id") String usergroup_id, @RequestBody UserGroup usergroup) {
        UserGroup usergroup2 = usergroupservice.save(usergroup_id, usergroup);
        return ResponseEntity.status(HttpStatus.OK).body(usergroup2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/usergroups/checkkey")
    @PreAuthorize("@usergroup_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserGroup usergroup) {
        boolean b = usergroupservice.checkKey(usergroup);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/usergroups/getdraft")
    @PreAuthorize("@usergroup_pms.check('CREATE')")
    public ResponseEntity<UserGroup> getDraft() {
        //UserGroup usergroup = usergroupservice.getDraft( usergroup_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserGroup());
    }

	@RequestMapping(method = RequestMethod.GET, value = "/usergroups/{usergroup_id}")
    @PreAuthorize("@usergroup_pms.check(#usergroup_id,'READ')")
    public ResponseEntity<UserGroup> get(@PathVariable("usergroup_id") String usergroup_id) {
        UserGroup usergroup = usergroupservice.get( usergroup_id);
        return ResponseEntity.status(HttpStatus.OK).body(usergroup);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/usergroups/{usergroup_id}")
    @PreAuthorize("@usergroup_pms.check(#usergroup_id,'UPDATE')")
    public ResponseEntity<UserGroup> update(@PathVariable("usergroup_id") String usergroup_id, @RequestBody UserGroup usergroup) {
        UserGroup usergroup2 = usergroupservice.update(usergroup_id, usergroup);
        return ResponseEntity.status(HttpStatus.OK).body(usergroup2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/usergroups/{usergroup_id}")
    @PreAuthorize("@usergroup_pms.check(#usergroup_id,'UPDATE')")
    public ResponseEntity<UserGroup> api_update(@PathVariable("usergroup_id") String usergroup_id, @RequestBody UserGroup usergroup) {
        UserGroup usergroup2 = usergroupservice.update(usergroup_id, usergroup);
        return ResponseEntity.status(HttpStatus.OK).body(usergroup2);
    }

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

    @RequestMapping(method= RequestMethod.GET , value="/usergroups/fetchdefault")
    @PreAuthorize("@usergroup_pms.check('READ')")
	public ResponseEntity<List<UserGroup>> fetchDefault(UserGroupSearchContext searchContext,Pageable pageable) {
        
        Page<UserGroup> page = usergroupservice.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());
    }


}
