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.UserObjectDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserObject;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserObjectService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserObjectSearchContext;

@Api(tags = {"UserObject" })
@RestController
@RequestMapping("")
public class UserObjectResource {

    @Autowired
    private IUserObjectService userobjectService;

    public IUserObjectService getUserobjectService() {
        return this.userobjectService;
    }

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

    @ApiOperation(value = "Save", tags = {"UserObject" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/userobjects/{userobject_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody UserObjectDTO userobjectdto) {
        UserObject userobject = userobjectdto.toDO();
    	Boolean b = userobjectService.save(userobject) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    public ResponseEntity<UserObjectDTO> create(@RequestBody UserObjectDTO userobjectdto) {
        UserObjectDTO dto = new UserObjectDTO();
        UserObject domain = userobjectdto.toDO();
		userobjectService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"UserObject" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userobjects/{userobject_id}/getdraft")

    public ResponseEntity<UserObjectDTO> getDraft(@PathVariable("userobject_id") String userobject_id, @RequestBody UserObjectDTO userobjectdto) {
        UserObject userobject = userobjectdto.toDO();
    	userobject = userobjectService.getDraft(userobject) ;
    	userobjectdto.fromDO(userobject);
        return ResponseEntity.status(HttpStatus.OK).body(userobjectdto);
    }

    @ApiOperation(value = "Remove", tags = {"UserObject" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/userobjects/{userobject_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("userobject_id") String userobject_id) {
        UserObjectDTO userobjectdto = new UserObjectDTO();
		UserObject domain = new UserObject();
		userobjectdto.setUserObjectId(userobject_id);
		domain.setUserObjectId(userobject_id);
        Boolean rst = userobjectService.remove(domain.getUserObjectId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Get", tags = {"UserObject" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/userobjects/{userobject_id}")
    public ResponseEntity<UserObjectDTO> get(@PathVariable("userobject_id") String userobject_id) {
        UserObjectDTO dto = new UserObjectDTO();
        UserObject domain = userobjectService.get(userobject_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Update", tags = {"UserObject" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/userobjects/{userobject_id}")

    public ResponseEntity<UserObjectDTO> update(@PathVariable("userobject_id") String userobject_id, @RequestBody UserObjectDTO userobjectdto) {
		UserObject domain = userobjectdto.toDO();
        domain.setUserObjectId(userobject_id);
		userobjectService.update(domain);
		UserObjectDTO dto = new UserObjectDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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


}
