package com.fs.wxwork.service;

import com.alibaba.fastjson.TypeReference;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.exception.CustomException;
import com.fs.ipad.param.WxGetSessionRoomListParam;
import com.fs.ipad.param.WxRoomUserListParam;
import com.fs.ipad.param.WxSendAtMsgParam;
import com.fs.ipad.vo.WxGetSessionRoomListVo;
import com.fs.ipad.vo.WxRoomUserListVo;
import com.fs.ipad.vo.WxSendAtMsgVo;
import com.fs.ipad.vo.WxSendTextAtMsgVo;
import com.fs.qw.domain.QwIpadServer;
import com.fs.qw.service.IQwIpadServerService;
import com.fs.wxwork.dto.*;
import com.fs.wxwork.utils.WxWorkHttpUtil;
import com.fs.wxwork.utils.WxWorkHttpUtilNew;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@AllArgsConstructor
public class WxWorkServiceNew {
    /**
     * 基础地址
     */
    private final RedisCache redisCache;
    private final IQwIpadServerService qwIpadServerService;

    public String getUrl(Long serverId) {
        String url = redisCache.getCacheObject("serverId:" + serverId);
        if (url != null && !url.isEmpty()) {
            return url;
        }
        log.info("serverId:" + serverId);
        QwIpadServer qwIpadServer = qwIpadServerService.selectQwIpadServerById(serverId);
        if (qwIpadServer == null||qwIpadServer.getUrl()==null) {
            throw new CustomException("未获取到服务地址与端口");
        }
        redisCache.setCacheObject("serverId:" + serverId,qwIpadServer.getUrl(),2, TimeUnit.HOURS);
        return qwIpadServer.getUrl();
    }

