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.unis.model.bean.VersionBean;

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

import java.util.List;

/**
 * 包名: com.unis.model
 * 类名: VersionModel
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/25 9:24
 * 更新人:
 * 更新时间: 2020/12/25 9:24
 * 更新说明:
 * 版本号: 1.0
 */
public class VersionModel {
    private VersionModel() {
    }

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

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

    /**
     * 获取版本列表
     */
    public void getVersionInfo(final DataCallback<List<VersionBean>> callBack) {
        OkGo.<String>get(UrlPath.version_info)
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        JSONObject object = new JSONObject(response.body());
                        JSONArray info = object.optJSONArray("info");
                        if (info == null || info.length() == 0) {
                            callBack.onSuccess(null);
                            return;
                        }
                        List<VersionBean> list =
                                new Gson().fromJson(info.toString(),
                                        new TypeToken<List<VersionBean>>() {
                                        }.getType());
                        callBack.onSuccess(list);
                    }

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

}

