package com.unis.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.unis.model.bean.notice.NoticeBean;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 通知请求类
 * 创建人: mQ
 * 时间: 2020/12/26 13:45
 */
public class NoticeModel {
    private NoticeModel() {
    }

    private static final class SingleHolder {
        private static final NoticeModel INSTANCE = new NoticeModel();
    }

    public static NoticeModel get() {
        return SingleHolder.INSTANCE;
    }

    /**
     * 获取通知列表
     */
    public void getList(int page, final DataCallback<List<NoticeBean>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", page);
        map.put("pageSize", 10);
        OkGo.<String>post(UrlPath.notice_list)
                .upJson(new Gson().toJson(map))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String json = getDataJson(response);
                        JSONObject object = new JSONObject(json);
                        JSONArray jsonArray = object.getJSONArray("records");
                        if (jsonArray == null || jsonArray.length() == 0) {
                            callback.onSuccess(new ArrayList<NoticeBean>());
                            return;
                        }
                        List<NoticeBean> list =
                                new Gson().fromJson(jsonArray.toString(),
                                        new TypeToken<List<NoticeBean>>() {
                                        }.getType());
                        callback.onSuccess(list);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callback.onError(getErrorMsg(response));
                    }
                });

    }


    /**
     * 获取通知列表
     */
    public void getNoticeCount(final DataCallback<Integer> callback) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("readStatus", false);
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", 1);
        map.put("pageSize", 10);
        map.put("condition", new Gson().toJson(condition));
        OkGo.<String>post(UrlPath.notice_list)
                .upJson(new Gson().toJson(map))
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String json = getDataJson(response);
                        JSONObject object = new JSONObject(json);
                        int total = object.optInt("total", 0);
                        callback.onSuccess(total);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callback.onError(getErrorMsg(response));
                    }
                });

    }

    /**
     * 通知标记已读
     *
     * @param id
     * @param callback
     */
    public void read(String id, final DataCallback<String> callback) {
        GetRequest<String> request = OkGo.get(UrlPath.notice_read + "/" + id);
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                callback.onSuccess(null);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callback.onError(getErrorMsg(response));
            }
        });
    }

}
