package com.hc.request.business;

import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.hc.model.BaseResponse;
import com.hc.request.Config;
import com.hc.request.core.CallServer;
import com.hc.request.core.DefaultCallback;
import com.hc.request.core.IHttpCallback;
import com.hc.request.core.parse.FastJsonRequest;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.rest.Request;

/**
 * 浩传接口请求服务
 */
public class UserService {

    /**
     * 查询素材是否已经收藏
     *
     * @param uId      用户ID
     * @param sourceId 素材ID
     * @param callback 请求回调
     */
    public void isCollect(String uId, String sourceId, final IHttpCallback<Boolean> callback) {
        Request<BaseResponse<Object>> request = new FastJsonRequest<>(Config.URL.IS_COLLECT, Object.class);
        request.add(Config.PARAMS.UID, uId);
        request.add(Config.PARAMS.SOURCE_ID, sourceId);

        CallServer.getInstance().request(Config.WHAT.IS_COLLECT, request, new DefaultCallback<BaseResponse<Object>>() {
            @Override
            public void onStart(int what) {
                Logger.d("开始查询是否收藏");
                if (callback != null) {
                    callback.onStart(what);
                }
            }

            @Override
            public void onSuccess(int what, @NonNull BaseResponse data) {
                Logger.d("查询是否收藏成功:" + JSON.toJSONString(data));
                if (callback != null) {
                    callback.onSuccess(what, Config.RESPONSE.CODE_HAD_COLLECT.equals(data.getCode()));
                }
            }

            @Override
            public void onFail(int what, String msg) {
                Logger.d("查询是否收藏失败:" + msg);
            }

            @Override
            public void onError(int what, Throwable e) {
                Logger.e(e, "查询是否收藏发生异常");
            }

            @Override
            public void onFinish(int what) {
                Logger.d("查询是否收藏请求结束");
                if (callback != null) {
                    callback.onFinish(what);
                }
            }
        });
    }

    /**
     * 添加观看历史，我的收藏
     *
     * @param uId      用户ID
     * @param sourceId 素材ID
     * @param callback 请求回调
     */
    public void collect(String uId, String sourceId, final IHttpCallback<BaseResponse> callback) {
        Request<BaseResponse<Object>> request = new FastJsonRequest<>(Config.URL.COLLECT, Object.class);
        request.add(Config.PARAMS.TYPE, Config.VALUES.COLLECT_TYPE_COLLECTION);
        request.add(Config.PARAMS.UID, uId);
        request.add(Config.PARAMS.SOURCE_ID, sourceId);
        CallServer.getInstance().request(1, request, new DefaultCallback<BaseResponse<Object>>() {
            @Override
            public void onStart(int what) {
                Logger.d("开始请求收藏");
                if (callback != null) {
                    callback.onStart(what);
                }
            }

            @Override
            public void onSuccess(int what, @NonNull BaseResponse data) {
                Logger.d("请求收藏成功:" + JSON.toJSONString(data));
                if (callback != null) {
                    if (Config.RESPONSE.CODE_COLLECT_SUCCESS.equals(data.getCode())) {
                        callback.onSuccess(what, data);
                    } else {
                        callback.onFail(what, data.getMsg());
                    }
                }
            }

            @Override
            public void onFail(int what, String msg) {
                Logger.d("请求收藏失败:" + msg);
                if (callback != null) {
                    callback.onFail(what, msg);
                }
            }

            @Override
            public void onError(int what, Throwable e) {
                Logger.e(e, "请求收藏发生异常");
                if (callback != null) {
                    callback.onError(what, e);
                }
            }

            @Override
            public void onFinish(int what) {
                Logger.d("收藏请求结束");
                if (callback != null) {
                    callback.onFinish(what);
                }
            }
        });
    }

    /**
     * 删除观看历史，我的收藏
     *
     * @param uId      用户ID
     * @param sourceId 素材ID
     * @param callback 请求回调
     */
    public void deleteCollection(String uId, String sourceId, final IHttpCallback<BaseResponse> callback) {
        Request<BaseResponse<Object>> request = new FastJsonRequest<>(Config.URL.COLLECT_CANCEL, Object.class);
        request.add(Config.PARAMS.TYPE, Config.VALUES.COLLECT_TYPE_COLLECTION);
        request.add(Config.PARAMS.UID, uId);
        request.add(Config.PARAMS.SOURCE_ID, sourceId);
        CallServer.getInstance().request(Config.WHAT.COLLECT_CANCEL, request, new DefaultCallback<BaseResponse<Object>>() {
            @Override
            public void onStart(int what) {
                Logger.d("开始请求取消收藏");
                if (callback != null) {
                    callback.onStart(what);
                }
            }

            @Override
            public void onSuccess(int what, @NonNull BaseResponse data) {
                Logger.d("请求取消收藏成功:" + JSON.toJSONString(data));
                if (callback != null) {
                    if (Config.RESPONSE.CODE_COLLECT_SUCCESS.equals(data.getCode())) {
                        callback.onSuccess(what, data);
                    } else {
                        callback.onFail(what, data.getMsg());
                    }
                }
            }

            @Override
            public void onFail(int what, String msg) {
                Logger.d("请求取消收藏失败:" + msg);
                if (callback != null) {
                    callback.onFail(what, msg);
                }
            }

            @Override
            public void onError(int what, Throwable e) {
                Logger.e(e, "请求取消收藏发生异常");
                if (callback != null) {
                    callback.onError(what, e);
                }
            }

            @Override
            public void onFinish(int what) {
                Logger.d("取消收藏请求结束");
                if (callback != null) {
                    callback.onFinish(what);
                }
            }
        });
    }
}
