package zhou.gank.io.util

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import groovy.transform.CompileStatic
import zhou.gank.io.model.Gank
import zhou.gank.io.model.GankDaily
import zhou.gank.io.model.ResultDaily

@CompileStatic
class JsonKit {

    public static ResultDaily generate(String json, Gson gson) {
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(json);
        JsonObject root = element.getAsJsonObject();
        JsonArray category = root.getAsJsonArray("category");
        Iterator<JsonElement> iterator = category.iterator();
        JsonObject results = root.getAsJsonObject("results");
        boolean error = root.get("error").getAsBoolean();
        ArrayList<String> types = new ArrayList<>(category.size());
        ArrayList<List<Gank>> ghs = new ArrayList<>(category.size());
        while (iterator.hasNext()) {
            JsonElement type = iterator.next();
            String t = type.getAsString();
            types.add(t);
            String e = results.get(t).toString();
            ghs.add(gson.fromJson(e, new TypeToken<List<Gank>>() {
            }.getType()) as List<Gank>);
        }

        ghs.sort { a, b ->
            def aa = a as List<Gank>
            def bb = b as List<Gank>
            return aa.get(0).type.compareTo(bb.get(0).type)
        }
        types.sort()

        return new ResultDaily(error, new GankDaily(types, ghs));
    }

