package com.skr.mdm.api.equipment.controller;

import com.skr.mdm.api.equipment.UserStrategyFactory;
import com.skr.mdm.api.equipment.service.EquipmentService;
import com.skr.mdm.bean.qo.BaseQueryQO;
import com.skr.mdm.bean.qo.equipment.EquipmentAddUpdateQO;
import com.skr.mdm.bean.qo.equipment.EquipmentPageQO;
import com.skr.mdm.bean.vo.equipment.DeviceEquipmentVO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author nfq
 * @date 2023-02-02 9:22
 */
@Validated
@RestController
@RequestMapping("equipment")
@Api(tags = "设备管理")
@Slf4j
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;


    @PostMapping("list")
    @PreAuthorize("hasAnyAuthority('sys:equipment:list')")
    @ApiOperation(value = "列表", response = DeviceEquipmentVO.class)
    public ResponseResult listEquipment(@RequestBody @Valid EquipmentPageQO equipmentPageQO,
                                        @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.listEquipment(equipmentPageQO, loginAdminDTO);
    }

    @PostMapping("info")
    @PreAuthorize("hasAnyAuthority('sys:equipment:info')")
    @ApiOperation(value = "设备详情")
    @ApiImplicitParam(name = "strategyType", value = "策略类型（1:设备信息 2：功能策略 3：配置策略 4：合规策略 5：应用推送 6：应用策略 7：应用卸载 8：应用安装 9：文档 10:猎鹰终端）")
    public ResponseResult listEquipment(@RequestParam @NotNull(message = "设备ID不能为空") Integer equipmentId,
                                        @RequestParam @NotNull(message = "strategyType不能为空") Integer strategyType) {
        return UserStrategyFactory.getStrategy(strategyType).getUserStrategy(equipmentId);
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    @PreAuthorize("hasAnyAuthority('sys:equipment:add')")
    public ResponseResult addEquipment(@RequestBody @Valid EquipmentAddUpdateQO equipmentAddUpdateQO,
                                       @ApiIgnore Authentication authentication) {
        LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.addEquipment(equipmentAddUpdateQO, admin);
    }

    @PostMapping("update")
    @ApiOperation(value = "编辑")
    @PreAuthorize("hasAnyAuthority('sys:equipment:update')")
    public ResponseResult updateEquipment(@RequestBody @Valid EquipmentAddUpdateQO equipmentAddUpdateQO,
                                          @ApiIgnore Authentication authentication) {
        LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.updateEquipment(equipmentAddUpdateQO, admin);
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    @PreAuthorize("hasAnyAuthority('sys:equipment:delete')")
    public ResponseResult deleteEquipment(@RequestParam @NotEmpty(message = "设备ID不能为空") List<Integer> equipmentIdList,
                                          @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.deleteEquipment(equipmentIdList, loginAdminDTO);
    }

    @ApiOperation("设备导入")
    @PostMapping("import")
    @PreAuthorize("hasAnyAuthority('sys:equipment:import')")
    public ResponseResult importDept(@RequestParam("file") MultipartFile file,
                                     @ApiIgnore Authentication authentication) throws Exception {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.importEquipment(file, loginAdminDTO);
    }

    @ApiOperation("设备导出")
    @PostMapping("export")
    @PreAuthorize("hasAnyAuthority('sys:equipment:export')")
    public void exportEquipment(@RequestParam(required = false) List<Integer> deptIdList,
                                @ApiIgnore Authentication authentication,
                                HttpServletResponse response) throws IOException {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        equipmentService.exportEquipment(deptIdList, loginAdminDTO, response);
    }

    @ApiOperation("绑定/解绑设备")
    @PostMapping(value = "updateBoundState")
    public ResponseResult updateBoundState(@RequestParam @NotNull(message = "设备ID不能为空") Integer equipmentId,
                                           @RequestParam @NotNull(message = "绑定状态不能为空")
                                           @ApiParam(value = "绑定状态（0:绑定 1:解绑）", required = true) Integer state,
                                           @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.updateBoundState(equipmentId, state, loginAdminDTO);
    }

    @ApiOperation("机卡解绑")
    @PostMapping(value = "card/unbind")
    @PreAuthorize("hasAnyAuthority('sys:equipment:sim:unbind')")
    public ResponseResult cardUnbind(@RequestParam @NotNull(message = "设备ID不能为空") Integer equipmentId,
                                     @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.cardUnbind(equipmentId, loginAdminDTO);
    }


    /**
     * 设备离线解锁码
     * @param offLineUnlockCode 离线解锁码
     * @return
     */
    @PostMapping("offLineUnlockCode")
    @ApiOperation("生成离线解锁码")
    @PreAuthorize("hasAnyAuthority(new String[] {'sys:equipment:offline:unlockcode'})")
    public ResponseResult offLineUnlockCode(@RequestParam @NotBlank(message = "离线解锁码不能为空") String offLineUnlockCode,
                                            @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return equipmentService.offLineUnlockCode(offLineUnlockCode, loginAdminDTO);
    }
}