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

import one.kii.kiimate.model.core.api.DeclareExtensionApi;
import one.kii.summer.io.context.ErestHeaders;
import one.kii.summer.io.context.WriteContext;
import one.kii.summer.io.exception.BadRequest;
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.web.bind.annotation.*;

import static one.kii.kiimate.model.core.ctl.DeclareExtensionCtl.OWNER_ID;


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

@RestController
@RequestMapping("/api/v1/{" + OWNER_ID + "}/extension")
public class DeclareExtensionCtl extends WriteController {

    public static final String OWNER_ID = "ownerId";

    @Autowired
    private DeclareExtensionApi api;

    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    ResponseEntity<DeclareExtensionApi.CommitReceipt> commit(
            @RequestHeader(ErestHeaders.REQUEST_ID) String requestId,
            @RequestHeader(ErestHeaders.OPERATOR_ID) String operatorId,
            @PathVariable(OWNER_ID) String ownerId,
            @ModelAttribute DeclareExtensionApi.CommitForm form) {

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

            return ErestResponse.created(requestId, api.commit(context, form));
        } catch (BadRequest badRequest) {
            return ErestResponse.badRequest(requestId, badRequest.getFields());
        } catch (Conflict conflict) {
            return ErestResponse.conflict(requestId, conflict.getKeys());
        }
    }


    @RequestMapping(method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    ResponseEntity<DeclareExtensionApi.CancelReceipt> cancel(
            @RequestHeader(ErestHeaders.REQUEST_ID) String requestId,
            @RequestHeader(ErestHeaders.OPERATOR_ID) String operatorId,
            @PathVariable(OWNER_ID) String ownerId,
            @ModelAttribute DeclareExtensionApi.CancelForm form) {

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

            return ErestResponse.created(requestId, api.cancel(context, form));
        } catch (NotFound notFound) {
            return ErestResponse.notFound(requestId, notFound.getKey());
        } catch (BadRequest badRequest) {
            return ErestResponse.badRequest(requestId, badRequest.getFields());
        }
    }


}

@@ -826,16 +826,42 @@
 nsion%22)%0A
+@CrossOrigin(value = %22*%22)%0A
 public c
@@ -1071,32 +1071,33 @@
 OST, consumes = 
+%7B
 MediaType.APPLIC
@@ -1115,37 +1115,84 @@
 URLENCODED_VALUE
-)%0A   
+, MediaType.APPLICATION_JSON_UTF8_VALUE%7D)%0A    public
  ResponseEntity%3C
@@ -1448,32 +1448,45 @@
 @ModelAttribute 
+@RequestBody 
 DeclareExtension
@@ -1500,33 +1500,32 @@
 mitForm form) %7B%0A
-%0A
         try %7B%0A  
@@ -1935,17 +1935,16 @@
 %0A    %7D%0A%0A
-%0A
     @Req
@@ -1996,16 +1996,17 @@
 sumes = 
+%7B
 MediaTyp
@@ -2040,21 +2040,68 @@
 ED_VALUE
-)%0A   
+, MediaType.APPLICATION_JSON_UTF8_VALUE%7D)%0A    public
  Respons
@@ -2337,32 +2337,32 @@
 String ownerId,%0A
-
             @Mod
@@ -2373,16 +2373,29 @@
 tribute 
+@RequestBody 
 DeclareE

support double form format: form-urlencoded & json