package com.mcwl.web.controller.communityCenter;


import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.communityCenter.domain.dto.*;
import com.mcwl.communityCenter.domain.vo.CommunityDetailVo;
import com.mcwl.communityCenter.service.CommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 社区
 */
@Api(tags = "社区")
@RestController
@RequestMapping("community")
@RequiredArgsConstructor
public class CommunityController {

    private final CommunityService communityService;


    /**
     * 社区列表
     *
     * @return 社区列表
     */
    @ApiOperation(value = "社区列表")
    @PostMapping("list")
    public TableDataInfo getCommunityList(@RequestBody @Valid CommunityListPageRes communityListPageRes) {
        return communityService.listByPage(communityListPageRes);
    }

    @ApiOperation(value = "社区详情")
    @GetMapping("detail")
    public R<CommunityDetailVo> getCommunityDetail(@Valid
                                                   @ApiParam(value = "社区id", required = true)
                                                   @NotNull(message = "社区id不能为空")
                                                   Long tenantId,
                                                   @Valid
                                                   @ApiParam(value = "社区id", required = true)
                                                   @NotNull(message = "社区id不能为空")
                                                   Long communityId) {

        CommunityDetailVo communityDetailVo = communityService.getCommunityDetail(tenantId, communityId);
        return R.ok(communityDetailVo);
    }

    /**
     * 编辑社区
     */
    @ApiOperation(value = "编辑社区")
    @PostMapping("edit")
    public R<Object> editCommunity(@RequestBody @Valid EditCommunityRes editCommunityRes) {

        communityService.editCommunity(editCommunityRes);
        return R.ok("编辑成功");
    }


    /**
     * 我加入的社区
     */
    @ApiOperation(value = "我加入的社区")
    @PostMapping("myJoin")
    public TableDataInfo getMyJoinCommunity(@RequestBody JoinCommunityListPageRes joinCommunityListPageRes) {

        return communityService.getMyJoinCommunity(joinCommunityListPageRes);
    }


    /**
     * 添加社区
     */
    @ApiOperation(value = "添加社区")
    @PostMapping("addCommunity")
    public R<Object> addCommunity(@RequestBody @Valid AddCommunityRes addCommunityRes) {
        communityService.addCommunity(addCommunityRes);
        return R.ok("社区添加成功");
    }

    /**
     * 加入社区
     */
    @ApiOperation(value = "加入社区")
    @PostMapping("join")
    public R<Object> joinCommunity(@RequestBody @Valid JoinCommunityRes joinCommunityRes) {

        return communityService.joinCommunity(joinCommunityRes);
    }

    /**
     * 退出社区
     */
    @ApiOperation(value = "退出社区")
    @PostMapping("quit")
    public R<Object> quitCommunity(@RequestBody @Valid QuitCommunityRes quitCommunityRes) {
        Long tenantId = quitCommunityRes.getTenantId();
        Long communityId = quitCommunityRes.getCommunityId();

        return communityService.quitCommunity(tenantId, communityId);
    }
//
//    /**
//     * 删除社区
//     *
//     * @param id 社区id
//     * @return 删除结果
//     */
//    @ApiOperation(value = "删除社区")
//    @GetMapping("delete")
//    public R<Object> deleteCommunity(@NotNull(message = "id不能为空")
//                                     @ApiParam(value = "id", required = true)
//                                     @Valid
//                                     Long id) {
//        Community community = communityService.getById(id);
//        if (Objects.isNull(community)) {
//            return R.fail(HttpStatus.SHOW_ERROR_MSG, "该社区不存在");
//        }
//        communityService.removeById(id);
//        return R.ok();
//    }

    /**
     * 是否加入社区
     */
    @ApiOperation(value = "是否加入社区")
    @PostMapping("isJoin")
    public R<Object> isJoinCommunity(@RequestBody @Valid JoinCommunityRes joinCommunityRes) {

        return communityService.isJoinCommunity(joinCommunityRes);
    }

}
