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

@RestController
@RequestMapping(value = "")
public class UserRoleController {
	@Autowired
    UserRoleService userroleservice;


	@RequestMapping(method = RequestMethod.POST, value = "/userroles")
    @PreAuthorize("@userrole_pms.check('CREATE')")
    public ResponseEntity<UserRole> create(@RequestBody UserRole userrole) {
        UserRole userrole2 = userroleservice.create(userrole);
        return ResponseEntity.status(HttpStatus.OK).body(userrole2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroles/{userrole_id}/save")
    @PreAuthorize("@userrole_pms.check(#userrole_id,'')")
    public ResponseEntity<UserRole> save(@PathVariable("userrole_id") String userrole_id, @RequestBody UserRole userrole) {
        UserRole userrole2 = userroleservice.save(userrole_id, userrole);
        return ResponseEntity.status(HttpStatus.OK).body(userrole2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/userroles/{userrole_id}")
    @PreAuthorize("@userrole_pms.check(#userrole_id,'UPDATE')")
    public ResponseEntity<UserRole> update(@PathVariable("userrole_id") String userrole_id, @RequestBody UserRole userrole) {
        UserRole userrole2 = userroleservice.update(userrole_id, userrole);
        return ResponseEntity.status(HttpStatus.OK).body(userrole2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/userroles/{userrole_id}")
    @PreAuthorize("@userrole_pms.check(#userrole_id,'UPDATE')")
    public ResponseEntity<UserRole> api_update(@PathVariable("userrole_id") String userrole_id, @RequestBody UserRole userrole) {
        UserRole userrole2 = userroleservice.update(userrole_id, userrole);
        return ResponseEntity.status(HttpStatus.OK).body(userrole2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/userroles/checkkey")
    @PreAuthorize("@userrole_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserRole userrole) {
        boolean b = userroleservice.checkKey(userrole);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/userroles/{userrole_id}")
    @PreAuthorize("@userrole_pms.check(#userrole_id,'READ')")
    public ResponseEntity<UserRole> get(@PathVariable("userrole_id") String userrole_id) {
        UserRole userrole = userroleservice.get( userrole_id);
        return ResponseEntity.status(HttpStatus.OK).body(userrole);
    }

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

	@RequestMapping(method = RequestMethod.GET, value = "/userroles/getdraft")
    @PreAuthorize("@userrole_pms.check('CREATE')")
    public ResponseEntity<UserRole> getDraft() {
        //UserRole userrole = userroleservice.getDraft( userrole_id);
        return ResponseEntity.status(HttpStatus.OK).body(new UserRole());
    }

    @RequestMapping(method= RequestMethod.GET , value="/userroles/fetchdefault")
    @PreAuthorize("@userrole_pms.check('READ')")
	public ResponseEntity<List<UserRole>> fetchDefault(UserRoleSearchContext searchContext,Pageable pageable) {
        
        Page<UserRole> page = userroleservice.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());
    }


}
