package com.fs.qw;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.qw.config.QwConfig;
import com.fs.qw.domain.*;
import com.fs.qw.utils.QwDownloadUtil;
import com.fs.qw.vo.QwContactVO;
import com.fs.qw.vo.QwLabelVO;
import com.fs.qw.vo.QwRoomListVO;
import com.fs.system.oss.CloudStorageService;
import com.fs.system.oss.OSSFactory;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
//import net.sourceforge.pinyin4j.PinyinHelper;

import javax.xml.bind.DatatypeConverter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.fs.pay.pay.util.PayUtil.GsonFromJson;
import static com.fs.qw.utils.QwUtil.sendQwHttp;


public class test {
//    @Autowired
//    static IQwMaterialService qwMaterialService;

    public static void main(String[] args) throws Exception{
//        IQwMaterialService qwMaterialService = new QwMaterialServiceImpl();
//        Map<String,Object> map = new HashMap<>();
//        String deviceId = "4eea1292408044beb49c02d3197871b0";
//        List<QwLabelVO> list = labelList(map,deviceId);
//        System.out.println(list);
//        String nickName = "（）战国1";
//        String a = getFormattedNickname(nickName);
//        System.out.println(a);
        // 模拟收到的字节数组
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date());
        System.out.println(dateString);


//        QwCDNDownload cdnDownload = new QwCDNDownload();
//        cdnDownload.setFileId("30690201020462306002010002049674a89b02030f59b10204b616fa710204652e1e5a042433346439313937372d653261322d346535312d613462332d64356233373461663136613802010002030371500410caff6b67ef15aee6c89412b0002db3440201020201000400");
//        cdnDownload.setAesKey("776a13ac1e0b773ac91b529b3865b4d1");
//        cdnDownload.setFileSize(225616L);
//        cdnDownload.setMd5("caff6b67ef15aee6c89412b0002db344");
//        cdnDownload.setFileName(UUID.randomUUID().toString().replaceAll("-", "")+".jpg");
//        cdnDownload.setFileType(2);
//        Map<String,Object> map = cdnDownload.toMap();
//        byte[] fileBytes = cdnDownload(map,deviceId);
//        String suffix = cdnDownload.getFileName().substring(cdnDownload.getFileName().lastIndexOf("."));
//        CloudStorageService storage = OSSFactory.build();
//        String url = storage.uploadSuffix(fileBytes, suffix);
//        System.out.println(url);
//        Long id = 1L;
//        QwMaterial material = qwMaterialService.selectQwMaterialById(id);
//        QwMessageImage image = new QwMessageImage();
//        BeanUtils.copyProperties(image,material);
//        image.setReceiverId(7881300287915556L);
////        image.setIsRoom(false);
//        Map<String,Object> map = image.toMap();
//        QwMessage qwMessage = messageImage(map,deviceId);
//        System.out.println(qwMessage);

