package com.zcloud.primeport.plan.mjDevice;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zcloud.gbscommon.dahuaDevice.DaHuaDeviceCommon;
import com.zcloud.gbscommon.dahuaDevice.DhuaConfig;
import com.zcloud.primeport.persistence.dataobject.VehicleDockLogDO;
import com.zcloud.primeport.persistence.repository.VehicleDockLogRepository;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarBlackDto;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarRemoveDto;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarSaveDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 一级人员门禁对接  前缀
 *
 */
@Component
public class OnePersonUtil {
    @Value("${kmmj.dockflag}")
    private Integer dockFlag;
    @Value("${oneLevelPerson.prefix}")
    private String prefix;
    private static String prefix2 = "https://skqhdg.porthebei.com:9008/yjkmRy/";
    private static String username = "shuangkong";
    private static String password = "qinan@202604";
    private static String clientId = "mkmj_management";
    private static String clientSecret = "5ef82f7c-9926-46a3-8d07-136fd054849c";

    /**
     * 跟据身份证号码进行用户对接
     */
    public void dockLongTermUserByUserCardAndFaceImg(String userCard, String userUid, String username, String userFaceImages, String visitStartTime, String visitEndTime) throws Exception {
        DaHuaDeviceCommon daHuaDeviceCommon = getDaHuaDeviceCommon();
        JSONObject userByUserCard = daHuaDeviceCommon.getUserByUserCard(userCard).getJSONObject("data");
        if (userByUserCard.isEmpty()) {
            // 这个人不存在进行保存用户
            Long generateId = daHuaDeviceCommon.getGenerateId();
            JSONObject userInfo = new JSONObject();
            JSONObject fileInfo = new JSONObject();
            List<JSONObject> dataList = new ArrayList();
            fileInfo.set("type", 3);
            fileInfo.set("index", 1);
            fileInfo.set("path", userFaceImages);
            dataList.add(fileInfo);
            userInfo.set("id", generateId);
            userInfo.set("personBiosignatures", generateId);
            userInfo.set("validStartTime", visitStartTime);
            userInfo.set("validEndTime", visitEndTime);
            userInfo.set("code", userUid);
            userInfo.set("name", username);
            userInfo.set("paperType", 111);
            userInfo.set("paperNumber", userCard);
            userInfo.set("departmentId", 1L);
            userInfo.set("personType", 0);
            daHuaDeviceCommon.saveUser(userInfo);
        }
    }

    /**
     * 跟据身份证号码进行用户门禁有效期刷新
     */
    public void expirationDateRefreshedUserCard(String userCard, String visitStartTime, String visitEndTime) throws Exception {
        JSONObject entries = new JSONObject();
        entries.set("userCard", userCard);
        entries.set("visitStartTime", visitStartTime);
        entries.set("visitEndTime", visitEndTime);
        expirationDateRefreshedUserCard(Arrays.asList(entries));
    }

    /**
     * 批量跟据身份证号码进行用户门禁有效期刷新
     *
     */
    public void expirationDateRefreshedUserCard(List<JSONObject> userList) throws Exception {
        DaHuaDeviceCommon daHuaDeviceCommon = getDaHuaDeviceCommon();
        List<String> userCard = userList.stream().map(item -> item.getStr("userCard")).collect(Collectors.toList());
        Map<String, JSONObject> userCardMap = userList.stream().collect(Collectors.toMap(item -> item.getStr("userCard"), item -> item, (existing, replacement) -> replacement));
        JSONArray userListByUserCardList = daHuaDeviceCommon.getUserListByUserCard(userCard).getJSONObject("data").getJSONArray("pageData");
        List<JSONObject> userDataList = new ArrayList<>();
        for (int i = 0; i < userListByUserCardList.size(); i++) {
            JSONObject item = userListByUserCardList.getJSONObject(i);
            JSONObject entries = new JSONObject();
            entries.set("id", item.get("id", Long.class));
            JSONObject userCardJsonObj = userCardMap.get(item.getStr("paperNumber"));
            entries.set("visitStartTime", userCardJsonObj.getStr("visitStartTime"));
            entries.set("visitEndTime", userCardJsonObj.getStr("visitEndTime"));
            userDataList.add(entries);
        }
        if (!userDataList.isEmpty()) {
            expirationDateBatchRefreshedUserCardList(userDataList);
        }
    }

    /**
     * 跟据身份证号码进行用户门禁有效期刷新
     */
    private void expirationDateBatchRefreshedUserCardList(List<JSONObject> dataList) throws Exception {
        DaHuaDeviceCommon daHuaDeviceCommon = getDaHuaDeviceCommon();
        daHuaDeviceCommon.updatePersonValidity(dataList);
    }

    /**
     * 跟据身份证号码进行用户门禁开启与关闭
     * 1：解冻 2：冻结
     * (1:有 2：无)
     */
    public void freezeByUserCard(String userCard, String mkmjPermission) throws Exception {
        DaHuaDeviceCommon daHuaDeviceCommon = getDaHuaDeviceCommon();
        JSONObject userByUserCard = daHuaDeviceCommon.getUserByUserCard(userCard).getJSONObject("data");
        daHuaDeviceCommon.updatePersonStatus(userByUserCard.getLong("id"), Integer.valueOf(mkmjPermission));
    }


    public DaHuaDeviceCommon getDaHuaDeviceCommon() {
        DhuaConfig dhuaConfig = new DhuaConfig();
        dhuaConfig.setPrefix(prefix2);
        dhuaConfig.setClientId(clientId);
        dhuaConfig.setClientSecret(clientSecret);
        dhuaConfig.setUsername(username);
        dhuaConfig.setPassword(password);
        return new DaHuaDeviceCommon(dhuaConfig);
    }

    public static void main(String[] args) throws Exception {
        OnePersonUtil onePersonUtil = new OnePersonUtil();
        onePersonUtil.freezeByUserCard("360425198208131948", "1");
    }
}
