package com.cico.nest.lcdp.boot.core.qxsd.adapter.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.request.LocusDataRequest;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.request.PassingProfileRequest;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.request.RealTimeVehicleQueryRequest;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.PassProfileImageVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.PassingProfileVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.RealTimeVehicleVO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelBaseService;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.VehicleService;
import com.cico.nest.lcdp.boot.core.qxsd.domain.gateway.HistoryGateway;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.HistoryTunnelVehicle;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.TunnelRealTimeVehicle;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.VehicleInfo;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelBaseDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.qjmh.IQjmhCarService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.qjmh.dto.QjmhCarDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.qjmh.dto.QjmhCarParamPageDTO;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.common.util.date.DateUtils;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车辆
 * @author zws
 * @date 2024/8/6
 */
@Tag(name = "管理后台 - 车辆相关接口")
@Slf4j
@RestController
@RequestMapping("/qxsd/vehicle")
public class VehicleController {
    @Resource
    private VehicleService vehicleService;
    @Resource
    private TunnelBaseService tunnelBaseService;
    @Resource
    private IQjmhCarService qjmhCarService;
    @Value("${qxsd.qjmh.tunnel-name}")
    private String qjmhTunnelName;
    @Value("${qxsd.xlw.images}")
    private String xlwImagePath;
    @Resource
    private HistoryGateway historyGateway;

    /**
     * 留存车辆列表
     * @return 留存车辆列表
     */
    @PostMapping("/retainedCarList")
    @Operation(summary = "留存车辆列表")
    public CommonResult<RealTimeVehicleVO> realTimeVehicleList(@RequestBody RealTimeVehicleQueryRequest query){
        TunnelRealTimeVehicle realTimeVehicleList = vehicleService.realTimeVehicleList(query);
        RealTimeVehicleVO realTimeVehicleVO = TunnelRealTimeVehicle.convert2VO(realTimeVehicleList,query.getPageNo(),query.getPageSize());
        if (realTimeVehicleVO.getVehicleInfoList() ==null || realTimeVehicleVO.getVehicleInfoList().getRecords()==null ){
            return CommonResult.success(realTimeVehicleVO);
        }
        try {
            QjmhCarParamPageDTO carParam = new QjmhCarParamPageDTO();
            carParam.setVehiclePlate(query.getCarPlate());
            carParam.setOverTime(1);
            List<String> tunnelNames = Lists.newArrayList(qjmhTunnelName);  //TODO 先默认一个隧道，后面跟前端再调整
            //查入参的隧道id对应的隧道名字
            //List<TunnelBaseDO> tunnelBaseDOS = tunnelBaseService.getByIds(null,query.getTunnelIds());
            //List<String> tunnelNames = tunnelBaseDOS.stream().map(TunnelBaseDO::getTunnelName).collect(Collectors.toList());
            carParam.setTunnelNameList(tunnelNames);
            //查找隧道留存车辆的清单
            List<QjmhCarDTO>  qjmhCars = qjmhCarService.getCarList(carParam);
            for (VehicleInfo vehicleInfo: realTimeVehicleVO.getVehicleInfoList().getRecords() ) {
                String carPlate = vehicleInfo.getCarPlate();
                for(QjmhCarDTO car: qjmhCars){
                    String plate = car.getVehiclePlate();//全景门户给的留存车辆的车牌 形式是“车牌_颜色”
                    if (StringUtils.isNotEmpty(plate)){
                        String[] plateArray = plate.split("_");
                        if (carPlate.equals(plateArray[0])){
                            vehicleInfo.setOverTimeMark(true);
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("留存车辆超时标记错误，{}",e.getMessage());
        }
        return CommonResult.success(realTimeVehicleVO);
    }

    /**
     * 车辆详情接口
     * @param carId 车辆id
     * @return 车辆信息
     */
    @GetMapping("/vehicleInfoByCarId")
    @Operation(summary = "车辆详情接口")
    public CommonResult<VehicleInfo> vehicleInfoByCarId(@RequestParam("carId")String carId){
        return CommonResult.success(vehicleService.vehicleInfoByCarId(carId));
    }


    /**
     * 重点车辆标记
     * @param carId 车辆Id
     */
    @GetMapping("/focusVehiclesMark")
    @Operation(summary = "重点车辆标记")
    public CommonResult<String> focusVehiclesMark(@RequestParam("carId") String carId){
        vehicleService.focusVehiclesMark(carId);
        return CommonResult.success("");
    }

    /**
     * 重点车辆取消标记
     * @param carId 车辆Id
     */
    @Operation(summary = "重点车辆取消标记")
    @GetMapping("/focusVehiclesUnMark")
    public CommonResult<String> focusVehiclesUnMark(@RequestParam("carId") String carId){
        vehicleService.focusVehiclesUnMark(carId);
        return CommonResult.success("");
    }


    @Operation(summary = "通行记录列表")
    @PostMapping("/passingProfileList")
    public CommonResult<PageResult<PassingProfileVO>> passingProfileList(@RequestBody PassingProfileRequest passingProfileRequest){
        return CommonResult.success(vehicleService.passingProfileList(passingProfileRequest));
    }

    @Operation(summary = "获取通行记录车辆轨迹图片数据")
    @Parameter(name = "passProfileId", description = "通行记录id")
    @GetMapping("/passProfileImage")
    public CommonResult<PassProfileImageVO> passProfileImage(@RequestParam("passProfileId") String passProfileId){
        return CommonResult.success(vehicleService.passProfileImage(passProfileId));
    }

    @Operation(summary = "获取通行记录车辆轨迹图片数据 列表获取多个")
    @PostMapping("/passProfileImageList")
    public CommonResult<Map<String,PassProfileImageVO>> passProfileImageList(@RequestBody List<String> passProfileIds){
        return CommonResult.success(vehicleService.passProfileImages(passProfileIds));
    }

    @Operation(summary = "获取车辆轨迹数据")
    @PostMapping("/getCarLocusData")
    public CommonResult<List<HistoryTunnelVehicle>> getCarLocusData(@RequestBody LocusDataRequest request){
        return CommonResult.success(vehicleService.getCarLocusData(request));
    }
//    @Operation(summary = "获取车辆卡口图片")
//    @GetMapping("/getCarBayonetImageList")
//    public CommonResult<List<String>> getCarBayonetImageList(@RequestParam("carPlate") String carPlate, @RequestParam("time") String time){
//        // todo 说是新老洞的取发不同
//        List<String> list = new ArrayList<>();
//        list = historyGateway.saveBayonetImage(carPlate,DateUtil.parse(time, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
//        if(CollUtil.isNotEmpty(list)){
//            list.stream().filter(e->e.startsWith("/")).map(xlwImagePath::concat).toList();
//        }
//        return CommonResult.success(list);
//    }

}