        //上传文件
        //群详情
//        Long roomId = 1970326754004217L;
//        map.put("roomId",roomId);
//        QwRoomListVO qwRoomListVO = roomDetail(map,deviceId);
        //联系人列表
//        List<QwContactsListVO> list = contactList(map,deviceId);
//        System.out.println(list);
        //获取联系人详情
//        Long userId = 7881300287915556L;
//        map.put("userId",userId);
//        QwContact qwContact = contactDetail(map,deviceId);
        //搜索联系人
//        String phone = "18323465069";
//        map.put("phone",phone);
//        QwContactSearch qwContactSearch = contactSearch(map,deviceId);
//        System.out.println(qwContactSearch);
        //添加联系人
//        Long userId = 7881300958990300L;
//        String openId = "orFrbsqo_CVEPvMlebav8JlYVpq4";
//        String hello = "我是张玉朋大帅哥";
//        map.put("userId",userId);
//        map.put("openId",openId);
//        map.put("hello",hello);
//        contactAdd(map,deviceId);
        //删除联系人
//        Long userId = 7881300958990300L;
//        map.put("userId",userId);
//        contactDel(map,deviceId);
        //同意好友申请
//        Long userId = 7881300958990300L;
//        Long corpId = 11111L;
//        map.put("userId",userId);
//        map.put("corpId",corpId);
//        R r = contactAgreeFriend(map,deviceId);
        //群列表
//        List<QwRoomListVO> list = roomList(map,deviceId);
        //发送文本消息
//        Long receiverId = 7881300287915556L;
//        Boolean room = false;
//        String content = "我是大帅哥";
//        map.put("receiverId",receiverId);
//        map.put("room",room);
//        map.put("content",content);
//        QwMessage message = messageText(map,deviceId);
//        System.out.println(map.get("content"));

    }

    public static String generateDownloadLink(byte[] fileData, String fileName) throws IOException {
        // 生成一个唯一的文件名
        String uniqueFileName = UUID.randomUUID().toString();

        // 将字节数组写入临时文件
        Path tempFilePath = Files.createTempFile(uniqueFileName, fileName);
        Files.write(tempFilePath, fileData, StandardOpenOption.CREATE);

        // 返回临时文件的下载链接
        return tempFilePath.toUri().toString();
    }
    public static String getFormattedNickname(String nickname) {
        if (nickname == null || nickname.isEmpty()) {
            return "";
        }
        char firstChar = nickname.charAt(0);
        if (isChineseChar(firstChar)) {
            //String pinyin = PinyinHelper.toHanyuPinyinStringArray(firstChar)[0];
            String pinyin ="PJYDSAF";
            return pinyin.substring(0, 1).toUpperCase();
        } else if (Character.isLetter(firstChar)) {
            return String.valueOf(Character.toUpperCase(firstChar));
        } else {
            return "#";
        }
    }

    // 判断是否为中文字符
    private static boolean isChineseChar(char c) {
        return Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN;
    }
    public static boolean decodeBase64ToImage(String base64String, String outputFilePath) {
        try {
            // 解码 Base64 字符串为字节数组
            byte[] imageBytes = DatatypeConverter.parseBase64Binary(base64String);

            // 创建文件输出流
            FileOutputStream outputStream = new FileOutputStream(outputFilePath);

            // 写入字节数组到输出流
            outputStream.write(imageBytes);

            // 关闭输出流
            outputStream.close();

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    public static byte[] cdnDownload(Map<String,Object> map,String deviceId){
        byte[] fileBytes = QwDownloadUtil.sendQwHttp(QwConfig.CDN_DOWNLOAD,map,deviceId);
        return fileBytes;
    }
    /**
     * 获取联系人列表
     */
    public static List<QwContactVO> contactList(Map<String,Object> map, String deviceId){
        Gson gson = new Gson();
        String data = common(QwConfig.CONTACT_LIST,map,deviceId);
        Type userListType = new TypeToken<List<QwContactVO>>() {}.getType();
        return gson.fromJson(data, userListType);
    }

    /**
     * 获取联系人详情
     */
    public static QwContact contactDetail(Map<String,Object> map,String deviceId){
        String data = common(QwConfig.CONTACT_DETAIL,map,deviceId);
        return GsonFromJson(data,QwContact.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 QwMessageReturn messageText(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_TEXT,map,deviceId);
        return GsonFromJson(data, QwMessageReturn.class);
    }
    /**
     * 群列表
     */
    public static List<QwRoomListVO> roomList(Map<String,Object> map, String deviceId){
        Gson gson = new Gson();
        String data = common(QwConfig.ROOM_LIST,map,deviceId);
        Type userListType = new TypeToken<List<QwRoomListVO>>() {}.getType();
        return gson.fromJson(data, userListType);
    }
    /**
     * 群详情
     */
    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 List<QwLabelVO> labelList(Map<String,Object> map, String deviceId){
        Gson gson = new Gson();
        String data = common(QwConfig.LABEL_LIST,map,deviceId);
        Type userListType = new TypeToken<List<QwLabelVO>>() {}.getType();
        return gson.fromJson(data, userListType);
    }





    public static QwMessageReturn messageImage(Map<String,Object> map, String deviceId){
        String data = common(QwConfig.MESSAGE_IMAGE,map,deviceId);
        return GsonFromJson(data, QwMessageReturn.class);
    }









    /**
     * 公共方法有返回值
     */
    public static String common(String url,Map<String,Object> map,String deviceId){
        Gson gson = new Gson();
        String json = sendQwHttp(url,map,deviceId);
        QwRestful qwRestful = GsonFromJson(json,QwRestful.class);
        if ("0".equals(qwRestful.getCode())){
            return gson.toJson(qwRestful.getData());
        }
        return null;
    }

    /**
     * 公共方法无返回值
     */
    public static QwRestful common2(String url,Map<String,Object> map,String deviceId){
        String json = sendQwHttp(url,map,deviceId);
        return GsonFromJson(json,QwRestful.class);
    }

}
