package com.ruoyi.cai.controller.app;

import com.ruoyi.cai.dto.app.query.AlbumAddReq;
import com.ruoyi.cai.dto.app.query.AlbumResetReq;
import com.ruoyi.cai.dto.app.query.IdRes;
import com.ruoyi.cai.dto.app.query.UserUpdateReq;
import com.ruoyi.cai.dto.app.vo.CurrentUserInfoVo;
import com.ruoyi.cai.dto.app.vo.user.CurrentUserUpdateInfoVo;
import com.ruoyi.cai.dto.app.vo.user.MemberInfoVo;
import com.ruoyi.cai.manager.CurrentUserManager;
import com.ruoyi.cai.service.UserAlbumService;
import com.ruoyi.cai.service.UserMemberService;
import com.ruoyi.cai.service.UserOnlineService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@Tag(name = "用户相关接口")
@Slf4j
public class UserAppController {
    @Autowired
    private CurrentUserManager currentUserManager;
    @Autowired
    private UserAlbumService userAlbumService;
    @Autowired
    private UserMemberService userMemberService;
    @Autowired
    private UserOnlineService userOnlineService;
    @GetMapping("/active_update")
    @Operation(summary = "更新用户在线状态")
    @Log(title = "更新用户在线状态", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Void> activeUpdate(){
        try {
            userOnlineService.activeUpdate();
        }catch (Exception e){
            log.error("更新当前用户失败");
        }
        return R.ok();
    }

    @GetMapping("/info")
    @Operation(summary = "当前用户信息")
    @Log(title = "当前用户信息", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<CurrentUserInfoVo> currentInfo(){
        return R.ok(currentUserManager.currentInfo());
    }

    @GetMapping("/updateInfo")
    @Operation(summary = "当前用户信息(用于编辑用户的时候查询,带审核数据)")
    @Log(title = "当前用户信息", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<CurrentUserUpdateInfoVo> currentUpdateInfo(){
        return R.ok(currentUserManager.currentUpdateInfo());
    }

    @GetMapping("/member")
    @Operation(summary = "当前会员信息")
    @Log(title = "当前会员信息", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<MemberInfoVo> member(@Parameter(description = "类型 0-普通会员 1-超级会员") Integer type){
        Long userId = LoginHelper.getUserId();
        if(type == null){
            type = 0;
        }else if(type != 0 && type != 1){
            type = 0;
        }
        return R.ok(userMemberService.memberApp(userId,type));
    }

    /*@PostMapping("/update/one")
    @Operation(summary = "完善用户必填信息")
    @Log(title = "完善用户必填信息", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Boolean> finishUser(@Validated @RequestBody FinishUserUpdateReq res){
        return R.ok(currentUserManager.finishUser(res));
    }*/

    @PostMapping("/update")
    @Operation(summary = "修改当前用户信息")
    @Log(title = "修改当前用户信息", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Boolean> userUpdate(@Validated @RequestBody UserUpdateReq res){
        currentUserManager.userUpdate(res);
        return R.ok();
    }

    @PostMapping("/album/del")
    @Operation(summary = "删除相册")
    @Log(title = "删除相册", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Boolean> userAlbumDel(@RequestBody IdRes res){
        return R.ok(userAlbumService.removeAlbum(res.getId()));
    }

    @PostMapping("/album/add")
    @Operation(summary = "新增相册")
    @Log(title = "新增相册", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Boolean> userAlbumAdd(@RequestBody AlbumAddReq res){
        res.setUserId(LoginHelper.getUserId());
        return R.ok(userAlbumService.addAlbum(res));
    }

    @PostMapping("/album/reset")
    @Operation(summary = "重排序相册",hidden = true)
    @Log(title = "重排序相册", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Boolean> userAlbumAdd(@RequestBody List<AlbumResetReq> res){
        return R.ok(userAlbumService.resetAlbum(res,LoginHelper.getUserId()));
    }

}
