package me.zhengjie.modules.front.rest;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.config.properties.FileProperties;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.front.domain.dto.BusUserVo;
import me.zhengjie.modules.front.domain.dto.LoginVo;
import me.zhengjie.modules.front.domain.dto.MyDeviceCriteria;
import me.zhengjie.modules.security.service.dto.JwtUserDto;
import me.zhengjie.modules.system.domain.BusDevice;
import me.zhengjie.modules.system.domain.BusUser;
import me.zhengjie.modules.system.service.BusDeviceService;
import me.zhengjie.modules.system.service.BusUserService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@Slf4j
@RestController("UserController")
@RequestMapping("api/front/user")
@Api(tags = "微信：用户管理")
public class BusUserController {

    @Autowired private BusUserService busUserService;
    @Autowired private BusDeviceService busDeviceService;
    @Autowired private FileProperties fileProperties;

    @Log("我的设备")
    @GetMapping(value = "myDevice")
    @ApiOperation("我的设备")
    public ResponseEntity<PageResult<BusDevice>> myDeviceList(MyDeviceCriteria criteria) {
        JwtUserDto jwtUser = (JwtUserDto) SecurityUtils.getCurrentUser();
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(busDeviceService.myDeviceList(jwtUser.getUser().getId(), page), HttpStatus.OK);
    }

    @Log("用户信息")
    @ApiOperation("获取用户信息")
    @GetMapping(value = "/info")
    public ResponseEntity<LoginVo> getLoginInfo() {
        JwtUserDto jwtUser = (JwtUserDto) SecurityUtils.getCurrentUser();
        BusUser busUser = busUserService.queryById(jwtUser.getUser().getId());
        LoginVo loginVo = new LoginVo();
        loginVo.setType("login");
        loginVo.setUid(jwtUser.getUser().getId());
        loginVo.setNikeName(busUser.getNickname());
        loginVo.setAvatar(busUser.getAvatar());
        loginVo.setPhone(busUser.getPhone());
        return new ResponseEntity<>(loginVo, HttpStatus.OK);
    }

    @Log("信息修改")
    @ApiOperation("修改用户信息")
    @PostMapping(value = "/update")
    public ResponseEntity<String> updateInfo(BusUserVo user) {
        JwtUserDto jwtUser = (JwtUserDto) SecurityUtils.getCurrentUser();
        BusUser upd = new BusUser();
        upd.setId(jwtUser.getUser().getId());
        if (StringUtils.isNotBlank(user.getAvatar())) {
            upd.setAvatar(user.getAvatar().trim());
        }
        if (StringUtils.isNotBlank(user.getNickname())) {
            upd.setNickname(user.getNickname().trim());
        }
        if (StringUtils.isNotBlank(user.getPhone())) {
            upd.setPhone(user.getPhone().trim());
        }
        busUserService.updateById(upd);
        return new ResponseEntity<>("修改成功", HttpStatus.OK);
    }

    @Log("上传图片")
    @ApiOperation("上传图片")
    @PostMapping(value = "/pictures")
    public ResponseEntity<String> uploadPicture(@RequestParam MultipartFile file) {
        // 判断文件是否为图片
        String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
        if(!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))){
            throw new BadRequestException("只能上传图片");
        }
        String type = FileUtil.getFileType(suffix);
        String path = fileProperties.getPath().getAvatar();
        File uploadFile = FileUtil.upload(file, path + type + File.separator);
        if(ObjectUtil.isNull(uploadFile)){
            throw new BadRequestException("上传失败");
        }
        String[] split = path.split("/");
        return new ResponseEntity<>("/" + split[split.length - 1] + "/" + type + "/" + uploadFile.getName(), HttpStatus.OK);
    }
}



