package jnpf.doclibrary.controller;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PaginationVO;
import jnpf.doclibrary.service.DocLibraryService;
import jnpf.model.doclibrary.dto.FileQueryDto;
import jnpf.model.doclibrary.dto.SpaceDto;
import jnpf.model.doclibrary.vo.InformationAuthorityVo;
import jnpf.model.doclibrary.vo.InformationVo;
import jnpf.util.UserProvider;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资料库控制器
 *
 * @author yanwenfu
 * @create 2023-07-20
 */
@RestController
@RequestMapping(value = "/doclibrary")
public class DocLibraryController {

    @Resource
    private DocLibraryService docLibraryService;
    @Resource
    private UserProvider userProvider;

    /**
     * 获取-与我相关的文件列表
     * @param fileQueryDto 搜索参数
     * @return 返回值
     */
    @GetMapping("/relatedToMeFile")
    public ActionResult relatedToMeFile(FileQueryDto fileQueryDto)  {
        fileQueryDto.setUserId(userProvider.get().getUserId());
        List<InformationVo> vo = docLibraryService.relatedToMeFile(fileQueryDto);
        return ActionResult.success(vo);
    }

    /**
     * 获取-与我相关的文件分页列表
     * @param fileQueryDto 搜索参数
     * @return 返回值
     */
    @GetMapping("/relatedToMeFile/page")
    public ActionResult relatedToMeFilePage(FileQueryDto fileQueryDto)  {
        fileQueryDto.setUserId(userProvider.get().getUserId());
        PageInfo<InformationVo> page = docLibraryService.relatedToMeFilePage(fileQueryDto);
        PaginationVO paginationVO = new PaginationVO();
        paginationVO.setTotal((int) page.getTotal());
        paginationVO.setPageSize((long) page.getPageSize());
        paginationVO.setCurrentPage((long) page.getPageNum());
        return ActionResult.page(page.getList(), paginationVO);
    }

    /**
     * 获取--最近查看
     * @param fileQueryDto 搜索参数
     * @return 返回值
     */
    @GetMapping("/recentlyViewed")
    public ActionResult recentlyViewed(FileQueryDto fileQueryDto)  {
        fileQueryDto.setUserId(userProvider.get().getUserId());
        List<InformationVo> vo = docLibraryService.recentlyViewed(fileQueryDto);
        return ActionResult.success(vo);
    }

    /**
     * 获取--最近查看分页
     * @param fileQueryDto 搜索参数
     * @return 返回值
     */
    @GetMapping("/recentlyViewed/page")
    public ActionResult recentlyViewedPage(FileQueryDto fileQueryDto)  {
        fileQueryDto.setUserId(userProvider.get().getUserId());
        PageInfo<InformationVo> page = docLibraryService.recentlyViewedPage(fileQueryDto);
        PaginationVO paginationVO = new PaginationVO();
        paginationVO.setTotal((int) page.getTotal());
        paginationVO.setPageSize((long) page.getPageSize());
        paginationVO.setCurrentPage((long) page.getPageNum());
        return ActionResult.page(page.getList(), paginationVO);
    }
    /**
     * 重命名
     * @param id 空间/文件夹/文件id
     * @param fileQueryDto 修改后参数
     * @return 返回值
     */
    @PutMapping("/rename/{id}")
    public ActionResult rename(@PathVariable("id" ) String id,@RequestBody FileQueryDto fileQueryDto)  {
        UserInfo userInfo = userProvider.get();
        fileQueryDto.setUserId(userInfo.getUserId());
        fileQueryDto.setUserName(userInfo.getUserName());
        int i = docLibraryService.rename(id,fileQueryDto);
        if (i == -1){
            return ActionResult.fail("您没有权限!");
        }
        return ActionResult.success();
    }

    /**
     * 获取空间/文件夹/文件的权限
     * @param id 空间/文件夹/文件id
     * @return 返回值
     */
    @GetMapping("/userPermissions/{id}")
    public ActionResult userPermissions(@PathVariable("id" ) String id)  {
        int i = docLibraryService.userPermissions(id);
        if (i == -1){
            return ActionResult.fail("您没有权限!");
        }
        return ActionResult.success(i);
    }

    /**
     * 删除空间/文件夹/文件
     * @param id 空间/文件夹/文件id
     * @return 返回值
     */
    @DeleteMapping("/deleteInformation/{id}")
    public ActionResult deleteInformation(@PathVariable("id" ) String id)  {
        int i = docLibraryService.deleteInformation(id);
        if (i == -1){
            return ActionResult.fail("您没有权限!");
        }
        return ActionResult.success();
    }


    /** 
     * 空间/文件夹/文件下成员列表
     * @param id 空间/文件夹/文件id
     * @return 返回值
     */
    @GetMapping("/userList/{id}")
    public ActionResult userList(@PathVariable("id" ) String id)  {
        //id如果是空间id 那么只用验证是不是空间的成员
        if(!docLibraryService.checkSpaceMember(id)){
            int i = docLibraryService.userPermissions(id);
            if (i < 0 || i >= 2){
                return ActionResult.fail("您没有权限!");
            }
        }
        return ActionResult.success(docLibraryService.userList(id));
    }

    /**
     * 转移所有权
     * @param id 空间/文件夹/文件id
     * @return 返回值
     */
    @PutMapping("/transferOwnership/{id}")
    public ActionResult transferOwnership(@PathVariable("id" ) String id,@RequestBody FileQueryDto fileQueryDto)  {
        int i = docLibraryService.userPermissions(id);
        if (i != 0 ){
            return ActionResult.fail("您没有权限!");
        }
        docLibraryService.transferOwnership(id,fileQueryDto);
        return ActionResult.success();
    }

