package com.njcn.csdevice.controller.equipment;

import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.csdevice.service.DeviceFtpService;
import com.njcn.csharmonic.pojo.vo.MakeUpVo;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xy
 */
@RestController
@Slf4j
@Api(tags = "装置文件管理")
@AllArgsConstructor
@RequestMapping("/deviceFile")
public class DeviceFtpController extends BaseController {

    private final DeviceFtpService deviceFtpService;
    private final RedisUtil redisUtil;
    private final MqttPublisher publisher;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/askDeviceRootPath")
    @ApiOperation("设备根目录询问")
    @ApiImplicitParam(name = "nDid", value = "nDid", required = true)
    public HttpResult<MakeUpVo> askDeviceRootPath(@RequestParam("nDid") String nDid){
        String methodDescribe = getMethodDescribe("askDeviceRootPath");
        MakeUpVo vo = deviceFtpService.askDeviceRootPath(nDid);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, vo, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/askDeviceFileOrDir")
    @ApiOperation("设备文件/目录信息询问")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "nDid", value = "nDid", required = true),
        @ApiImplicitParam(name = "name", value = "文件路径名", required = true),
        @ApiImplicitParam(name = "type", value = "文件类型", required = true)
    })
    public HttpResult<List<MakeUpVo>> askDeviceFileOrDir(@RequestParam("nDid") String nDid, @RequestParam("name") String name, @RequestParam("type") String type){
        String methodDescribe = getMethodDescribe("askDeviceFileOrDir");
        List<MakeUpVo> list = deviceFtpService.askDeviceFileOrDir(nDid,name,type);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/downloadFile")
    @ApiOperation("设备文件下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nDid", value = "nDid", required = true),
            @ApiImplicitParam(name = "name", value = "文件路径名", required = true),
            @ApiImplicitParam(name = "size", value = "文件大小(单位byte)", required = true),
            @ApiImplicitParam(name = "fileCheck", value = "文件校验码", required = true)
    })
    public HttpResult<String> downloadFile(@RequestParam("nDid") String nDid, @RequestParam("name") String name, @RequestParam("size") Integer size, @RequestParam("fileCheck") String fileCheck){
        redisUtil.saveByKeyWithExpire("fileDownUserId"+nDid+name,RequestUtil.getUserIndex(),600L);
        deviceFtpService.downloadFile(nDid,name,size,fileCheck);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, "文件下载中,请稍等");
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getDownloadFilePath")
    @ApiOperation("获取文件下载路径")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nDid", value = "nDid", required = true),
            @ApiImplicitParam(name = "name", value = "文件路径名", required = true)
    })
    public HttpResult<String> getDownloadFilePath(@RequestParam("nDid") String nDid, @RequestParam("name") String name){
        String methodDescribe = getMethodDescribe("getDownloadFilePath");
        String result = deviceFtpService.getDownloadFilePath(nDid,name);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/createFolder")
    @ApiOperation("创建文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nDid", value = "nDid", required = true),
            @ApiImplicitParam(name = "path", value = "文件路径", required = true)
    })
    public HttpResult<String> createFolder(@RequestParam("nDid") String nDid, @RequestParam("path") String path){
        String methodDescribe = getMethodDescribe("createFolder");
        boolean result = deviceFtpService.createFile(nDid,path);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/deleteFolder")
    @ApiOperation("删除文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nDid", value = "nDid", required = true),
            @ApiImplicitParam(name = "path", value = "文件路径", required = true)
    })
    public HttpResult<String> deleteFolder(@RequestParam("nDid") String nDid, @RequestParam("path") String path){
        String methodDescribe = getMethodDescribe("deleteFolder");
        boolean result = deviceFtpService.deleteFile(nDid,path);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }
}
