package cn.ibizlab.r7.runtime.service.runtimemgr.resource;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.PageRequest;
import cn.ibizlab.r7.runtime.service.runtimemgr.dto.UserDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.User;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserSearchContext;

@Api(tags = {"User" })
@RestController
@RequestMapping("")
public class UserResource {

    @Autowired
    private IUserService userService;

    public IUserService getUserService() {
        return this.userService;
    }

    @ApiOperation(value = "Remove", tags = {"User" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/users/{user_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("user_id") String user_id) {
        UserDTO userdto = new UserDTO();
		User domain = new User();
		userdto.setUserId(user_id);
		domain.setUserId(user_id);
        Boolean rst = userService.remove(domain.getUserId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Create", tags = {"User" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/users")

    public ResponseEntity<UserDTO> create(@RequestBody UserDTO userdto) {
        UserDTO dto = new UserDTO();
        User domain = userdto.toDO();
		userService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "同步到用户", tags = {"User" },  notes = "同步到用户")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/users/{user_id}/synctouser")

    public ResponseEntity<UserDTO> syncToUser(@PathVariable("user_id") String user_id, @RequestBody UserDTO userdto) {
        User user = userdto.toDO();
    	user = userService.syncToUser(user) ;
    	userdto.fromDO(user);
        return ResponseEntity.status(HttpStatus.OK).body(userdto);
    }

    @ApiOperation(value = "CheckKey", tags = {"User" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/users/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody UserDTO userdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "Get", tags = {"User" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/users/{user_id}")
    public ResponseEntity<UserDTO> get(@PathVariable("user_id") String user_id) {
        UserDTO dto = new UserDTO();
        User domain = userService.get(user_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"User" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/users/{user_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserDTO userdto) {
        User user = userdto.toDO();
    	Boolean b = userService.save(user) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "GetDraft", tags = {"User" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/users/{user_id}/getdraft")

    public ResponseEntity<UserDTO> getDraft(@PathVariable("user_id") String user_id, @RequestBody UserDTO userdto) {
        User user = userdto.toDO();
    	user = userService.getDraft(user) ;
    	userdto.fromDO(user);
        return ResponseEntity.status(HttpStatus.OK).body(userdto);
    }

    @ApiOperation(value = "Update", tags = {"User" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/users/{user_id}")

    public ResponseEntity<UserDTO> update(@PathVariable("user_id") String user_id, @RequestBody UserDTO userdto) {
		User domain = userdto.toDO();
        domain.setUserId(user_id);
		userService.update(domain);
		UserDTO dto = new UserDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

	@ApiOperation(value = "获取DEFAULT", tags = {"User" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/users/fetchdefault")
	public ResponseEntity<Page<UserDTO>> fetchDefault(UserSearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<UserDTO> list = new ArrayList<UserDTO>();
        
        Page<User> domains = userService.searchDefault(context) ;
        for(User user : domains.getContent()){
            UserDTO dto = new UserDTO();
            dto.fromDO(user);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}


}
