package com.sinewang.statemate.core.spi;

import one.kii.statemate.core.spi.CreateIntensionSpi;
import one.kii.summer.io.exception.*;
import one.kii.summer.io.sender.ErestPost;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by WangYanJiong on 4/7/17.
 */
@ConfigurationProperties(prefix = "metamate")
@Component
public class DefaultCreateIntensionSpi implements CreateIntensionSpi {

    private static String TREE = "master";
    private static String VISIBILITY_PUBLIC = "public";
    private static String URI = "/{ownerId}/intension";

    private String baseUrl;

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    @Override
    public String createPublicPrimitiveIntension(PrimitiveIntensionForm form) throws Panic {
        String url = baseUrl + URI;

        ErestPost erest = new ErestPost(form.getOwnerId());

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.put("extId", toList(form.getExtId()));
        map.put("field", toList(form.getField()));
        map.put("single", toList(String.valueOf(form.isSingle())));
        map.put("structure", toList(form.getStructure()));
        map.put("visibility", toList(VISIBILITY_PUBLIC));
        IntensionReceipt receipt = null;
        try {
            receipt = erest.execute(url, map, IntensionReceipt.class, form.getOwnerId());
            return receipt.getId();
        } catch (Conflict conflict) {
            return conflict.getKeys()[0];
        } catch (BadRequest | NotFound | Forbidden | Panic panic) {
            throw new Panic();
        }
    }

    @Override
    public String createPublicImportIntension(ImportIntensionForm form) throws Panic {
        String url = baseUrl + URI;

        ErestPost erest = new ErestPost(form.getOwnerId());

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.put("extId", toList(form.getExtId()));
        map.put("field", toList(form.getField()));
        map.put("single", toList(String.valueOf(form.isSingle())));
        map.put("structure", toList(form.getStructure()));
        map.put("refExtId", toList(form.getRefExtId()));
        map.put("visibility", toList(VISIBILITY_PUBLIC));
        try {
            IntensionReceipt receipt = erest.execute(url, map, IntensionReceipt.class, form.getOwnerId());
            return receipt.getExtId();
        } catch (Conflict conflict) {
            return conflict.getKeys()[0];
        } catch (BadRequest | NotFound | Forbidden | Panic panic) {
            throw new Panic();
        }
    }

    private List<String> toList(String string) {
        List<String> list = new ArrayList<>();
        list.add(string);
        return list;
    }

}

@@ -1175,34 +1175,34 @@
 ();%0A        map.
-pu
+se
 t(%22extId%22, toLis
@@ -1188,39 +1188,32 @@
 ap.set(%22extId%22, 
-toList(
 form.getExtId())
@@ -1204,33 +1204,32 @@
 form.getExtId())
-)
 ;%0A        map.pu
@@ -1218,34 +1218,34 @@
 ));%0A        map.
-pu
+se
 t(%22field%22, toLis
@@ -1231,39 +1231,32 @@
 ap.set(%22field%22, 
-toList(
 form.getField())
@@ -1247,33 +1247,32 @@
 form.getField())
-)
 ;%0A        map.pu
@@ -1261,34 +1261,34 @@
 ));%0A        map.
-pu
+se
 t(%22single%22, toLi
@@ -1275,39 +1275,32 @@
 p.set(%22single%22, 
-toList(
 String.valueOf(f
@@ -1307,33 +1307,32 @@
 orm.isSingle()))
-)
 ;%0A        map.pu
@@ -1321,34 +1321,34 @@
 ));%0A        map.
-pu
+se
 t(%22structure%22, t
@@ -1338,39 +1338,32 @@
 et(%22structure%22, 
-toList(
 form.getStructur
@@ -1358,33 +1358,32 @@
 .getStructure())
-)
 ;%0A        map.pu
@@ -1372,34 +1372,34 @@
 ));%0A        map.
-pu
+se
 t(%22visibility%22, 
@@ -1390,39 +1390,32 @@
 t(%22visibility%22, 
-toList(
 VISIBILITY_PUBLI
@@ -1408,33 +1408,32 @@
 SIBILITY_PUBLIC)
-)
 ;%0A        Intens
@@ -2079,18 +2079,18 @@
     map.
-pu
+se
 t(%22extId
@@ -2088,31 +2088,24 @@
 et(%22extId%22, 
-toList(
 form.getExtI
@@ -2100,33 +2100,32 @@
 form.getExtId())
-)
 ;%0A        map.pu
@@ -2122,18 +2122,18 @@
     map.
-pu
+se
 t(%22field
@@ -2131,31 +2131,24 @@
 et(%22field%22, 
-toList(
 form.getFiel
@@ -2147,25 +2147,24 @@
 .getField())
-)
 ;%0A        ma
@@ -2161,26 +2161,26 @@
         map.
-pu
+se
 t(%22single%22, 
@@ -2179,23 +2179,16 @@
 ingle%22, 
-toList(
 String.v
@@ -2211,17 +2211,16 @@
 ngle()))
-)
 ;%0A      
@@ -2225,18 +2225,18 @@
     map.
-pu
+se
 t(%22struc
@@ -2242,23 +2242,16 @@
 cture%22, 
-toList(
 form.get
@@ -2258,25 +2258,24 @@
 Structure())
-)
 ;%0A        ma
@@ -2276,18 +2276,18 @@
     map.
-pu
+se
 t(%22refEx
@@ -2292,23 +2292,16 @@
 ExtId%22, 
-toList(
 form.get
@@ -2311,17 +2311,16 @@
 ExtId())
-)
 ;%0A      
@@ -2325,18 +2325,18 @@
     map.
-pu
+se
 t(%22visib
@@ -2343,23 +2343,16 @@
 ility%22, 
-toList(
 VISIBILI
@@ -2361,17 +2361,16 @@
 _PUBLIC)
-)
 ;%0A      

update multvaluemap, using set instead of put