    /** 
     * 成员权限编辑
     * @param id  空间/文件夹/文件id
     * @param informationAuthorityVo 权限信息
     * @return 返回值
     */
    @PutMapping("/editPermissions/{id}")
    public ActionResult editPermissions(@PathVariable("id" ) String id,@RequestBody InformationAuthorityVo informationAuthorityVo)  {
        int i = docLibraryService.userPermissions(id);
        if (i != 0 && i != 1){
            return ActionResult.fail("您没有权限!");
        }
        if(null == informationAuthorityVo.getLimitsOfAuthority() || informationAuthorityVo.getLimitsOfAuthority() < 1 || informationAuthorityVo.getLimitsOfAuthority() >5){
            return ActionResult.fail("非法参数!");
        }
        docLibraryService.editPermissions(id,informationAuthorityVo);
        return ActionResult.success();
    }

    /**
     * 批量成员权限编辑
     * @param id  空间/文件夹/文件id
     * @param fileQueryDto 权限信息
     * @return 返回值
     */
    @PutMapping("/editPermissionsBatch/{id}")
    public ActionResult editPermissionsBatch(@PathVariable("id" ) String id,@RequestBody FileQueryDto fileQueryDto)  {
        int i = docLibraryService.userPermissions(id);
        if (i != 0 && i != 1){
            return ActionResult.fail("您没有权限!");
        }
        if (null != fileQueryDto.getInformationAuthorityVos() && fileQueryDto.getInformationAuthorityVos().size() >0) {
            for (InformationAuthorityVo informationAuthorityVo : fileQueryDto.getInformationAuthorityVos()) {
                if (null == informationAuthorityVo.getLimitsOfAuthority() || informationAuthorityVo.getLimitsOfAuthority() < 1 || informationAuthorityVo.getLimitsOfAuthority() > 5) {
                    return ActionResult.fail("非法参数!");
                }
            }
            docLibraryService.editPermissionsBatch(id, fileQueryDto);
        }
        return ActionResult.success();
    }
    /** 
     * 移除成员
     * @param id 空间/文件夹/文件id
     * @return 返回值
     */
    @DeleteMapping("/removeUser/{id}")
    public ActionResult removeUser(@PathVariable("id" ) String id, @RequestBody InformationAuthorityVo informationAuthorityVo)  {
        int i = docLibraryService.userPermissions(id);
        if (i != 0 && i != 1){
            return ActionResult.fail("您没有权限!");
        }
        docLibraryService.removeUser(informationAuthorityVo.getId());
        return ActionResult.success();
    }

    /**
     * 批量移除成员
     * @param id 空间/文件夹/文件id
     * @return 返回值
     */
    @DeleteMapping("/removeUserBatch/{id}")
    public ActionResult removeUserBatch(@PathVariable("id" ) String id,@RequestBody FileQueryDto fileQueryDto)  {
        int i = docLibraryService.userPermissions(id);
        if (i != 0 && i != 1){
            return ActionResult.fail("您没有权限!");
        }
        docLibraryService.removeUserBatch(fileQueryDto.getInformationAuthorityVos());
        return ActionResult.success();
    }

    /**
     * 移动文件
     * @param id 被移动的文件id
     * @param fileQueryDto 信息
     * @return 返回值
     */
    @PutMapping("/move/{id}")
    public ActionResult move(@PathVariable("id" ) String id,@RequestBody FileQueryDto fileQueryDto)  {
        int i = docLibraryService.userPermissions(id);
        if (i != 0 && i != 1 && i != 2){
            return ActionResult.fail("您没有权限!");
        }
        docLibraryService.move(id,fileQueryDto);
        return ActionResult.success();
    }

    /**
     * 新增共享空间
     * @param spaceDto 空间信息
     * @return 返回值
     */
    @PostMapping("/space")
    public ActionResult space(@RequestBody SpaceDto spaceDto)  {
        docLibraryService.space(spaceDto);
        return ActionResult.success();
    }
    /**
     * 批量新增成员
     * @param spaceDto 成员信息
     * @return 返回值
     */
    @PostMapping("/membersBatch")
    public ActionResult membersBatch(@RequestBody SpaceDto spaceDto)  {
        int i = docLibraryService.userPermissions(spaceDto.getId());
        if (i != 0 && i != 1){
            return ActionResult.fail("您没有权限!");
        }
        docLibraryService.membersBatch(spaceDto);
        return ActionResult.success();
    }


    /**
     * 新增文件夹
     * @param spaceDto 空间信息
     * @return 返回值
     */
    @PostMapping("/folder")
    public ActionResult folder(@RequestBody SpaceDto spaceDto)  {
        docLibraryService.folder(spaceDto);
        return ActionResult.success();
    }

    /**
     * 退出空间
     * @param id 空间id
     * @return 返回值
     */
    @PutMapping("/exitSpace/{id}")
    public ActionResult exitSpace(@PathVariable("id" ) String id)  {
        Integer integer = docLibraryService.exitSpace(id);
        if (integer < 0){
            return ActionResult.fail("空间拥有者不能退出!");
        }
        return ActionResult.success();
    }

    /**
     * 退出空间按钮判断
     * @param id 空间id
     * @return 返回值
     */
    @GetMapping("/exitSpaceButton/{id}")
    public ActionResult exitSpaceButton(@PathVariable("id" ) String id)  {

        return ActionResult.success(docLibraryService.exitSpaceButton(id));
    }
}
