package com.gh.gamecenter.manager;

import android.content.Context;

import com.gh.common.constant.Config;
import com.gh.gamecenter.core.utils.StringUtils;
import com.gh.gamecenter.common.utils.TimestampUtils;
import com.gh.gamecenter.common.retrofit.ObservableUtil;
import com.gh.gamecenter.common.retrofit.OkHttpCache;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * Created by khy on 2016/11/17.
 */
public class CommentManager {
    private List<String> urlList;

    private CommentManager() {
        urlList = new ArrayList<>();
    }

    // 更新okhttp缓存数据 修改资讯关注列表评论数 ids
    public static void updateOkhttpCache(final Context context, final String comment_id) {
        ObservableUtil.io(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                String key = CommentManager.getInstance().getUrl(comment_id);
                if (key == null) {
                    return;
                }
                key = TimestampUtils.addTimestamp(key);
                byte[] data = OkHttpCache.getCache(context, key);
                if (data != null) {
                    try {
                        JSONArray jsonArray = new JSONArray(new String(data));
                        JSONObject jsonObject;
                        for (int i = 0, size = jsonArray.length(); i < size; i++) {
                            jsonObject = jsonArray.getJSONObject(i);
                            if (jsonObject.getString("_id").equals(comment_id)) {
                                jsonObject.put("num", jsonObject.getInt("num") + 1);
                                break;
                            }
                        }
                        OkHttpCache.updateCache(context, key, jsonArray.toString().getBytes());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, null);
    }

    public String getUrl(String id) {
        for (int i = 0, size = urlList.size(); i < size; i++) {
            if (urlList.get(i).contains(id)) {
                return urlList.get(i);
            }
        }
        return null;
    }

    public static CommentManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    // 修改新闻详情评论数 id
    public static void updateOkhttpCacheForId(final Context context, final String comment_id) {
        ObservableUtil.io(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                String key = TimestampUtils.addTimestamp(StringUtils.buildString(Config.API_HOST, "article/", comment_id, "/comment/num"));
                byte[] data = OkHttpCache.getCache(context, key);
                if (data != null) {
                    try {
                        JSONArray jsonArray = new JSONArray(new String(data));
                        JSONObject jsonObject;
                        for (int i = 0, size = jsonArray.length(); i < size; i++) {
                            jsonObject = jsonArray.getJSONObject(i);
                            if (jsonObject.getString("_id").equals(comment_id)) {
                                jsonObject.put("num", jsonObject.getInt("num") + 1);
                                break;
                            }
                        }
                        OkHttpCache.updateCache(context, key, jsonArray.toString().getBytes());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, null);
    }

    public void addUrl(String ids) {
        String url = StringUtils.buildString(Config.API_HOST, "article/", ids, "/comment/num");
        if (!urlList.contains(url)) {
            urlList.add(url);
        }
    }

    private static class SingletonHolder {
        private static final CommentManager INSTANCE = new CommentManager();
    }
}
