package com.limap.controller.work;

import com.limap.common.core.controller.BaseController;
import com.limap.common.core.domain.AjaxResult;
import com.limap.common.core.page.PageDomain;
import com.limap.common.core.page.TableDataInfo;
import com.limap.common.core.page.TableSupport;
import com.limap.common.enums.iot.DeviceType;
import com.limap.common.utils.ali.iot.ClientUtil;
import com.limap.core.screen.service.IScreenService;
import com.limap.core.screen.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author limap
 */
@Api(tags = "大屏设备信息")
@RestController
@RequestMapping("/screen")
public class TestController extends BaseController {

    @Resource
    private IScreenService screenService;
    @Resource
    private ClientUtil clientUtil;

    @ApiOperation("获取塘口信息")
    @PostMapping("pond")
    public AjaxResult getPonds(){
        Long userId = getUserId();
        List<PondVo> userPonds = screenService.getUserPonds(userId);
        return AjaxResult.success(userPonds);
    }

    @ApiOperation("获取设备信息")
    @PostMapping("equipment")
    public AjaxResult getEquipments(@RequestBody SearchVo vo){
        Long userId = getUserId();
        List<AppEquipmentVo> equipments = screenService.getUserEquipments(userId, vo.getPondIds(),vo.getDeviceType());
        List<PondVo> userPonds = screenService.getUserPonds(userId);
        Map<Long, List<PondVo>> collect = userPonds.stream().collect(Collectors.groupingBy(PondVo::getId));
        equipments.forEach(e-> {
            Long pondId = e.getPondId();
            if(collect.containsKey(pondId)){
                PondVo pondVo = collect.get(pondId).get(0);
                e.setPondName(pondVo.getPondName());
            }
        });
        return AjaxResult.success(equipments);
    }

    @ApiOperation("获取全部设备信息")
    @PostMapping("equipment/list")
    public AjaxResult getEquipmentsList(@RequestBody SearchVo vo){
        Long userId = getUserId();
        List<AppEquipmentVo> equipments = screenService.getEquipmentsList(userId, vo.getPondIds(),vo.getDeviceType());
        List<PondVo> userPonds = screenService.getUserPonds(userId);
        Map<Long, List<PondVo>> collect = userPonds.stream().collect(Collectors.groupingBy(PondVo::getId));
        equipments.forEach(e-> {
            Long pondId = e.getPondId();
            if(collect.containsKey(pondId)){
                PondVo pondVo = collect.get(pondId).get(0);
                e.setPondName(pondVo.getPondName());
            }
        });
        return AjaxResult.success(equipments);
    }

    @ApiOperation("获取历史数据")
    @PostMapping("history")
    public AjaxResult getEquipmentHistory(@RequestBody SearchVo vo){
        if( vo.getDeviceId() == null){
            return error("设备信息不能为空");
        }
        Long userId = getUserId();
        List<EquipmentHistory> list = screenService.getEquipmentHistory(userId,vo.getDeviceId());
        return AjaxResult.success(list);
    }

    @ApiOperation("获取历史数据")
    @PostMapping("history/compare")
    public AjaxResult historyCompare(@RequestBody @Valid CompareSearchVo vo){
        List<CompareRequest> list = screenService.historyCompare(vo);
        Long userId = getUserId();
        List<AppEquipmentVo> equipments = screenService.getUserEquipments(userId, null, DeviceType.doxygen.getCode());
        Map<Long, List<AppEquipmentVo>> collect = equipments.stream().collect(Collectors.groupingBy(AppEquipmentVo::getId));
        list.forEach(e->{
            if(collect.containsKey(e.getDeviceId())) {
                AppEquipmentVo appEquipmentVo = collect.get(e.getDeviceId()).get(0);
                e.setDeviceName(appEquipmentVo.getDeviceName());
            }
        });
        return AjaxResult.success(list);
    }

    @ApiOperation("获取告警数据")
    @GetMapping("warn")
    public TableDataInfo getWarnList(){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<WarnVo> list = screenService.getWarnList(getUserId(),pageNum,pageSize);
        long total = screenService.getWarnListCount(getUserId());
        return getDataTable(list,total);
    }

    @ApiOperation("控制器开关控制")
    @PostMapping("controlSwitch")
    public AjaxResult controlSwitch(@RequestBody @Valid ControlSwitchVo vo){
        Map<String,Integer> params = new HashMap<>();
        params.put("Switch"+vo.getIndex(),vo.getStatus());
        Boolean aBoolean = clientUtil.propertiesSet(vo.getIotId(), params);
        return toAjax(aBoolean);
    }
}
