package com.socks.jiandan.net.parser;

import com.socks.jiandan.model.CommentNumber;
import com.socks.jiandan.utils.GsonHelper;
import com.socks.okhttp.plus.parser.OkBaseParser;

import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Response;

/**
 * Created by zhaokaiqiang on 15/11/22.
 */
public class CommentCountsParser extends OkBaseParser<ArrayList<CommentNumber>> {

    @Override
    public ArrayList<CommentNumber> parse(Response response) {

        if (!response.isSuccessful())
            return null;

        try {
            String body = response.body().string();
            JSONObject jsonObject = new JSONObject(body).getJSONObject("response");
            String[] comment_IDs = response.request().url().toString().split("\\=")[1].split("\\,");
            ArrayList<CommentNumber> commentNumbers = new ArrayList<>();

            GsonHelper<CommentNumber> helper = new GsonHelper<>();

            for (String comment_ID : comment_IDs) {
                if (!jsonObject.isNull(comment_ID)) {
                    JSONObject commentObject = jsonObject.getJSONObject(comment_ID);
                    CommentNumber commentNumber = helper.fromJson(commentObject.toString(), CommentNumber.class);
                    commentNumbers.add(commentNumber);
                } else {
                    //可能会出现没有对应id的数据的情况,为了保证条数一致,添加默认数据
                    commentNumbers.add(new CommentNumber("0", "0", 0));
                }
            }
            return commentNumbers;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}