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.UserGroupDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserGroup;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserGroupService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserGroupSearchContext;

@Api(tags = {"UserGroup" })
@RestController
@RequestMapping("")
public class UserGroupResource {

    @Autowired
    private IUserGroupService usergroupService;

    public IUserGroupService getUsergroupService() {
        return this.usergroupService;
    }

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

    public ResponseEntity<UserGroupDTO> create(@RequestBody UserGroupDTO usergroupdto) {
        UserGroupDTO dto = new UserGroupDTO();
        UserGroup domain = usergroupdto.toDO();
		usergroupService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"UserGroup" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/usergroups/{usergroup_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserGroupDTO usergroupdto) {
        UserGroup usergroup = usergroupdto.toDO();
    	Boolean b = usergroupService.save(usergroup) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    @ApiOperation(value = "GetDraft", tags = {"UserGroup" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/usergroups/{usergroup_id}/getdraft")

    public ResponseEntity<UserGroupDTO> getDraft(@PathVariable("usergroup_id") String usergroup_id, @RequestBody UserGroupDTO usergroupdto) {
        UserGroup usergroup = usergroupdto.toDO();
    	usergroup = usergroupService.getDraft(usergroup) ;
    	usergroupdto.fromDO(usergroup);
        return ResponseEntity.status(HttpStatus.OK).body(usergroupdto);
    }

    @ApiOperation(value = "Get", tags = {"UserGroup" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/usergroups/{usergroup_id}")
    public ResponseEntity<UserGroupDTO> get(@PathVariable("usergroup_id") String usergroup_id) {
        UserGroupDTO dto = new UserGroupDTO();
        UserGroup domain = usergroupService.get(usergroup_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Update", tags = {"UserGroup" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/usergroups/{usergroup_id}")

    public ResponseEntity<UserGroupDTO> update(@PathVariable("usergroup_id") String usergroup_id, @RequestBody UserGroupDTO usergroupdto) {
		UserGroup domain = usergroupdto.toDO();
        domain.setUserGroupId(usergroup_id);
		usergroupService.update(domain);
		UserGroupDTO dto = new UserGroupDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"UserGroup" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/usergroups/{usergroup_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("usergroup_id") String usergroup_id) {
        UserGroupDTO usergroupdto = new UserGroupDTO();
		UserGroup domain = new UserGroup();
		usergroupdto.setUserGroupId(usergroup_id);
		domain.setUserGroupId(usergroup_id);
        Boolean rst = usergroupService.remove(domain.getUserGroupId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

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


}
