package com.fs.im.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.fs.common.config.FSConfig;
import com.fs.his.config.FsSysConfig;
import com.fs.his.utils.ConfigUtil;
import com.fs.im.dto.*;
import com.fs.im.service.IImService;
import com.tencentyun.TLSSigAPIv2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class ImServiceImpl implements IImService {
    String account="system";//管理员帐号
    @Autowired
    ConfigUtil configUtil;
    @Override
    public MsgResponseDTO sendMsg(MsgDTO msg) {
        int random = (int)(Math.random()*10000000);
        msg.setMsgRandom( random);
        FsSysConfig fsConfig = configUtil.getSysConfig();
        TLSSigAPIv2 api = new TLSSigAPIv2(fsConfig.getSdkAppId(), fsConfig.getSdkAppKey());
        String sign=api.genUserSig(account,180*86400);
        String url="https://console.tim.qq.com/v4/openim/sendmsg?sdkappid="+fsConfig.getSdkAppId()+"&identifier="+account+"&usersig="+sign+"&random="+random+"&contenttype=json";
        // 添加请求头信息
        Map<String, String > heads = new HashMap<>();
        // 使用json发送请求，下面的是必须的
        heads.put("Content-Type", "application/json;charset=UTF-8");
        HttpResponse response = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(String.valueOf(JSONUtil.toJsonStr(msg)))
                .timeout(5 * 60 * 1000)
                .execute();

        MsgResponseDTO responseDTO=JSONUtil.toBean(response.body(),MsgResponseDTO.class);
        return responseDTO;

    }

    @Override
    public AccountImportResultDTO accountImport(AccountImportDTO dto) {
        int random = (int)(Math.random()*10000000);

        FsSysConfig fsConfig = configUtil.getSysConfig();
        TLSSigAPIv2 api = new TLSSigAPIv2(fsConfig.getSdkAppId(), fsConfig.getSdkAppKey());
        String sign=api.genUserSig(account,180*86400);
        String url="https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid="+fsConfig.getSdkAppId()+"&identifier="+account+"&usersig="+sign+"&random="+random+"&contenttype=json";
        // 添加请求头信息
        Map<String, String > heads = new HashMap<>();
        // 使用json发送请求，下面的是必须的
        heads.put("Content-Type", "application/json;charset=UTF-8");
        HttpResponse response = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(String.valueOf(JSONUtil.toJsonStr(dto)))
                .timeout(5 * 60 * 1000)
                .execute();

        AccountImportResultDTO responseDTO=JSONUtil.toBean(response.body(),AccountImportResultDTO.class);
        return responseDTO;
    }

    @Override
    public AccountCheckResultDTO accountCheck(AccountCheckDTO dto) {
        int random = (int)(Math.random()*10000000);

        FsSysConfig fsConfig = configUtil.getSysConfig();
        TLSSigAPIv2 api = new TLSSigAPIv2(fsConfig.getSdkAppId(), fsConfig.getSdkAppKey());
        String sign=api.genUserSig(account,180*86400);
        String url="https://console.tim.qq.com/v4/im_open_login_svc/account_check?sdkappid="+fsConfig.getSdkAppId()+"&identifier="+account+"&usersig="+sign+"&random="+random+"&contenttype=json";
        // 添加请求头信息
        Map<String, String > heads = new HashMap<>();
        // 使用json发送请求，下面的是必须的
        heads.put("Content-Type", "application/json;charset=UTF-8");
        HttpResponse response = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(String.valueOf(JSONUtil.toJsonStr(dto)))
                .timeout(5 * 60 * 1000)
                .execute();

        AccountCheckResultDTO responseDTO=JSONUtil.toBean(response.body(),AccountCheckResultDTO.class);
        return responseDTO;
    }

    @Override
    public void addAccount(Long userId,String faceUrl,String nick) {
        AccountCheckDTO accountCheckDTO = new AccountCheckDTO();
        AccountCheckDTO.UserItem userItem = new AccountCheckDTO.UserItem();
        userItem.setUserID("U-"+userId);
        accountCheckDTO.setCheckItem(Arrays.asList(userItem));
        AccountCheckResultDTO accountCheckResultDTO = accountCheck(accountCheckDTO);
        if (accountCheckResultDTO.getErrorCode()==0){
            List<AccountCheckResultDTO.ResultItem> resultItem = accountCheckResultDTO.getResultItem();
            if (resultItem!=null&&resultItem.size()==1){
                AccountCheckResultDTO.ResultItem re = resultItem.get(0);
                if (re.getAccountStatus().equals("NotImported")){
                    AccountImportDTO accountImportDTO = new AccountImportDTO();
                    accountImportDTO.setFaceUrl(faceUrl);
                    accountImportDTO.setUserID("U-"+userId);
                    accountImportDTO.setNick(nick);
                    AccountImportResultDTO accountImportResultDTO = accountImport(accountImportDTO);
                }
            }
        }
    }
}
