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.ApplicationDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.Application;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IApplicationService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.ApplicationSearchContext;

@Api(tags = {"Application" })
@RestController
@RequestMapping("")
public class ApplicationResource {

    @Autowired
    private IApplicationService applicationService;

    public IApplicationService getApplicationService() {
        return this.applicationService;
    }

    @ApiOperation(value = "Remove", tags = {"Application" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/applications/{application_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("application_id") String application_id) {
        ApplicationDTO applicationdto = new ApplicationDTO();
		Application domain = new Application();
		applicationdto.setApplicationId(application_id);
		domain.setApplicationId(application_id);
        Boolean rst = applicationService.remove(domain.getApplicationId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

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

    @ApiOperation(value = "Save", tags = {"Application" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/applications/{application_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody ApplicationDTO applicationdto) {
        Application application = applicationdto.toDO();
    	Boolean b = applicationService.save(application) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "GetDraft", tags = {"Application" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/applications/{application_id}/getdraft")

    public ResponseEntity<ApplicationDTO> getDraft(@PathVariable("application_id") String application_id, @RequestBody ApplicationDTO applicationdto) {
        Application application = applicationdto.toDO();
    	application = applicationService.getDraft(application) ;
    	applicationdto.fromDO(application);
        return ResponseEntity.status(HttpStatus.OK).body(applicationdto);
    }

    @ApiOperation(value = "Update", tags = {"Application" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/applications/{application_id}")

    public ResponseEntity<ApplicationDTO> update(@PathVariable("application_id") String application_id, @RequestBody ApplicationDTO applicationdto) {
		Application domain = applicationdto.toDO();
        domain.setApplicationId(application_id);
		applicationService.update(domain);
		ApplicationDTO dto = new ApplicationDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Get", tags = {"Application" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/applications/{application_id}")
    public ResponseEntity<ApplicationDTO> get(@PathVariable("application_id") String application_id) {
        ApplicationDTO dto = new ApplicationDTO();
        Application domain = applicationService.get(application_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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

    public ResponseEntity<ApplicationDTO> create(@RequestBody ApplicationDTO applicationdto) {
        ApplicationDTO dto = new ApplicationDTO();
        Application domain = applicationdto.toDO();
		applicationService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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


}
