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.SystemDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.System;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.ISystemService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.SystemSearchContext;

@Api(tags = {"System" })
@RestController
@RequestMapping("")
public class SystemResource {

    @Autowired
    private ISystemService systemService;

    public ISystemService getSystemService() {
        return this.systemService;
    }

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

    public ResponseEntity<SystemDTO> create(@RequestBody SystemDTO systemdto) {
        SystemDTO dto = new SystemDTO();
        System domain = systemdto.toDO();
		systemService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Update", tags = {"System" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/systems/{system_id}")

    public ResponseEntity<SystemDTO> update(@PathVariable("system_id") String system_id, @RequestBody SystemDTO systemdto) {
		System domain = systemdto.toDO();
        domain.setSystemId(system_id);
		systemService.update(domain);
		SystemDTO dto = new SystemDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Get", tags = {"System" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/systems/{system_id}")
    public ResponseEntity<SystemDTO> get(@PathVariable("system_id") String system_id) {
        SystemDTO dto = new SystemDTO();
        System domain = systemService.get(system_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"System" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/systems/{system_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("system_id") String system_id) {
        SystemDTO systemdto = new SystemDTO();
		System domain = new System();
		systemdto.setSystemId(system_id);
		domain.setSystemId(system_id);
        Boolean rst = systemService.remove(domain.getSystemId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Save", tags = {"System" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/systems/{system_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody SystemDTO systemdto) {
        System system = systemdto.toDO();
    	Boolean b = systemService.save(system) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    @ApiOperation(value = "GetDraft", tags = {"System" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/systems/{system_id}/getdraft")

    public ResponseEntity<SystemDTO> getDraft(@PathVariable("system_id") String system_id, @RequestBody SystemDTO systemdto) {
        System system = systemdto.toDO();
    	system = systemService.getDraft(system) ;
    	systemdto.fromDO(system);
        return ResponseEntity.status(HttpStatus.OK).body(systemdto);
    }

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


}
