package com.xiaoshangxing.network.netUtil;

import com.google.gson.JsonObject;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.friend.FriendService;
import com.xiaoshangxing.data.TempUser;
import com.xiaoshangxing.network.callBack.DefaultCallback;
import com.xiaoshangxing.network.callBack.SimpleCallback;
import com.xiaoshangxing.network.netCollection.IMNet;
import com.xiaoshangxing.utils.baseClass.BaseView;
import com.xiaoshangxing.utils.normalUtils.TimeUtils;
import com.xiaoshangxing.yujian.im.cache.FriendDataCache;

import org.json.JSONObject;

/**
 * Created by FengChaoQun
 * on 2017/1/11
 */

public class IMUtils {
    /**
     * description:留心某人
     *
     * @param account  留心对象的id
     * @param callback 回调
     */

    public static void Favor(String account, DefaultCallback callback) {

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(NormalKey.userId, TempUser.id);
        jsonObject.addProperty(NormalKey.oppositeUserId, account);
        jsonObject.addProperty(NormalKey.timeStamp, TimeUtils.currentTime());

        IMNet.Favor(jsonObject, callback);
    }

    public static void CancleCrush(String account, BaseView baseView, SimpleCallback callback) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(NormalKey.userId, TempUser.id);
        jsonObject.addProperty(NormalKey.oppositeUserId, account);
        jsonObject.addProperty(NormalKey.category, "1");

        IMNet.CancelCrush(jsonObject, baseView, callback);
    }

    public static void Crush(String account, BaseView baseView, SimpleCallback callback) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(NormalKey.userId, TempUser.getId());
        jsonObject.addProperty(NormalKey.oppositeUserId, account);
        jsonObject.addProperty(NormalKey.category, "1");
        jsonObject.addProperty(NormalKey.timeStamp, TimeUtils.currentTime());
        IMNet.Crush(jsonObject, baseView, callback);
    }

    public static void getCrushBack(DefaultCallback callback) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(NormalKey.userId, TempUser.getId());
        jsonObject.addProperty(NormalKey.category, "1");
        IMNet.getCrushBack(jsonObject, callback);
    }

    /**
     * description:取消留心某人  如果是好友 则删除好友  (该方法也是校上行APP上删除好友的方法)
     *
     * @param account        对象id
     * @param simpleCallBack 回调
     */

    public static void CancelFavor(final String account, final BaseView baseView, final SimpleCallback simpleCallBack) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(NormalKey.userId, TempUser.id);
        jsonObject.addProperty(NormalKey.oppositeUserId, account);
        jsonObject.addProperty(NormalKey.category, "0");

        IMNet.CancelFavor(jsonObject, baseView, new SimpleCallback() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                if (FriendDataCache.getInstance().isMyFriend(account)) {
                    deleteFriend(account, baseView, simpleCallBack);
                } else {
                    simpleCallBack.onSuccess(null);
                }
            }

            @Override
            public void onFail(String code, String msg) {
                if (FriendDataCache.getInstance().isMyFriend(account)) {
                    deleteFriend(account, baseView, simpleCallBack);
                } else {
                    simpleCallBack.onFail(code, msg);
                }
            }

            @Override
            public void onError(Throwable throwable) {

            }
        });
    }

    /**
     * description:删除好友
     *
     * @param account        对方id
     * @param simpleCallBack 回调
     */

    public static void deleteFriend(String account, final BaseView baseView, final SimpleCallback simpleCallBack) {
        NIMClient.getService(FriendService.class).deleteFriend(account).setCallback(new RequestCallback<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                simpleCallBack.onSuccess(null);
            }

            @Override
            public void onFailed(int i) {
                baseView.showToast("删除失败:" + i);
            }

            @Override
            public void onException(Throwable throwable) {
                baseView.showToast("删除失败:异常");
                throwable.printStackTrace();
            }
        });
    }
}
