package com.fs.qw.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fs.common.core.domain.R;
import com.fs.qw.config.QwConfig;
import com.fs.qw.domain.*;
import com.fs.qw.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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


import static com.fs.qw.utils.QwUtil.sendQwHttp;

@SuppressWarnings("all")
public class QwAllUtil {
    private static final Logger logger= LoggerFactory.getLogger("QwAllUtil");
    /**
     * 下载文件
     */
    public static String cdnDownload(Map<String,Object> map,String deviceId){
        String json = sendQwHttp(QwConfig.CDN_DOWNLOAD,map,deviceId);
        return json;
    }

    /**
     * 生成登录二维码
     */
    public static QwLoginVO getqrCode( String deviceId){
        Map<String,Object> map = new HashMap<>();
        String data = common(QwConfig.AUTH_QRCODE,map,deviceId);
        return GsonFromJson(data,QwLoginVO.class);
    }

    /**
     * 登录验证
     */
    public static R verify(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.AUTH_VERIFY,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error(qwRestful.getMessage());
        }else {
            return R.ok(qwRestful.getMessage());
        }
    }

    /**
     * 注销登录
     */
    public static R ban ( String deviceId){
        Map<String,Object> map = new HashMap<>();
        QwRestful qwRestful = common2(QwConfig.AUTH_BAN,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     * 检查账号是否在线
     */
    public static QwOnline check( String deviceId){
        Map<String,Object> map = new HashMap<>();
        String url=QwConfig.AUTH_CHECK;
        String data = common(QwConfig.AUTH_CHECK,map,deviceId);
        return GsonFromJson(data,QwOnline.class);
    }

    /**
     * 获取在线设备
     */
    public static QwDevice authDevice( String deviceId){
        Map<String,Object> map = new HashMap<>();
        String data = common(QwConfig.AUTH_DEVICE,map,deviceId);
        return GsonFromJson(data,QwDevice.class);
    }

    /**
     * 获取联系人列表
     */
    public static QwInnerContactReturn innerContactList(String seq,String deviceId){
        Map<String,Object> map = new HashMap<>();
        map.put("seq",seq);
        String data = common(QwConfig.INNER_CONTACT_LIST,map,deviceId);
        return JSON.parseObject(data, QwInnerContactReturn.class);
    }

    /**
     * 获取联系人列表
     */
    public static QwContactReturn contactList(Long seq,String deviceId){
        Map<String,Object> map = new HashMap<>();
        map.put("seq",seq);
        String data = common(QwConfig.CONTACT_LIST,map,deviceId);
        return JSON.parseObject(data, QwContactReturn.class);
    }

    /**
     * 获取联系人详情
     */
    public static QwContactVO contactDetail(Long userId, String deviceId){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        String data = common(QwConfig.CONTACT_DETAIL,map,deviceId);
        return GsonFromJson(data,QwContactVO.class);
    }

    /**
     * 搜索联系人
     */
    public static QwContactSearch contactSearch(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.CONTACT_SEARCH,map,deviceId);
        return GsonFromJson(data,QwContactSearch.class);
    }

    /**
     * 添加联系人
     */
    public static R contactAdd(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.CONTACT_ADD,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     * 删除联系人
     */
    public static R contactDel(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.CONTACT_DELETE,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     *同意好友申请
     */
    public static R contactAgreeFriend(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.CONTACT_AF,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }
    /**
     * 发送文本消息
     */
    public static R messageText(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_TEXT,map,deviceId);
        QwMessageReturn messageReturn = GsonFromJson(data,QwMessageReturn.class);
        if (messageReturn == null){
            return R.error(data);
        }else {
            return R.ok().put("QwSendMessageText",messageReturn);
        }
    }
    /**
     * 群列表
     */
    public static QwRoomReturn roomList(Long seq,String deviceId){
        Map<String,Object> map = new HashMap<>();
        map.put("seq",seq);
        String data = common(QwConfig.ROOM_LIST,map,deviceId);
        return JSON.parseObject(data, QwRoomReturn.class);
    }
    /**
     * 群详情
     */
    public static QwRoomListVO roomDetail(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.ROOM_DETAIL,map,deviceId);
        return GsonFromJson(data,QwRoomListVO.class);
    }

    /**
     *发送图片
     */
    public static R messageImage(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_IMAGE,map,deviceId);
        QwMessageReturn messageReturn = GsonFromJson(data,QwMessageReturn.class);
        if (messageReturn == null){
            return R.error(data);
        }else {
            return R.ok().put("QwSendMessageImage",messageReturn);
        }
    }

    /**
     *发送语音
     */
    public static R messageVoice(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_VOICE,map,deviceId);
        QwMessageReturn messageReturn = GsonFromJson(data,QwMessageReturn.class);
        if (messageReturn == null){
            return R.error(data);
        }else {
            return R.ok().put("QwSendMessageVoice",messageReturn);
        }
    }

    /**
     *发送文件消息
     */
    public static QwMessageReturn messageFile(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_FILE,map,deviceId);
        return GsonFromJson(data, QwMessageReturn.class);
    }
    /**
     * 发送链接消息
     */
    public static QwMessageReturn messageLink(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_LINK,map,deviceId);
        return GsonFromJson(data, QwMessageReturn.class);
    }
    /**
     * 发送小程序消息
     */
    public static QwMessageReturn messageApp(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_APP,map,deviceId);
        return GsonFromJson(data, QwMessageReturn.class);
    }
    /**
     * 发送视频消息
     */
    public static QwMessageReturn messageVideo(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_VIDEO,map,deviceId);
        return GsonFromJson(data, QwMessageReturn.class);
    }
    /**
     * 撤回消息
     */
    public static R messageRevoke(QwMessageRevoke qwMessageRevoke, String deviceId){
        Map<String,Object> map = qwMessageRevoke.toMap();
        QwRestful qwRestful = common2(QwConfig.MESSAGE_REVOKE,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }
    /**
     * 建群
     */
    public static QwRoomCreateVO roomCreate(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.ROOM_CREATE,map,deviceId);
        return GsonFromJson(data,QwRoomCreateVO.class);
    }

    /**
     * 修改群名称
     */
    public static R roomChangeName(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_CHANGENAME,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     * 添加群成员
     */
    public static R addRoomMembers(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_ADDMEMBERS,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     *移除群成员
     */
    public static R removeMembers(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_REMOVEMEMBERS,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     * 添加群成员为好友
     */
    public static R roomAddFriend(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_ADDFRIEND,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     * 生成群二维码
     */
    public static QwRoomqrcodeVO roomqrcode(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.ROOM_QRCODE,map,deviceId);
        return GsonFromJson(data,QwRoomqrcodeVO.class);
    }

    /**
     * 修改群公告
     */
    public static R roomNotic(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_NOTICE,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     *退出群
     */
    public static R roomExit(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_EXIT,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     *删除群
     */
    public static R roomDelete(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_DELETE,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     * 转让群
     */
    public static R roomChangeOwner(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.ROOM_CHANGEOWNER,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     *获取标签列表
     */
    public static List<QwLabelVO> labelList( String deviceId){
        Map<String,Object> map = new HashMap<>();
        String data = common(QwConfig.LABEL_LIST,map,deviceId);
        return JSON.parseObject(data, new TypeReference<List<QwLabelVO>>(){});
    }

    /**
     * 创建标签
     */
    public static QwLabelVO labelCreate(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.LABEL_CREATE,map,deviceId);
        return GsonFromJson(data,QwLabelVO.class);
    }
    /**
     * 删除标签
     */
    public static R labelDelete(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.LABEL_DELETE,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }

    /**
     * 修改标签
     */
    public static QwLabelVO labelUpdate(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.LABEL_UPDATE,map,deviceId);
        return GsonFromJson(data,QwLabelVO.class);
    }

    /**
     * 设置用户标签
     */
    public static R labelAssign(Map<String,Object> map, String deviceId){
        QwRestful qwRestful = common2(QwConfig.LABEL_ASSIGN,map,deviceId);
        if (!qwRestful.getCode().equals("0")){
            return R.error();
        }else {
            return R.ok();
        }
    }


    /**
     * 公共方法有返回值
     */
    public static String common(String url,Map<String,Object> map,String deviceId){
        String json = sendQwHttp(url,map,deviceId);
//        QwRestful qwRestful = GsonFromJson(json,QwRestful.class);
        QwRestful qwRestful = GsonFromJson(json, QwRestful.class);
        if (qwRestful!=null){
            if ("0".equals(qwRestful.getCode())) {
                return JSON.toJSONString(qwRestful.getData());
            }
            return qwRestful.getMessage();
        }
        logger.error("请求接口错误：{}",json);
        return json;
    }

    /**
     * 公共方法无返回值
     */
    public static QwRestful common2(String url,Map<String,Object> map,String deviceId){
        String json = sendQwHttp(url,map,deviceId);
        QwRestful restful =  GsonFromJson(json,QwRestful.class);
        if (restful!=null){
            if (!"0".equals(restful.getCode())) {
                logger.error("请求接口错误：{}",restful.getMessage());
            }
        }
        logger.error("请求接口错误：{}",json);
        return restful;
    }

    public static <T> T GsonFromJson(String json, Class<T> classOfT) {
        return JSON.parseObject(json, classOfT);
    }


}