    public static
    final String test = "{\"error\":false,\"results\":{\"iOS\":[{\"who\":\"CallMeWhy\",\"publishedAt\":\"2015-08-07T03:57:48.070Z\",\"desc\":\"LLVM 简介\",\"type\":\"iOS\",\"url\":\"http://adriansampson.net/blog/llvm.html\",\"used\":true,\"objectId\":\"55c40ac360b2c9d32a67ca25\",\"createdAt\":\"2015-08-07T01:32:51.588Z\",\"updatedAt\":\"2015-08-15T03:15:54.384Z\"},{\"who\":\"鲍永章\",\"publishedAt\":\"2015-08-07T03:57:47.242Z\",\"desc\":\"基于TextKit的UILabel，支持超链接和自定义表达式。\",\"type\":\"iOS\",\"url\":\"https://github.com/molon/MLLabel\",\"used\":true,\"objectId\":\"55c372fe60b2f809e41eb388\",\"createdAt\":\"2015-08-06T14:45:18.733Z\",\"updatedAt\":\"2015-08-15T03:15:54.536Z\"},{\"who\":\"CallMeWhy\",\"publishedAt\":\"2015-08-07T03:57:48.083Z\",\"desc\":\"Swift 和 C 函数\",\"type\":\"iOS\",\"url\":\"http://chris.eidhof.nl/posts/swift-c-interop.html\",\"used\":true,\"objectId\":\"55c40aea00b025867b19c9af\",\"createdAt\":\"2015-08-07T01:33:30.871Z\",\"updatedAt\":\"2015-08-15T03:15:54.408Z\"},{\"who\":\"CallMeWhy\",\"publishedAt\":\"2015-08-07T03:57:48.174Z\",\"desc\":\"Arrays Linked Lists 和性能比较\",\"type\":\"iOS\",\"url\":\"http://airspeedvelocity.net/2015/08/03/arrays-linked-lists-and-performance/\",\"used\":true,\"objectId\":\"55c40b0800b0fac2c2809acc\",\"createdAt\":\"2015-08-07T01:34:00.984Z\",\"updatedAt\":\"2015-08-15T03:15:55.105Z\"}],\"Android\":[{\"who\":\"mthli\",\"publishedAt\":\"2015-08-07T03:57:48.045Z\",\"desc\":\"类似Link Bubble的悬浮式操作设计\",\"type\":\"Android\",\"url\":\"https://github.com/recruit-lifestyle/FloatingView\",\"used\":true,\"objectId\":\"55c309a800b00045333db517\",\"createdAt\":\"2015-08-06T07:15:52.065Z\",\"updatedAt\":\"2015-08-15T03:15:55.098Z\"},{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:47.317Z\",\"desc\":\"Android开发中，有哪些让你觉得相见恨晚的方法、类或接口？\",\"type\":\"Android\",\"url\":\"http://www.zhihu.com/question/33636939\",\"used\":true,\"objectId\":\"55c40ad340ac7d0a9507b324\",\"createdAt\":\"2015-08-07T01:33:07.815Z\",\"updatedAt\":\"2015-08-15T03:15:54.407Z\"},{\"who\":\"鲍永章\",\"publishedAt\":\"2015-08-07T03:57:48.076Z\",\"desc\":\"使用注解来处理Activity的状态恢复\",\"type\":\"Android\",\"url\":\"https://github.com/tom91136/Akatsuki\",\"used\":true,\"objectId\":\"55c3769660b2750766971ce6\",\"createdAt\":\"2015-08-06T15:00:38.350Z\",\"updatedAt\":\"2015-08-15T03:15:54.866Z\"},{\"who\":\"有时放纵\",\"publishedAt\":\"2015-08-07T03:57:48.142Z\",\"desc\":\"Android Lollipop联系人之PinnedListView简单使用\",\"type\":\"Android\",\"url\":\"https://git.oschina.net/way/PinnedHeaderListView\",\"used\":true,\"objectId\":\"55c415c060b2d140ca882eac\",\"createdAt\":\"2015-08-07T02:19:44.342Z\",\"updatedAt\":\"2015-08-15T03:15:55.020Z\"},{\"who\":\"鲍永章\",\"publishedAt\":\"2015-08-07T03:57:48.073Z\",\"desc\":\"图片可以自动滚动的ImageView，可以实现视差效果。\",\"type\":\"Android\",\"url\":\"https://github.com/Q42/AndroidScrollingImageView\",\"used\":true,\"objectId\":\"55c3761400b00045334480d2\",\"createdAt\":\"2015-08-06T14:58:28.171Z\",\"updatedAt\":\"2015-08-15T03:15:55.123Z\"}],\"瞎推荐\":[{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:48.084Z\",\"desc\":\"程序员的电台FmM，这个页面chrome插件有问题啊哭，我写了回删除不了啊\",\"type\":\"瞎推荐\",\"url\":\"https://cmd.fm/\",\"used\":true,\"objectId\":\"55c40f5e00b00045334934b4\",\"createdAt\":\"2015-08-07T01:52:30.267Z\",\"updatedAt\":\"2015-08-15T03:15:54.383Z\"}],\"拓展资源\":[{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:48.081Z\",\"desc\":\"Display GitHub code in tree format\",\"type\":\"拓展资源\",\"url\":\"https://github.com/buunguyen/octotree\",\"used\":true,\"objectId\":\"55c40b8600b08484a7f3a032\",\"createdAt\":\"2015-08-07T01:36:06.932Z\",\"updatedAt\":\"2015-08-15T03:15:54.382Z\"}],\"福利\":[{\"who\":\"张涵宇\",\"publishedAt\":\"2015-08-07T03:57:47.310Z\",\"desc\":\"8.7——（1）\",\"type\":\"福利\",\"url\":\"http://ww2.sinaimg.cn/large/7a8aed7bgw1eutscfcqtcj20dw0i0q4l.jpg\",\"used\":true,\"objectId\":\"55c4080240ac7d0a9507905e\",\"createdAt\":\"2015-08-07T01:21:06.112Z\",\"updatedAt\":\"2015-08-15T03:15:54.765Z\"},{\"who\":\"张涵宇\",\"publishedAt\":\"2015-08-07T03:57:47.229Z\",\"desc\":\"8.7——（2）\",\"type\":\"福利\",\"url\":\"http://ww2.sinaimg.cn/large/7a8aed7bgw1eutsd0pgiwj20go0p0djn.jpg\",\"used\":true,\"objectId\":\"55c4081d60b2c9d32a67a92e\",\"createdAt\":\"2015-08-07T01:21:33.518Z\",\"updatedAt\":\"2015-08-15T03:15:54.843Z\"}],\"休息视频\":[{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:48.104Z\",\"desc\":\"听到就心情大好的歌，简直妖魔哈哈哈哈哈，原地址\\nhttp://v.youku.com/v_show/id_XMTQxODA5NDM2.html\",\"type\":\"休息视频\",\"url\":\"http://www.zhihu.com/question/21778055/answer/19905413?utm_source=weibo&utm_medium=weibo_share&utm_content=share_answer&utm_campaign=share_button\",\"used\":true,\"objectId\":\"55c35bc960b2750766954ec3\",\"createdAt\":\"2015-08-06T13:06:17.211Z\",\"updatedAt\":\"2015-08-15T03:15:54.872Z\"}]},\"category\":[\"iOS\",\"Android\",\"瞎推荐\",\"拓展资源\",\"福利\",\"休息视频\"]}";

}