    public WxWorkResponseDTO<WxWorkInitRespDTO> init(WxWorkInitDTO param,Long serverId) {
        String url = getUrl(serverId) + "/init";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxWorkInitRespDTO>>() {
        }, serverId);
    }

    public WxWorkResponseDTO<WxWorkSendTextMsgRespDTO> SendTextMsg(WxWorkSendTextMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendTextMsg";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxWorkSendTextMsgRespDTO>>() {
        }, serverId);
    }


    public WxWorkResponseDTO<WxWorkSendAppMsgRespDTO> SendAppMsg(WxWorkSendAppMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendAppMsg";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxWorkSendAppMsgRespDTO>>() {
        }, serverId);
    }


    public WxWorkResponseDTO<WxwSendLinkMsgRespDTO> SendLinkMsg(WxwSendLinkMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendLinkMsg";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwSendLinkMsgRespDTO>>() {
        }, serverId);
    }

    public WxWorkResponseDTO<WxwSendCDNImgMsgRespDTO> SendCDNImgMsg(WxwSendCDNImgMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendCDNImgMsg";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwSendCDNImgMsgRespDTO>>() {}, serverId);

    }

    public WxWorkResponseDTO<WxwSendCDNFileMsgRespDTO> SendCDNFileMsg(WxwSendCDNFileMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendCDNFileMsg";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwSendCDNFileMsgRespDTO>>() {}, serverId);
    }

    public WxWorkResponseDTO<WxwSendCDNVoiceMsgRespDTO> SendCDNVoiceMsg(WxwSendCDNVoiceMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendCDNVoiceMsg";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwSendCDNVoiceMsgRespDTO>>() {}, serverId);
    }

    public WxWorkResponseDTO<WxwSendCDNVideoMsgRespDTO> SendCDNVideoMsg(WxwSendCDNVideoMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendCDNVideoMsg";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwSendCDNVideoMsgRespDTO>>() {}, serverId);
    }


    public WxWorkResponseDTO<WxwSendNoticeMsgRespDTO> SendNotice(WxwSendNoticeMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendNotice";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwSendNoticeMsgRespDTO>>() {}, serverId);
    }

    public WxWorkResponseDTO<WxwSendVideoNumberRespDTO> SendVideoNumber(WxwSendVideoNumberMsgDTO param,Long serverId) {
        String url = getUrl(serverId) + "/SendVideoNumber";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwSendVideoNumberRespDTO>>() {}, serverId);
    }

    public String getCorpId(String uuid, Long corpId,Long serverId) {
        String sCorpId = redisCache.getCacheObject("ipad:corpId:" + corpId);
        if (sCorpId != null&& !sCorpId.isEmpty()) {

            return sCorpId;
        }
        String url = getUrl(serverId) + "/GetCorpInfo";
        WxWorkCorpDTO param = new WxWorkCorpDTO();
        param.setUuid(uuid);
        param.setCorpid(corpId);

        WxWorkResponseDTO<WxWorkCorpRespDTO> listWxWorkResponseDTO = WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxWorkCorpRespDTO>>() {}, serverId);
        WxWorkCorpRespDTO data = listWxWorkResponseDTO.getData();
        List<WxWorkCorpRespDTO.CorpInfo> list = data.getList();
        if (list != null && !list.isEmpty()) {

            WxWorkCorpRespDTO.CorpInfo corpInfo = list.get(0);


            String scorpId = corpInfo.getScorp_id();

            if (scorpId!=null&&!scorpId.isEmpty()) {
                redisCache.setCacheObject("ipad:corpId:" + corpId,scorpId);

                return scorpId;
            }
        }

        return "";
    }

    public WxWorkResponseDTO<WxwUploadCdnLinkFileRespDTO> uploadCdnLinkFile(WxwUploadCdnLinkFileDTO param, Long serverId) {

        String url = getUrl(serverId) + "/UploadCdnLinkFile";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxwUploadCdnLinkFileRespDTO>>() {}, serverId);
    }

    public WxWorkResponseDTO<List<WxWorkVid2UserIdRespDTO>> UserId2Vid(WxWorkUserId2VidDTO param,Long serverId) {
        String url = getUrl(serverId) + "/UserId2Vid";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<List<WxWorkVid2UserIdRespDTO>>>() {}, serverId);
    }


    public WxWorkResponseDTO<WxWorkChatIdToRoomIdResp> ChatIdToRoomId(WxWorkChatIdToRoomIdDTO param, Long serverId) {
        String url = getUrl(serverId) + "/ChatIdToRoomId";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxWorkChatIdToRoomIdResp>>() {}, serverId);
    }

    /**
     * CDN上传网络图片
     * @param param     参数
     * @param serverId  服务器ID
     * @return  WxWorkResponseDTO
     */

    public WxWorkResponseDTO<WxCdnUploadImgLinkResp> cdnUploadImgLink(WxCdnUploadImgLinkDTO param, Long serverId) {
        String url = getUrl(serverId) + "/CdnUploadImgLink";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxCdnUploadImgLinkResp>>() {}, serverId);
    }
    /**
     * CDN上传网络视频
     * @param param     参数
     * @param serverId  服务器ID
     * @return  WxWorkResponseDTO
     */

    public WxWorkResponseDTO<WxCdnUploadVideoResp> uploadCdnVideoLink(WxCdnUploadVideoLinkDTO param, Long serverId) {
        String url = getUrl(serverId) + "/UploadCdnVideoLink";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxCdnUploadVideoResp>>() {}, serverId);
    }

    /**
     * 上传大文件
     * @param param     参数
     * @param serverId  服务器ID
     * @return  WxWorkResponseDTO
     */

    public WxWorkResponseDTO<WxBigFileUploadLinkResp> bigFileUploadLink(WxBigFileUploadLinkDTO param, Long serverId) {
        WGetBigAuthkeyDTO dto = new WGetBigAuthkeyDTO();
        dto.setUuid(param.getUuid());
        WxWorkResponseDTO<WxGetBigAuthkeyResp> authkey = getBigAuthkey(dto, serverId);
        param.setAuthkey(authkey.getData().getAuthKey());
        param.setFilekey(authkey.getData().getFilekey());
        String url = getUrl(serverId) + "/BigFileUploadLink";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxBigFileUploadLinkResp>>() {}, serverId);
    }


    public WxWorkResponseDTO<WxLoginResp> isLogin(WxLoginDTO param, Long serverId) {
        String url = getUrl(serverId) + "/GetRunClientByUuid";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxLoginResp>>() {}, serverId);
    }

    /**
     * 获取大文件授权key
     * @param param     参数
     * @param serverId  服务器ID
     * @return  WxWorkResponseDTO
     */
    public WxWorkResponseDTO<WxGetBigAuthkeyResp> getBigAuthkey(WGetBigAuthkeyDTO param, Long serverId) {
        String url = getUrl(serverId) + "/GetBigAuthkey";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxGetBigAuthkeyResp>>() {}, serverId);
    }

    public void LoginOut(WxWorkGetQrCodeDTO dto, Long serverId) {
        String url = getUrl(serverId) + "/LoginOut";
        WxWorkHttpUtil.postWithType(url, dto, new TypeReference<WxWorkResponseDTO<WxwLoginOutRespDTO>>() {});
    }

    public WxWorkResponseDTO<WxGetSessionRoomListVo> getSessionRoomList(WxGetSessionRoomListParam param, Long serverId) {
        String url = getUrl(serverId) + "/GetSessionRoomList";
        return WxWorkHttpUtil.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxGetSessionRoomListVo>>() {});
    }

    public WxWorkResponseDTO<WxRoomUserListVo> getRoomUserList(WxRoomUserListParam param, Long serverId) {
        String url = getUrl(serverId) + "/GetRoomUserList";
        return WxWorkHttpUtil.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxRoomUserListVo>>() {});
    }

    public WxWorkResponseDTO<WxSendAtMsgVo> sendTextAtMsg(WxSendAtMsgParam param, Long serverId) {
        String url = getUrl(serverId) + "/SendTextAtMsg";
        return WxWorkHttpUtil.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxSendAtMsgVo>>() {});
    }

    public WxWorkResponseDTO<WxSendTextAtMsgVo> sendTextAtMsgTwo(WxSendTextAtMsgTwoDTO param, Long serverId) {
        String url = getUrl(serverId) + "/SendTextAtMsgTwo";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxSendTextAtMsgVo>>() {},serverId);
    }

    public WxWorkResponseDTO<WxCreateRoomRespDTO> createRoom(WxCreateRoomMsgDTO param, Long serverId) {
        String url = getUrl(serverId) + "/CreateRoomWx";
        return WxWorkHttpUtil.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxCreateRoomRespDTO>>() {});
    }


    public WxWorkResponseDTO<WxWorkChatIdToRoomIdResp> RoomIdToChatId(WxWorkChatIdToRoomIdDTO param, Long serverId) {
        String url = getUrl(serverId) + "/RoomIdToChatId";
        return WxWorkHttpUtilNew.postWithType(url, param, new TypeReference<WxWorkResponseDTO<WxWorkChatIdToRoomIdResp>>() {}, serverId);
    }
}
