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

@RestController
@RequestMapping(value = "")
public class UserController {
	@Autowired
    UserService userservice;


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

	@RequestMapping(method = RequestMethod.POST, value = "/users")
    @PreAuthorize("@user_pms.check('CREATE')")
    public ResponseEntity<User> create(@RequestBody User user) {
        User user2 = userservice.create(user);
        return ResponseEntity.status(HttpStatus.OK).body(user2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/users/{user_id}/synctouser")
    @PreAuthorize("@user_pms.check(#user_id,'')")
    public ResponseEntity<User> syncToUser(@PathVariable("user_id") String user_id, @RequestBody User user) {
        User user2 = userservice.syncToUser(user_id, user);
        return ResponseEntity.status(HttpStatus.OK).body(user2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/users/checkkey")
    @PreAuthorize("@user_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody User user) {
        boolean b = userservice.checkKey(user);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/users/{user_id}")
    @PreAuthorize("@user_pms.check(#user_id,'READ')")
    public ResponseEntity<User> get(@PathVariable("user_id") String user_id) {
        User user = userservice.get( user_id);
        return ResponseEntity.status(HttpStatus.OK).body(user);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/users/{user_id}/save")
    @PreAuthorize("@user_pms.check(#user_id,'')")
    public ResponseEntity<User> save(@PathVariable("user_id") String user_id, @RequestBody User user) {
        User user2 = userservice.save(user_id, user);
        return ResponseEntity.status(HttpStatus.OK).body(user2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/users/getdraft")
    @PreAuthorize("@user_pms.check('CREATE')")
    public ResponseEntity<User> getDraft() {
        //User user = userservice.getDraft( user_id);
        return ResponseEntity.status(HttpStatus.OK).body(new User());
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/users/{user_id}")
    @PreAuthorize("@user_pms.check(#user_id,'UPDATE')")
    public ResponseEntity<User> update(@PathVariable("user_id") String user_id, @RequestBody User user) {
        User user2 = userservice.update(user_id, user);
        return ResponseEntity.status(HttpStatus.OK).body(user2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/users/{user_id}")
    @PreAuthorize("@user_pms.check(#user_id,'UPDATE')")
    public ResponseEntity<User> api_update(@PathVariable("user_id") String user_id, @RequestBody User user) {
        User user2 = userservice.update(user_id, user);
        return ResponseEntity.status(HttpStatus.OK).body(user2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/users/fetchdefault")
    @PreAuthorize("@user_pms.check('READ')")
	public ResponseEntity<List<User>> fetchDefault(UserSearchContext searchContext,Pageable pageable) {
        
        Page<User> page = userservice.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());
    }


}
