package com.xjrsoft.module.hikvision.util;

import cn.hutool.db.Db;
import cn.hutool.jwt.JWTUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.DatasourceUtil;
import com.xjrsoft.module.system.controller.AuthorizeController;
import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.text.ParseException;

@RestController
@RequestMapping("/event")
@Api(value = "/event", tags = "事件接收")
@AllArgsConstructor
@Slf4j
public class EventController {
    private final Out_In_RecordUtil outInRecordUtil = new Out_In_RecordUtil();
    private final AuthorizeController authorizeController;

    DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
    private final Db use = Db.use(datasource);
    private final FaceImportMapper faceImportMapper;

    @PostMapping("/receiveCar")
    @ApiOperation(value = "接收车辆事件")
    public ResponseEntity<Void> receiveCarEvent(@RequestBody(required = false) String eventData) {
        log.info("Received event data: {}", eventData);
        new Thread(() -> {
            try {
                outInRecordUtil.GetVehicleRecordTest(use, faceImportMapper, eventData);
            } catch (SQLException | ParseException e) {
                log.error("Error processing event data", e);
            }
        }).start();

        // 立即返回HTTP 200 OK响应
        return ResponseEntity.ok().build();
    }

    @PostMapping("/receivePeople")
    @ApiOperation(value = "接收人员事件")
    public ResponseEntity<Void> receivePeopleEvent(@RequestBody(required = false) String eventData) {
        log.info("Received event data: {}", eventData);
        new Thread(() -> {
            try {
                outInRecordUtil.GetTeacherAndStudentRecordsTest(use, faceImportMapper, eventData);
            } catch (SQLException | ParseException e) {
                log.error("Error processing event data", e);
            }
        }).start();

        return ResponseEntity.ok().build();
    }

    @PostMapping("/subscribe")
    @ApiOperation(value = "订阅事件")
    public RT<Boolean> subscribeEvent() {
        ApiUtil apiUtil = new ApiUtil();
        JsonArray eventList = new JsonArray();
        String apiPath = "/api/eventService/v1/eventSubscriptionByEventTypes";
        JsonObject paramJson = new JsonObject();
//*******手动更改****************************************************
        eventList.add(196893); //人脸匹配
//        eventList.add(771760131); //正常过车
//        eventList.add(771760134);
        paramJson.addProperty("eventDest", "http://ff.cf.yingcaibx.com/event/receivePeople");
//******************************************************************
        paramJson.add("eventTypes", eventList);

        String response = apiUtil.doPost(apiPath, paramJson.toString(), null);
        log.info("Subscribe response: {}", response);
        return RT.ok(true);
    }

    @PostMapping("/query")
    @ApiOperation(value = "查询已订阅事件")
    public String queryEvent() {
        ApiUtil apiUtil = new ApiUtil();
        String apiPath = "/api/eventService/v1/eventSubscriptionView";
        String response = apiUtil.doPost(apiPath, null, null);
        System.out.println("output: " + response);

        return response;
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "取消已订阅事件")
    public String cancelEvent() {
        ApiUtil apiUtil = new ApiUtil();
        JsonArray eventList = new JsonArray();
//*******手动更改****************************************************
        eventList.add(196893);
        eventList.add(771760131); //正常过车
        eventList.add(771760134);
//******************************************************************
        JsonObject paramJson = new JsonObject();
        paramJson.add("eventTypes", eventList);
        String apiPath = "/api/eventService/v1/eventUnSubscriptionByEventTypes";
        String response = apiUtil.doPost(apiPath, paramJson.toString(), null);
        System.out.println("output: " + response);

        return response;
    }
}
