package org.wfc.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wfc.common.core.domain.R;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.omada.api.device.model.AdoptDeviceRequest;
import org.wfc.omada.api.device.model.ApGeneralConfig;
import org.wfc.omada.api.device.model.DeviceListAddBySnOpenApiVo;
import org.wfc.system.domain.SysDevice;
import org.wfc.system.service.ISysDeviceService;

import java.util.List;

/**
 * <p>
 * 客户平台-设备表 前端控制器
 * </p>
 *
 * @author sys
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/device")
public class SysDeviceController extends BaseController {

    @Autowired
    private ISysDeviceService sysDeviceService;

    @GetMapping("/page/{siteId}")
    public TableDataInfo page(@PathVariable String siteId, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "searchKey", required = false) String searchKey) {
        return sysDeviceService.getDeviceList(siteId, pageNum, pageSize, searchKey);
    }

    @GetMapping("/list")
    public AjaxResult list(SysDevice sysDevice) {
        List<SysDevice> list = sysDeviceService.list(Wrappers.<SysDevice>lambdaQuery()
                .like(SysDevice::getName, sysDevice.getName())
                .like(SysDevice::getMac, sysDevice.getMac()));
        return success(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return success(sysDeviceService.getById(id));
    }

    @PostMapping
    public AjaxResult add(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.save(sysDevice));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.updateById(sysDevice));
    }

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysDeviceService.removeByIds(CollUtil.newArrayList(ids)));
    }

    @PostMapping("/deviceJob")
    public AjaxResult deviceJob() {
        return toAjax(sysDeviceService.deviceJob());
    }

    @PostMapping("/settingJob")
    public AjaxResult settingJob() {
        return toAjax(sysDeviceService.settingJob());
    }

    @PostMapping("/testJob")
    public R<String> testJob() {
        return R.ok(sysDeviceService.testJob());
    }

    @PostMapping("/forget/{siteId}/{deviceMac}")
    public R<Boolean> forgetDevice(@PathVariable(required = true) String siteId, @PathVariable(required = true) String deviceMac) {
        return R.ok(sysDeviceService.forgetDevice(siteId, deviceMac));
    }

    @PostMapping("/add/{siteId}")
    public R<Boolean> addDevice(@PathVariable(required = true) String siteId, @RequestBody(required = false) DeviceListAddBySnOpenApiVo deviceListAddBySnOpenApiVo) {
        return R.ok(sysDeviceService.addDeviceBySn(siteId, deviceListAddBySnOpenApiVo));
    }

    @PostMapping("/startAdopt/{siteId}/{deviceMac}")
    public R<Boolean> startAdoptDevice(@PathVariable(required = true) String siteId, @PathVariable(required = true) String deviceMac, @RequestBody(required = false) AdoptDeviceRequest adoptDeviceRequest) {
        return R.ok(sysDeviceService.startAdoptDevice(siteId, deviceMac, adoptDeviceRequest));
    }

    @PostMapping("/reboot/{siteId}/{deviceMac}")
    public R<Boolean> rebootDevice(@PathVariable(required = true) String siteId, @PathVariable(required = true) String deviceMac) {
        return R.ok(sysDeviceService.rebootDevice(siteId, deviceMac));
    }

    @PostMapping("/updateConfig/{siteId}/{deviceMac}")
    public R<Boolean> updateConfig(@PathVariable(required = true) String siteId, @PathVariable(required = true) String deviceMac, @RequestBody(required = false) ApGeneralConfig apGeneralConfig) {
        return R.ok(sysDeviceService.updateConfig(siteId, deviceMac, apGeneralConfig));
    }

    @PostMapping("/getConfig/{siteId}/{deviceMac}")
    public R<ApGeneralConfig> getConfig(@PathVariable(required = true) String siteId, @PathVariable(required = true) String deviceMac) {
        return R.ok(sysDeviceService.getConfig(siteId, deviceMac));
    }

}
