package com.ruoyi.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.demo.entity.Equipment;
import com.ruoyi.demo.entity.Plan;
import com.ruoyi.demo.entity.PlanItem;
import com.ruoyi.demo.entity.bo.EquipmentBo;
import com.ruoyi.demo.entity.bo.PlanItemBo;
import com.ruoyi.demo.entity.bo.StateBo;
import com.ruoyi.demo.entity.vo.EquipmentVo;
import com.ruoyi.demo.entity.vo.PlanItemVo;
import com.ruoyi.demo.enums.EquipmentOnStateEnum;
import com.ruoyi.demo.enums.EquipmentStateEnum;
import com.ruoyi.demo.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/equimpent")
@RestController
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;

    /**
     * 添加新设备
     * @param equipmentVo
     * @return
     */
    @PostMapping("/addEquipment")
    public R addEquipment(@RequestBody @Validated(value = {AddGroup.class}) EquipmentVo equipmentVo){
        Integer integer = equipmentService.addEquipment(equipmentVo);
        if (integer == 0)
            R.ok("添加失败，设备编号已存在");
        return R.ok();
    }

    /**
     * 修改设备信息
     * @param equipmentVo
     * @return
     */
    @PutMapping("/updateEquipment")
    public R updateEquipment(@RequestBody @Validated(value = {AddGroup.class})EquipmentVo equipmentVo){
        R r = equipmentService.updateEquipment(equipmentVo);
        return r;
    }

    /**
     * 获取设备详细信息
     * @param equipmentId
     * @return
     */
    @GetMapping("/getEquipment")
    public R getEquipment(@NotBlank String equipmentId){
        EquipmentBo equipment = equipmentService.getEquipment(equipmentId);
        return R.ok(equipment);
    }

    /**
     * 获取设备列表
     * */
    @GetMapping("/getEquipmentList")
    public R getEquipmentList(EquipmentVo equipmentVo){
        HashMap hashMap = equipmentService.getEquipmentList(equipmentVo);
        return R.ok(hashMap);
    }

    /**
     * 删除设备
     * @param equipmentIds
     * @return
     */
    @DeleteMapping("/deleteEquipment")
    public R deleteEquipment(@NotEmpty String[] equipmentIds){
        String s = equipmentService.deleteEquipment(equipmentIds);
        return R.ok(s);
    }

    /**
     * TODO 获得该设备下的广告计划
     * @param equipmentVo
     * @return
     */
    @GetMapping("/getEquipmentPlanList")
    public R getEquipmentPlanList(@Validated(value = {QueryGroup.class}) EquipmentVo equipmentVo){
        IPage<PlanItemBo> euipmentPlanList = equipmentService.getEquipmentPlanList(equipmentVo);
        return R.ok(euipmentPlanList);
    }

    /**
     * TODO 修改该设备下的广告计划的排序
     * @param planItemVo
     * @return
     */
    @PutMapping("/updateEquipmentPlan")
    public R updateEquipmentPlan(@RequestBody @Validated(value = {EditGroup.class}) PlanItemVo planItemVo){
        equipmentService.updateEquipmentPlan(planItemVo);
        return R.ok();
    }

    @DeleteMapping("/deleteEquipmentPlan")
    public R deleteEquipmentPlan(@NotEmpty String[] planItemIds){
        equipmentService.deleteEquipmentPlan(planItemIds);
        return R.ok();
    }


    /**
     * 找到未分组的设备
     * @param equipmentVo
     * @return
     */
    @GetMapping("/getEquipmentBySelect")
    public R getEquipmentBySelect(EquipmentVo equipmentVo){
        Page<Equipment> equipments = equipmentService.getEquipmentBySelect(equipmentVo);
        return R.ok(equipments);
    }

    /**
     * 添加店铺下的设备
     * @param equipmentVo
     * @return
     */
    @PutMapping("/addStoreEquipment")
    public R addStoreEquipment(@RequestBody @Validated(value = {QueryGroup.class}) EquipmentVo equipmentVo){
        if (equipmentVo.getStoreId() == null || equipmentVo.getStoreId().equals(""))
            return R.fail("添加失败");
        equipmentService.addStoreEquipment(equipmentVo);
        return R.ok();
    }

    /**
     * 移除店铺下的设备
     * @param equipmentIds
     * @return
     */
    @DeleteMapping("/deleteStoreEquipment")
    public R deleteStoreEquipment(@NotEmpty String[] equipmentIds){
        equipmentService.deleteStoreEquipment(equipmentIds);
        return R.ok();
    }

    /**
     * 查看store下已有的设备
     * @param equipmentVo
     * @return
     */
    @GetMapping("/getEquipmentByStoreId")
    public R getEquipmentByStoreId(EquipmentVo equipmentVo){
        Page<Equipment> equipments = equipmentService.getEquipmentByStoreId(equipmentVo);
        return R.ok(equipments);
    }

    /**
     * 获取总设备数
     * @return
     */
    @GetMapping("/getEquipmentCount")
    public R getEquipmentCount(){
        HashMap equipmentCount = equipmentService.getEquipmentCount();
        return R.ok(equipmentCount);
    }

    /**
     * 获取设备授权状态（下拉框）
     * @return
     */
    @GetMapping("/getEquipmentState")
    public R getEquipmentState(){
        EquipmentStateEnum[] values = EquipmentStateEnum.values();
        ArrayList<StateBo> list = new ArrayList<>();
        for (EquipmentStateEnum value : values) {
            StateBo stateBo = new StateBo();
            stateBo.setState(value.getState());
            stateBo.setName(value.getName());
            list.add(stateBo);
        }
        return R.ok(list);
    }

    /**
     * 获取设备在线状态（下拉框）
     * @return
     */
    @GetMapping("/getEquipmentOnState")
    public R getEquipmentOnState(){
        EquipmentOnStateEnum[] values = EquipmentOnStateEnum.values();
        ArrayList<StateBo> list = new ArrayList<>();
        for (EquipmentOnStateEnum value : values) {
            StateBo stateBo = new StateBo();
            stateBo.setState(value.getState());
            stateBo.setName(value.getName());
            list.add(stateBo);
        }
        return R.ok(list);
    }
}
