package pwc.taxtech.atms.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pwc.taxtech.atms.dpo.UserRoleInfo;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.dimension.DimensionValueUpdateDto;
import pwc.taxtech.atms.dto.navtree.NavTreeDto;
import pwc.taxtech.atms.dto.role.RoleDisplayDto;
import pwc.taxtech.atms.dto.role.RoleDto;
import pwc.taxtech.atms.dto.role.RoleDtoTree;
import pwc.taxtech.atms.dto.role.RolePermissionDto;
import pwc.taxtech.atms.dto.role.UpdateRoleInfo;
import pwc.taxtech.atms.dto.user.DimensionRole;
import pwc.taxtech.atms.dto.user.DimensionUser;
import pwc.taxtech.atms.dto.user.UserRoleDimensionValueDto;
import pwc.taxtech.atms.dto.user.UserRoleDto;
import pwc.taxtech.atms.dto.user.UserRoleUpdateDto;
import pwc.taxtech.atms.service.impl.RoleServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @see PwC.Tax.Tech.Atms.WebApi\Controllers\RoleController.cs
 */
@RestController
@RequestMapping("/api/v1/role/")
public class RoleController {

    private static Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleServiceImpl roleService;

//    @ApiOperation(value = "获取用户树形列表", notes = "返回所有用户的树形列表")
    @RequestMapping(value = "getRoleTreeList", method = RequestMethod.GET)
    public @ResponseBody
    List<NavTreeDto> getRoleTreeList() {
        logger.debug("RoleController getRoleTreeList");
        return roleService.getRoleTreeList();
    }

//    @ApiOperation(value = "获取所有用户角色", notes = "返回所有用户的角色")
    @RequestMapping(value = "getAllOwnUserRoleList", method = RequestMethod.GET)
    public @ResponseBody
    List<UserRoleInfo> getAllOwnUserRoleList() {
        logger.debug("RoleController getAllOwnUserRoleList");
        return roleService.getAllOwnUserRoleList();
    }

//    @ApiOperation(value = "获取可访问的所有机构", notes = "返回可访问的所有机构")
    @RequestMapping(value = "getAllUserRoleList", method = RequestMethod.GET)
    public @ResponseBody
    List<UserRoleInfo> getAllUserRoleList() {
        logger.debug("RoleController getAllUserRoleList");
        return roleService.getAllUserRoleList();
    }

//    @ApiOperation(value = "通过传入的用户Id获取该用户的所有角色")
    @RequestMapping(value = "getAllRoleListByUserID", method = RequestMethod.GET)
    public @ResponseBody
    List<UserRoleDto> getAllRoleListByUserId(@RequestParam("userID") String userId,
                                             @RequestParam("serviceTypeID") String serviceTypeId) {
        logger.debug("RoleController getAllRoleListByUserId");
        return roleService.getAllRoleListByUserId(userId, serviceTypeId);
    }

//    @ApiOperation(value = "获取所有角色")
    @RequestMapping(value = "displayByServiceGroup", method = RequestMethod.GET)
    public @ResponseBody
    List<RoleDtoTree> getRoleList() {
        logger.debug("RoleController getRoleList");
        return roleService.getRoleList();
    }

//    @ApiOperation(value = "获取角色下用户列表")
    @RequestMapping(value = "getUsersByRoleID", method = RequestMethod.GET)
    public @ResponseBody
    List<UserRoleDto> getUsersByRoleId(@RequestParam("roleId") String roleId) {
        return roleService.getUsersByRoleId(roleId);
    }

//    @ApiOperation(value = "获取角色下额外用户列表")
    @RequestMapping(value = "getExtraUsersByRoleID", method = RequestMethod.GET)
    public @ResponseBody
    List<UserRoleDto> getExtraUsersByRoleId(@RequestParam("roleId") String roleId) {
        return roleService.getExtraUsersByRoleId(roleId);
    }

//    @ApiOperation(value = "删除角色下用户")
    @RequestMapping(value = "removeUserRole", method = RequestMethod.POST)
    @Secured("role:edit")
    public @ResponseBody
    Boolean removeUserRole(@RequestParam("userID") String userId,
                           @RequestBody List<String> roleIdList, @RequestParam("serviceTypeID") String serviceTypeId) {
        roleService.removeUserRole(userId, roleIdList, serviceTypeId);
        return true;
    }

//    @ApiOperation(value = "Get role list by role type")
    @RequestMapping(value = "displayByRoleType", method = RequestMethod.GET)
    public @ResponseBody
    List<RoleDto> getRoleListByRoleType(@RequestParam("roleTypeId") String roleTypeId) {
        return roleService.getRoleListByRoleType(roleTypeId);
    }

//    @ApiOperation(value = "增加角色分类")
    @RequestMapping(value = "addRoleCategory", method = RequestMethod.GET)
    @Secured("roleCategory:add")
    public @ResponseBody
    Boolean addRoleCategory(@RequestParam("name") String roleCategoryName,
                            @RequestParam("roleCategoryID") String roleCategoryId) {
        roleService.addRoleCategory(roleCategoryName, roleCategoryId);
        return true;
    }

//    @ApiOperation(value = "更新角色分类名称")
    @RequestMapping(value = "updateRoleCategory", method = RequestMethod.GET)
    @Secured("roleCategory:edit")
    public @ResponseBody
    Boolean updateRoleCategory(@RequestParam("updateName") String roleCategoryName,
                               @RequestParam("id") String roleCategoryId) {
        roleService.updateRoleCategory(roleCategoryName, roleCategoryId);
        return true;
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "删除角色分类")
    @RequestMapping(value = "deleteRoleCategory", method = RequestMethod.GET)
    @Secured("roleCategory:edit")
    public @ResponseBody
    OperationResultDto deleteRoleCategory(@RequestParam("id") String roleCategoryId) {
        return roleService.deleteRoleCategory(roleCategoryId);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
//    @ApiOperation(value = "Add a role info")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @Secured("role:add")
    public @ResponseBody
    Map addRole(@RequestBody RoleDisplayDto roleDisplayDto) {

        Map result = new HashMap<>();
        if ("-1".equals(roleService.addRole(roleDisplayDto))) {
            result.put("data", -1);
        } else {
            result.put("data", roleService.getRoleList());
        }
        return result;
    }

//    @ApiOperation(value = "Update a role info")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    @Secured("role:edit")
    public void updateRole(@RequestBody UpdateRoleInfo updateRole, @RequestParam("roleID") String roleId) {

        roleService.updateRole(updateRole, roleId);
    }

//    @ApiOperation(value = "Validate if the role name is unique")
    @RequestMapping(value = "validateRoleNameUnique", method = RequestMethod.GET)
    public Boolean validateRoleNameUnique(@RequestParam("roleName") String roleName,
                                          @RequestParam("oldRoleName") String oldRoleName) {

        return roleService.validateRoleNameUnique(roleName, oldRoleName);
    }

//    @ApiOperation(value = "Need to check mapping between user and role, if yes not allow delete role")
    @RequestMapping(value = "checkReferenceforRole", method = RequestMethod.GET)
    public Boolean checkExistReference(@RequestParam("roleID") String roleId) {

        return roleService.checkUserRole(roleId);
    }

//    @ApiOperation(value = "Delete a role info")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @Secured("role:edit")
    public @ResponseBody
    List<RoleDtoTree> deleteRole(@RequestBody RoleDto roleDto) {

        roleService.deleteRole(roleDto);
        return roleService.getRoleList();
    }

//    @ApiOperation(value = "Get user role list by organizationId, dimensionId and dimensionValueId")
    @RequestMapping(value = "getUserRoleList", method = RequestMethod.GET)
    public @ResponseBody
    List<UserRoleInfo> getUserRoleList(@RequestParam("organizationID") String organizationId,
                                       @RequestParam("dimensionID") String dimensionId,
                                       @RequestParam("dimensionValueID") String dimensionValueId) {

        UserRoleDimensionValueDto userRoleDimensionValueDto = new UserRoleDimensionValueDto();
        userRoleDimensionValueDto.setOrganizationId(organizationId);
        userRoleDimensionValueDto.setDimensionId(dimensionId);
        userRoleDimensionValueDto.setDimensionValueId(dimensionValueId);
        return roleService.getUserRoleList(userRoleDimensionValueDto);
    }

//    @ApiOperation(value = "Get all role permission list by serviceType")
    @RequestMapping(value = "getAllRolePermission", method = RequestMethod.GET)
    public @ResponseBody
    List<RolePermissionDto> getAllRolePermission(
            @RequestParam("serviceTypeID") String serviceTypeId) {

        return roleService.getAllRolePermission(serviceTypeId);
    }

//    @ApiOperation(value = "更新用户角色列表")
    @RequestMapping(value = "updateUserRole", method = RequestMethod.POST)
    @Secured("role:edit")
    public Boolean updateUserRole(@RequestBody UserRoleUpdateDto userRoleDto) {

        roleService.updateUserRole(userRoleDto);
        return true;
    }

//    @ApiOperation(value = "用户详情页面,增加范围")
    @RequestMapping(value = "updateDimensionValues", method = RequestMethod.POST)
    @Secured("role:edit")
    public Boolean updateDimensionValues(@RequestBody DimensionValueUpdateDto dimensionValueUpdateDto) {

        roleService.updateDimensionValues(dimensionValueUpdateDto);
        return true;
    }

//    @ApiOperation(value = "用户详情页面,增加机构")
    @RequestMapping(value = "updateUserOrg", method = RequestMethod.POST)
    @Secured("role:edit")
    public Boolean updateUserOrg(@RequestBody List<String> orgIdList, @RequestParam("userID") String userId) {

        roleService.updateUserOrg(orgIdList, userId);
        return true;
    }

//    @ApiOperation(value = "获取维度,角色,用户列表", notes = "机构/卡片/机构卡片/点击用户数")
    @RequestMapping(value = "getDimensionRoleUserList", method = RequestMethod.GET)
    public @ResponseBody
    List<DimensionRole> getDimensionRoleUserList(@RequestParam("dimensionId") String dimensionId,
                                                 @RequestParam("dimensionValueId") String dimensionValueId) {
        return roleService.getDimensionRoleUserList(dimensionId, dimensionValueId);
    }

//    @ApiOperation(value = "获取某一个维度的 用户 和 机构列表,角色列表", notes = "机构/卡片/机构卡片/点击用户数")
    @RequestMapping(value = "getDimensionUserRoleList", method = RequestMethod.GET)
    public @ResponseBody
    List<DimensionUser> getDimensionUserRoleList(@RequestParam("dimensionId") String dimensionId,
                                                 @RequestParam("dimensionValueId") String dimensionValueId) {
        return roleService.getDimensionUserRoleList(dimensionId, dimensionValueId);
    }

//    @ApiOperation(value = "根据区域Id,获取区域用户角色信息", notes = "机构/卡片/区域卡片/选择区域/点击用户数")
    @RequestMapping(value = "getActiveUserRoleListByAreaId", method = RequestMethod.GET)
    public @ResponseBody
    List<UserRoleInfo> getActiveUserRoleListByAreaId(@RequestParam("areaId") String areaId) {
        return roleService.getActiveUserRoleListByAreaId(areaId);
    }
}