package com.njzscloud.supervisory.device.controller;

import cn.hutool.core.lang.Assert;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.device.pojo.entity.DeviceLocalizerEntity;
import com.njzscloud.supervisory.device.pojo.entity.LocalizerConfig;
import com.njzscloud.supervisory.device.pojo.param.SearchDeviceLocalizerParam;
import com.njzscloud.supervisory.device.pojo.result.SearchDeviceLocalizerResult;
import com.njzscloud.supervisory.device.service.DeviceLocalizerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定位器
 */
@Slf4j
@RestController
@RequestMapping("/device_localizer")
@RequiredArgsConstructor
public class DeviceLocalizerController {

    private final DeviceLocalizerService deviceLocalizerService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody DeviceLocalizerEntity deviceLocalizerEntity) {
        deviceLocalizerService.add(deviceLocalizerEntity);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody DeviceLocalizerEntity deviceLocalizerEntity) {
        deviceLocalizerService.modify(deviceLocalizerEntity);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        deviceLocalizerService.del(ids);
        return R.success();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<SearchDeviceLocalizerResult> detail(@RequestParam Long id) {
        return R.success(deviceLocalizerService.detail(id));
    }

    /**
     * 刷新设备配置
     */
    @GetMapping("/reflash_config")
    public R<SearchDeviceLocalizerResult> reflashConfig(@RequestParam Long id) {
        deviceLocalizerService.reflashConfig(id);
        return R.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<SearchDeviceLocalizerResult>> paging(PageParam pageParam, SearchDeviceLocalizerParam searchDeviceLocalizerParam) {
        return R.success(deviceLocalizerService.paging(pageParam, searchDeviceLocalizerParam));
    }

    /**
     * 修改设备配置
     */
    @PostMapping("/modify_config")
    public R<?> modifyConfig(@RequestBody LocalizerConfig config,
                             @RequestParam Long id
    ) {
        Assert.isTrue(SecurityUtil.isAdmin(), () -> Exceptions.clierr("非管理员不能操作"));
        deviceLocalizerService.modifyConfig(id, config);
        return R.success();
    }

    /**
     * 刷新设备
     */
    @GetMapping("/reflash_device")
    public R<?> reflash() {
        Assert.isTrue(SecurityUtil.isAdmin(), () -> Exceptions.clierr("非管理员不能操作"));
        deviceLocalizerService.reflash();
        return R.success();
    }

    @GetMapping("/send_directive")
    public R<?> sendDirective(@RequestParam Long id,
                              @RequestParam String directive
    ) {
        Assert.isTrue(SecurityUtil.isAdmin(), () -> Exceptions.clierr("非管理员不能操作"));
        deviceLocalizerService.sendDirective(id, directive);
        return R.success();
    }

    @GetMapping("/enable_track")
    public R<?> enableTrack(@RequestParam Long id) {
        Assert.isTrue(SecurityUtil.isAdmin(), () -> Exceptions.clierr("非管理员不能操作"));
        deviceLocalizerService.enableTrack(id);
        return R.success();
    }
}
