package net.vtstar.zhongtong.avi.monitoring.controller;

import io.netty.buffer.ByteBuf;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.protocol.modbus.tcp.netty.packet.ModbusResponse;
import net.vtstar.protocol.modbus.tcp.netty.packet.ReadCoilsRequest;
import net.vtstar.protocol.modbus.tcp.netty.packet.ReadCoilsResponse;
import net.vtstar.protocol.modbus.tcp.netty.service.ModbusTemplate;
import net.vtstar.protocol.modbus.tcp.netty.utils.PoolKey;
import net.vtstar.utils.domain.Return;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopArea;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.monitoring.domain.Bus;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.BusType;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.DateType;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.ProductionType;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.StationState;
import net.vtstar.zhongtong.avi.monitoring.domain.vo.CarStatisticsVO;
import net.vtstar.zhongtong.avi.monitoring.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(description = "avi区域实时监控模块相关")
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private AreaService areaService;
    @Autowired
    private ModbusTemplate template;

    @ApiOperation(value = "根据区域编号查询区域实时信息", response = PaintshopArea.class, responseContainer = "List", notes = "区域编号：\r\n" +
            "整体区域：0\r\n钣金：1\r\n电泳：2\r\n密封防腐：3\r\n腻子：4\r\n中涂：5\r\n面/色漆：6\r\n成品：7")
    @GetMapping("/areaInfo/realTime")
    private Return getAreaInfo(@RequestParam(value = "areaNo", required = false) String areaNo) {

        List<PaintshopArea> areas = areaService.getInfoByAreaNo(areaNo);
//        List<PaintshopArea> areas = new ArrayList<>();
//        PaintshopArea area = new PaintshopArea();
//        area.setCode("2");
//        area.setName("电泳");
//        area.setOnlineNum("44");
//        Map<String, PaintshopStation> stationMap = new HashMap<>();
//        Map<String, PaintshopStation> stationMap2 = new HashMap<>();
//        Map<String, PaintshopStation> stationMap3 = new HashMap<>();
//        Map<String, PaintshopStation> stationMap4 = new HashMap<>();
//        for (int i = 0; i < 9; i++) {
//            PaintshopStation station = new PaintshopStation();
//            station.setBusNo("No" + i);
//            station.setKeyStation(true);
//            station.setCode("HDG1-4" + i);
//            station.setState(StationState.USE);
//            station.setBusType(ProductionType.OWN);
//            stationMap.put("HDG1-4" + i,station);
//        }
//        PaintshopArea area2 = new PaintshopArea();
//        area2.setCode("1");
//        area2.setName("钣金");
//        area2.setOnlineNum("44");
//        for (int i = 0; i < 9; i++) {
//            PaintshopStation station = new PaintshopStation();
//            station.setBusNo("No" + i);
//            station.setKeyStation(true);
//            station.setCode("HDG1-1" + i);
//            station.setState(StationState.USE);
//            station.setBusType(ProductionType.OWN);
//            stationMap2.put("HDG1-1" + i,station);
//        }
//        PaintshopArea area3 = new PaintshopArea();
//        area3.setCode("6");
//        area3.setName("喷涂");
//        area3.setOnlineNum("44");
//        for (int i = 0; i < 9; i++) {
//            PaintshopStation station = new PaintshopStation();
//            station.setBusNo("No" + i);
//            station.setKeyStation(true);
//            station.setCode("HDG1-19" + i);
//            station.setState(StationState.USE);
//            station.setBusType(ProductionType.OWN);
//            stationMap3.put("HDG1-19" + i,station);
//        }
//        PaintshopArea area4 = new PaintshopArea();
//        area4.setCode("5");
//        area4.setName("中涂");
//        area4.setOnlineNum("44");
//        for (int i = 0; i < 9; i++) {
//            PaintshopStation station = new PaintshopStation();
//            station.setBusNo("No" + i);
//            station.setKeyStation(true);
//            station.setCode("HDG1-12" + i);
//            station.setState(StationState.USE);
//            station.setBusType(ProductionType.OTHER);
//            stationMap4.put("HDG1-12" + i,station);
//        }
//        area.setStationMap(stationMap);
//        area2.setStationMap(stationMap2);
//        area3.setStationMap(stationMap3);
//        area4.setStationMap(stationMap4);
//        areas.add(area4);
//        areas.add(area);
//        areas.add(area2);
//        areas.add(area3);
        return Return.success(areas);
    }

    @ApiOperation(value = "查询车间各区域进车及交车数量", response = CarStatisticsVO.class, responseContainer = "List")
    @GetMapping("/bus/statistics")
    private Return getRealtimeData() {
        List<CarStatisticsVO> vos = new ArrayList<>();
        CarStatisticsVO carStatisticsVO = new CarStatisticsVO();
        carStatisticsVO.setAreaNo("1");
        carStatisticsVO.setBusType(BusType.IN);
        carStatisticsVO.setDateType(DateType.YESTERDAY);
        carStatisticsVO.setSize(new BigDecimal(20));

        CarStatisticsVO carStatisticsVO2 = new CarStatisticsVO();
        carStatisticsVO2.setAreaNo("2");
        carStatisticsVO2.setBusType(BusType.OUT);
        carStatisticsVO2.setDateType(DateType.TODAY);
        carStatisticsVO2.setSize(new BigDecimal(20));
        vos.add(carStatisticsVO);
        vos.add(carStatisticsVO2);

        vos = areaService.getBusSta();
        return Return.success(vos);
    }

    @ApiOperation(value = "根据工单号及订单号定位车体当前位置", response = PaintshopArea.class, responseContainer = "List")
    @GetMapping("/currentPosition/orderNo/workOrderNo")
    private Return getPositionByOrderAndNo(@RequestParam(value = "orderNo", required = false) String orderNo, @RequestParam(value = "workOrderNo", required = false) String workOrderNo) {
//        Map<String, PaintshopStation> stationMap = new HashMap<>();
//        PaintshopStation station = new PaintshopStation();
//        station.setBusNo("aaa");
//        station.setKeyStation(true);
//        station.setCode("HDG1-1");
//        station.setState(StationState.USE);
//        station.setBusType(ProductionType.OWN);
//        stationMap.put("HDG1-1", station);
        Map<String, PaintshopStation> areas = areaService.findBusLocation(orderNo, workOrderNo);
        return Return.success(areas);
    }

    @ApiOperation(value = "根据车身号获得当前车体信息", response = Bus.class)
    @GetMapping("/detail/busNo")
    private Return getBusDetail(@RequestParam("busNo") String busNo) {

        Bus bus = areaService.getByBusNo(busNo);
        return Return.success(bus);
    }

    @GetMapping("/test")
    private Return test() {
        PoolKey key = new PoolKey("localhost", 502);
        ReadCoilsRequest readCoilsRequest = new ReadCoilsRequest(1, 0, 50);
        try {
            ModbusResponse execute = template.execute(key, readCoilsRequest, ReadCoilsResponse.class);
            ByteBuf body = execute.getBody();

            int capacity = body.capacity();
            //byte b = body.readByte();
            byte aByte4 = body.getByte(0);


            byte aByte1 = body.getByte(1);
            byte aByte = body.getByte(2);
            int length = body.readableBytes();
//          byte[] bytes = new byte[lenght];
//          ByteBuf bytes1 = body.getBytes(body.readerIndex(), bytes);

            int a = 1;
        } catch (Exception e) {


        }

        return Return.success();
    }

}
