package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzt.airconditionersystem.bean.*;
import com.yzt.airconditionersystem.enums.AlarmStatusEnum;
import com.yzt.airconditionersystem.enums.CompanyTypeEnum;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.AlarmService;
import com.yzt.airconditionersystem.service.CompanyService;
import com.yzt.airconditionersystem.service.FacilityAlarmService;
import com.yzt.airconditionersystem.service.GatewayService;
import com.yzt.airconditionersystem.util.BasePageVoUtil;
import com.yzt.airconditionersystem.vo.BasePageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author TzZach
 * @since 2022-04-21 22:42
 */
@Controller
public class AlarmController {

    private static final String PREFIX = "alarm/";

    @Autowired
    private AlarmService alarmService;
    @Autowired
    private FacilityAlarmService facilityAlarmService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private GatewayService gatewayService;

    @GetMapping("/alarm/index")
    public String alarmList() {
        return PREFIX + "alarm-list";
    }

    @GetMapping("/alarm/page")
    @ResponseBody
    public BasePageVo alarmPage(@RequestParam("page") Long page,
                                @RequestParam("limit") Long limit,
                                HttpSession session,
                                @RequestParam(value = "companyId") String companyId) {
//        User loginUser = (User) session.getAttribute("loginUser");
//        String companyId = loginUser.getCompanyId();
//        if (companyId.equals(CompanyTypeEnum.HEADQUARTERS.getId().toString())) {
//            Page<FacilityAlarm> facilityAlarmPage = new Page<>(page, limit);
//            facilityAlarmPage = facilityAlarmService.page(facilityAlarmPage);
//            List<FacilityAlarm> list = facilityAlarmPage.getRecords();
//            for (FacilityAlarm facilityAlarm : list) {
//                String gatewayId = facilityAlarm.getGatewayId();
//                List<Gateway> gateways = gatewayService.listByIds(Arrays.asList(gatewayId));
//                for (Gateway gateway : gateways) {
//                    String gatewayCompanyId = gateway.getCompanyId();
//                    Company company = companyService.getById(gatewayCompanyId);
//                    facilityAlarm.setCompanyName(company.getName());
//                    facilityAlarm.setDistrict(company.getDistrict());
//                    facilityAlarm.setOperatorId(company.getOperatorId());
//                    Company operator = companyService.getById(company.getOperatorId());
//                    facilityAlarm.setOperatorName(operator.getName());
//                }
//            }
//            BasePageVo pageVo = BasePageVoUtil.getPageVo(facilityAlarmPage);
//            return pageVo;
//        }
//        Company company = companyService.getById(companyId);


//        Page<Alarm> pageInfo = new Page<>(page, limit);
//        pageInfo = alarmService.page(pageInfo, null);
//        BasePageVo pageVo = BasePageVoUtil.getPageVo(pageInfo);
        User loginUser = (User) session.getAttribute("loginUser");
        String HqId = CompanyTypeEnum.HEADQUARTERS.getId().toString();
        List<String> companyIds = new ArrayList<>();
        if (loginUser.getCompanyId().equals(HqId) && companyId.equals(HqId)) {
            companyIds = null;
        } else {
//            List<Company> companyList = companyService.list();
//            companyList = companyList.stream().filter(company ->
//                company.getType().equals(CompanyTypeEnum.CLIENT.getId().toString())
//            ).collect(Collectors.toList());
//            for (Company company : companyList) {
//                operatorIds.add(company.getOperatorId());
//            }
            LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Company::getOperatorId, companyId)
                            .eq(Company::getType, CompanyTypeEnum.CLIENT.getId().toString())
                            .select(Company::getId);
            List<Company> list = companyService.list(queryWrapper);
            for (Company company : list) {
                companyIds.add(company.getId());
            }
        }
        PageHelper.startPage(Math.toIntExact(page), Math.toIntExact(limit));
        List<FacilityAlarm> list = facilityAlarmService.selectByGatewayIdAndCompanyId(companyIds);
        int navigatePages = (int) (list.size() / limit);
        PageInfo<FacilityAlarm> pageInfo = new PageInfo<>(list, navigatePages);
        return BasePageVoUtil.getPageVo(pageInfo);
    }

    @PostMapping("/alarm/edit")
    @ResponseBody
    public Result updateAlarmStatus(@RequestBody Alarm alarm) {
        alarm.setStatus(AlarmStatusEnum.HANDLED);
        alarmService.updateById(alarm);
        return Result.success();
    }

    @PostMapping("/alarm/delete")
    @ResponseBody
    public Result deleteAlarm(@RequestBody Alarm alarm) {
        alarmService.removeById(alarm);
        return Result.success();
    }

    @PostMapping("/alarm/batchDelete")
    @ResponseBody
    public Result deleteAlarmBatch(@RequestBody List<Integer> ids) {
        alarmService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping("/alarm/search")
    @ResponseBody
    public BasePageVo searchAlarm(@RequestParam("page") Long page,
                                     @RequestParam("limit") Long limit,
                                     Alarm alarm) {

        String clientName = alarm.getClientName();
        String district = alarm.getDistrict();
        String operatorName = alarm.getOperatorName();

        PageHelper.startPage(Math.toIntExact(page), Math.toIntExact(limit));
        LambdaQueryWrapper<Alarm> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(clientName)) {
            queryWrapper.like(Alarm::getClientName, clientName);
        }
        if(StringUtils.hasLength(district)) {
            queryWrapper.like(Alarm::getDistrict, district);
        }
        if(StringUtils.hasLength(operatorName)) {
            queryWrapper.like(Alarm::getOperatorName, operatorName);
        }
        List<Alarm> alarms = alarmService.list(queryWrapper);
        int navigatePages = (int) (alarms.size() / limit);
        PageInfo<Alarm> pageInfo = new PageInfo<>(alarms, navigatePages);
        BasePageVo pageVo = BasePageVoUtil.getPageVo(pageInfo);

        return pageVo;
    }

}
