package one.kii.kiimate.status.core.ctl;

import one.kii.kiimate.status.core.api.RefreshStatusApi;
import one.kii.summer.beans.utils.MultiValueMapTools;
import one.kii.summer.io.context.ErestHeaders;
import one.kii.summer.io.context.WriteContext;
import one.kii.summer.io.exception.Conflict;
import one.kii.summer.io.exception.NotFound;
import one.kii.summer.io.receiver.ErestResponse;
import one.kii.summer.io.receiver.WriteController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static one.kii.kiimate.status.core.ctl.RefreshStatusCtl.OWNER_ID;
import static one.kii.kiimate.status.core.ctl.RefreshStatusCtl.SUB_ID;


/**
 * Created by WangYanJiong on 4/13/17.
 */

@RestController
@RequestMapping(value = "/api/v1/{" + OWNER_ID + "}/status/{" + SUB_ID + "}", method = RequestMethod.PUT)
@CrossOrigin(origins = "*")
public class RefreshStatusCtl extends WriteController {

    public static final String OWNER_ID = "owner-id";

    public static final String SUB_ID = "sub-id";

    @Autowired
    private RefreshStatusApi api;

    @RequestMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseEntity<List<RefreshStatusApi.Instance>> commitForm(
            @RequestHeader(ErestHeaders.REQUEST_ID) String requestId,
            @RequestHeader(ErestHeaders.OPERATOR_ID) String operatorId,
            @PathVariable(OWNER_ID) String ownerId,
            @PathVariable(SUB_ID) String subId,
            @RequestParam MultiValueMap<String, String> map) {
        return commit(requestId, operatorId, ownerId, subId, map);
    }


    @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<List<RefreshStatusApi.Instance>> commitJson(
            @RequestHeader(ErestHeaders.REQUEST_ID) String requestId,
            @RequestHeader(ErestHeaders.OPERATOR_ID) String operatorId,
            @PathVariable(OWNER_ID) String ownerId,
            @PathVariable(SUB_ID) String subId,
            @RequestBody String json) {
        MultiValueMap mmap = MultiValueMapTools.from(json);
        return commit(requestId, operatorId, ownerId, subId, mmap);
    }

    private ResponseEntity<List<RefreshStatusApi.Instance>> commit(
            String requestId,
            String operatorId,
            String ownerId,
            String subId,
            MultiValueMap<String, String> map) {
        try {

            WriteContext context = buildContext(requestId, operatorId, ownerId);

            RefreshStatusApi.Form form = new RefreshStatusApi.Form();
            form.setSubId(subId);
            form.setMap(map);

            return ErestResponse.created(requestId, api.commit(context, form));
        } catch (NotFound notFound) {
            return ErestResponse.notFound(requestId, notFound.getKey());
        } catch (Conflict conflict) {
            return ErestResponse.conflict(requestId, conflict.getKeys()[0]);
        }
    }


}

@@ -1107,31 +1107,24 @@
 ller %7B%0A%0A    
-public 
 static final
@@ -1159,23 +1159,16 @@
 %22;%0A%0A    
-public 
 static f
@@ -1354,41 +1354,8 @@
 tity
-%3CList%3CRefreshStatusApi.Instance%3E%3E
  com
@@ -1842,41 +1842,8 @@
 tity
-%3CList%3CRefreshStatusApi.Instance%3E%3E
  com
@@ -2154,16 +2154,32 @@
 ValueMap
+%3CString, String%3E
  mmap = 

fix warnings