import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Patch,
  Query,
} from '@nestjs/common';
import {
  ApiTags,
  ApiBearerAuth,
  ApiOperation,
  ApiProperty,
} from '@nestjs/swagger';
import { TokenData } from 'src/common/decorator/token-data.decorator';
import { PaginationDTO } from 'src/assessment-evaluation/dto/pagination.dto';
import { NoAuthToken } from 'src/common/decorator/no-token.decorator';
import {
  UpdateDeviceDto,
  CreateDeviceDto,
  DeviceService,
  DevicePagingDto,
  FindOperationContentDto,
} from 'src/train/service/device/device.service';
import { exec } from 'child_process';
import ping from 'pingman';
// eslint-disable-next-line @typescript-eslint/no-var-requires
export class PingDto {
  @ApiProperty({ description: '主机地址' })
  host: string;
  @ApiProperty({ description: 'ping次数', required: false, default: 1 })
  count: number;
  @ApiProperty({ description: '每次ping超时秒', required: false, default: 2 })
  timeout: number;
}

@ApiTags('训练管理-设备管理')
@ApiBearerAuth()
// @NoAuthToken()
@Controller('train/device/device')
export class DeviceController {
  constructor(private readonly deviceService: DeviceService) {}

  @Post()
  @ApiOperation({ summary: '创建设备' })
  async create(
    @Body() createClassifyDto: CreateDeviceDto,
    @TokenData() token: any,
  ) {
    const userId = token.userId;
    createClassifyDto.creator = createClassifyDto.updater = userId;
    return this.deviceService.create(createClassifyDto, token);
  }

  @Get()
  @ApiOperation({ summary: '获取所有设备' })
  async findAll() {
    return this.deviceService.findAll();
  }
  @Post('findOperationContent')
  @ApiOperation({ summary: '操作内容选择器,保存时请使用has中的id' })
  @NoAuthToken()
  async findOperationContent(@Body() list: FindOperationContentDto) {
    return this.deviceService.findOperationContent(list);
  }
  @Get('ping')
  @ApiOperation({ summary: 'ping命令测连通性' })
  @NoAuthToken()
  async ping(@Query() query: PingDto) {
    const response = await ping(query.host, {
      numberOfEchos: +query?.count || 1,
      timeout: +query.timeout || 2,
    });
    return response;
  }
  @Get('details/:id')
  @ApiOperation({ summary: '根据ID获取设备详情' })
  @NoAuthToken()
  async findOneById(@Param('id') id: number) {
    return await this.deviceService.getOneById(+id);
  }

  @Get('paging')
  @ApiOperation({ summary: '分页获取设备' })
  @NoAuthToken()
  async paging(@Query() pagingInfo: DevicePagingDto) {
    return this.deviceService.pagingDetails(pagingInfo);
  }
  // async paging(@Query() pagingInfo: PaginationDTO) {
  //   return this.deviceService.paging(pagingInfo);
  // }

  @Patch(':id')
  @ApiOperation({ summary: '更新设备' })
  async update(
    @Param('id') id: string,
    @Body() updateClassifyDto: UpdateDeviceDto,
  ) {
    return this.deviceService.update(+id, updateClassifyDto);
  }

  @Delete('/all')
  @ApiOperation({ summary: '批量删除设备' })
  async remove(@Body() ids: number[]) {
    return this.deviceService.remove(ids);
  }
}
