package pwc.taxtech.atms.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PathVariable;
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.UserDto;
import pwc.taxtech.atms.dpo.UserOrgDto;
import pwc.taxtech.atms.dpo.UserOrgRoleDto;
import pwc.taxtech.atms.dpo.UserRoleInfo;
import pwc.taxtech.atms.dto.LoginInputDto;
import pwc.taxtech.atms.dto.LoginOutputDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.analysis.AnalysisDomesticlParam;
import pwc.taxtech.atms.dto.organization.OrgRoleDtoList;
import pwc.taxtech.atms.dto.permission.UserPermissionDto;
import pwc.taxtech.atms.dto.permission.UserPermissionKeyDto;
import pwc.taxtech.atms.dto.user.UpdateParam;
import pwc.taxtech.atms.dto.user.UserAndUserRoleSaveDto;
import pwc.taxtech.atms.dto.user.UserOrganizationDto;
import pwc.taxtech.atms.dto.user.UserRoleDimensionValueDto;
import pwc.taxtech.atms.dto.user.UserRoleDisplayInfo;
import pwc.taxtech.atms.dto.user.UserRoleSaveDto;
import pwc.taxtech.atms.entity.User;
import pwc.taxtech.atms.service.impl.RoleServiceImpl;
import pwc.taxtech.atms.service.impl.UserAccountServiceImpl;
import pwc.taxtech.atms.service.impl.UserRoleServiceImpl;
import pwc.taxtech.atms.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/v1/user/")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private UserRoleServiceImpl userRoleService;
    @Autowired
    private RoleServiceImpl roleService;
    @Autowired
    private UserAccountServiceImpl userAccountService;

    // used to test,
    // http://localhost:8080/atms-api/api/v1/user/getUser?id=0906913f-f8c3-423c-b9b1-9ae1be647087
    @RequestMapping(value = "getUser", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public User getUser(@RequestParam("id") String id) {
        return userService.getUser(id);
    }

//    @ApiOperation(value = "获取指定用户的权限", notes = "获取用户的权限级别,可访问的模块以及页面")
    @RequestMapping(value = "getUserPermission", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UserPermissionDto getUserPermission(@RequestParam("userName") String userName) {
        return userService.getUserPermission(userName);
    }

//    @ApiOperation(value = "获取指定用户的权限(新接口)", notes = "获取用户的权限级别,可访问的模块以及页面")
    @RequestMapping(value = "getUserPermissionKey", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UserPermissionKeyDto getUserPermissionKey(@RequestParam("userName") String userName) {
        return userService.getUserPermissionKey(userName);
    }

    @RequestMapping(value = "login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public OperationResultDto<LoginOutputDto> login(@RequestBody(required = false) LoginInputDto input) {
        logger.debug("enter login");
        OperationResultDto<LoginOutputDto> result = userService.login(input);
        return result;
    }

//    @ApiOperation(value = "获取所有的用户角色列表")
    @RequestMapping(value = "getAllUserRoleList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<UserRoleDisplayInfo> getAllUserRoleList(@RequestParam("serviceTypeID") String serviceTypeId) {
        return userRoleService.getAllUserRoleList(serviceTypeId);
    }

//    @ApiOperation(value = "根据传入的用户Id获取该用户以及所属机构的信息")
    @RequestMapping(value = "getUser/{userId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UserDto getUserById(@PathVariable("userId") String userId) {
        return userService.getUserById(userId);
    }

//    @ApiOperation(value = "通过传入的用户Id获取该用户所有的角色", notes = "通过用户获取可访问不可访问的所有机构,包括维度上继承下来的")
    @RequestMapping(value = "getUserRoleByUserID", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public OrgRoleDtoList getUserRoleByUserId(@RequestParam("userID") String userId) {
        return userRoleService.getUserRoleByUserId(userId);
    }

//    @ApiOperation(value = "根据传入的用户Id获取该用户的角色机构等信息")
    @RequestMapping(value = "displaySingle", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UserAndUserRoleSaveDto getSingleUserByUserId(@RequestParam("userId") String userId) {
        return userRoleService.getSingleUserByUserId(userId);
    }

//    @ApiOperation(value = "获取用户的默认角色")
    @RequestMapping(value = "getUserRoleListByUserId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UserRoleDisplayInfo getUserRoleListByUserId(@RequestParam("userId") String userId) {
        return userRoleService.getUserRoleListByUserId(userId);
    }

//    @ApiOperation(value = "通过机构Id和用户Id获取该用户的默认角色")
    @RequestMapping(value = "getUserRoleByOrgIdUserId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UserOrganizationDto getUserRoleByOrgIdUserId(@RequestParam("userId") String userId,
                                                        @RequestParam("orgId") String orgId) {
        return userRoleService.getUserRoleByOrgId(userId, orgId);
    }

//    @ApiOperation(value = "为角色添加用户")
    @RequestMapping(value = "addUsersToRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Secured("user:add")
    public @ResponseBody
    Boolean addUsersToRole(@RequestBody UserRoleSaveDto userRoleSaveDto) {
        roleService.addUsersToRole(userRoleSaveDto.getRoleId(), userRoleSaveDto.getServiceTypeId(),
                userRoleSaveDto.getUserIdList());
        return true;
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "启用或停用用户")
    @Secured("user:edit")
    @RequestMapping(value = "enableordisableuser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto enableOrDisableUser(@RequestBody UpdateParam updateParam) {
        return userRoleService.enableOrDisableUser(updateParam);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "为机构删除用户角色")
    @RequestMapping(value = "deleteUserRoleForOrg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Secured("user:edit")
    public @ResponseBody
    OperationResultDto deleteUserRoleForOrg(@RequestBody UserOrgDto userDto) {
        logger.debug("enter deleteUserRoleForOrg");
        return userRoleService.deleteUserRoleByOrgId(userDto);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "机构删除,包括维度上的继承删除")
    @RequestMapping(value = "deleteUserRoleOrg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Secured("userRole:edit")
    public @ResponseBody
    OperationResultDto deleteUserRoleOrg(
            @RequestBody List<UserRoleDimensionValueDto> userRoleList) {
        return userRoleService.deleteUserRoleOrg(userRoleList);
    }

//    @ApiOperation(value = "获取事业部,产品线的值下的所有机构的用户权限(维度权限)", notes = "展开事业部卡片>用户>权限设置")
    @RequestMapping(value = "getUserRoleByDimensionValueId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<UserRoleDisplayInfo> getUserRoleByDimensionValueId(
            @RequestParam("parentDimensionId") String parentDimensionId,
            @RequestParam("dimensionValueId") String dimensionValueId) {
        return userRoleService.getUserRoleByDimensionValueId(parentDimensionId, dimensionValueId);
    }

//    @ApiOperation(value = "获取在某个维度下的机构的特殊角色,用户单独跟机构设置角色", notes = "展开事业部卡片>用户>权限设置")
    @RequestMapping(value = "getSpecialUserRoleByDimensionValueId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<UserRoleDisplayInfo> getSpecialUserRoleByDimensionValueId(
            @RequestParam("parentDimensionId") String parentDimensionId,
            @RequestParam("dimensionValueId") String dimensionValueId) {
        return userRoleService.getSpecialUserRoleByDimensionValueId(parentDimensionId, dimensionValueId);
    }

//    @ApiOperation(value = "获取用户维度角色列表", notes = "展开事业部卡片>用户")
    @RequestMapping(value = "getDimensionUserRoleList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<UserOrgRoleDto> getDimensionUserRoleList(@RequestParam("parentDimensionId") String parentDimensionId,
                                                         @RequestParam("dimensionValueId") String dimensionValueId) {
        return userRoleService.getDimensionUserRoleList(parentDimensionId, dimensionValueId);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "维度上权限用户删除")
    @RequestMapping(value = "deleteUserRoleDimension", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public OperationResultDto deleteUserRoleDimension(@RequestBody List<UserRoleDimensionValueDto> userRoleList) {
        return userRoleService.deleteUserRoleDimension(userRoleList);
    }

//    @ApiOperation(value = "为维度添加用户")
    @RequestMapping(value = "updateUserRoleForDimension", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<?> updateUserRoleForDimension(
            @RequestBody List<UserRoleDimensionValueDto> userRoleList) {
        return userRoleService.updateUserRoleForDimension(userRoleList);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "添加事业部的值的权限")
    @RequestMapping(value = "updateUserRoleDimension", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto updateUserRoleDimension(
            @RequestBody List<UserRoleDimensionValueDto> userRoleList) {
        return userRoleService.updateUserRoleDimension(userRoleList);
    }

//    @ApiOperation(value = "更新一个用户的信息")
    @RequestMapping(value = "update", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<User> updateUser(@RequestBody UserAndUserRoleSaveDto userDto) {
        return userService.updateUser(userDto);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "机构用户权限编辑")
    @RequestMapping(value = "updateUserRoleOrganization", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Secured("user:edit")
    public @ResponseBody
    OperationResultDto updateUserRoleOrganization(
            @RequestBody List<UserRoleDimensionValueDto> userRoleList) {
        return userRoleService.updateUserRoleOrganization(userRoleList);
    }

//    @ApiOperation(value = "添加一个新的用户")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Secured("user:add")
    public @ResponseBody
    OperationResultDto<User> addUser(@RequestBody UserAndUserRoleSaveDto userAndUserRoleSaveDto) {
        return userAccountService.addNewUser(userAndUserRoleSaveDto);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "删除可继承权限", notes = "用户管理>点击用户卡片>点击各机构设置数据的修改按钮>点击角色>取消设置下的允许继承>确定")
    @RequestMapping(value = "deleteUserOrg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Secured("user:edit")
    public @ResponseBody
    OperationResultDto deleteUserOrg(@RequestBody List<UserRoleDimensionValueDto> userRoleList) {
        return userService.deleteUserOrg(userRoleList);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "给机构添加用户", notes = "机构管理>点击机构>用户>添加用户>选中用户并提交")
    @RequestMapping(value = "updateUserRoleForOrg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Secured("user:edit")
    public @ResponseBody
    OperationResultDto updateUserRoleForOrg(
            @RequestBody List<UserRoleDimensionValueDto> userRoleList) {
        return userRoleService.updateUserRoleForOrg(userRoleList);
    }

    @SuppressWarnings("rawtypes")
//    @ApiOperation(value = "查询用户信息", notes = "税务运营管理平台>增值税申报")
    @RequestMapping(value = "getUserByName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    UserDto getUserByName(
            @RequestBody UserDto userParam) {
        return userRoleService.GetUserByUserName(userParam);
    }

    @RequestMapping(value = "downloadFile/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void downloadFile(@RequestBody List<UserRoleInfo> data, HttpServletResponse response) {
        logger.debug("enter downloadFile");
        String fileName="testFile";
        userService.downloadFile(response, data, fileName);
    }

}