package com.dji.sample.manage.controller;

import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.manage.log.annotation.Log;
import com.dji.sample.manage.log.enums.BusinessType;
import com.dji.sample.manage.model.dto.UserListDTO;
import com.dji.sample.manage.model.dto.UserLoginDTO;
import com.dji.sample.manage.model.dto.UserPilotDTO;
import com.dji.sample.manage.service.IUserService;
import com.dji.sdk.common.HttpResultResponse;
import com.dji.sdk.common.PaginationData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;

@Slf4j
@RestController
@RequestMapping("${url.manage.prefix}${url.manage.version}/users")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * Query the information of the current user.
     * @param request
     * @return
     */
    @GetMapping("/current")
    public HttpResultResponse getCurrentUserInfo(HttpServletRequest request) {
        CustomClaim customClaim = (CustomClaim)request.getAttribute(TOKEN_CLAIM);
        return userService.getUserByUsername(customClaim.getUsername(), customClaim.getWorkspaceId());
    }

    /**
     * Paging to query all users in a workspace.
     * @param page      current page
     * @param pageSize
     * @param workspaceId
     * @return
     */
    @GetMapping("/{workspace_id}/users")
    public HttpResultResponse<PaginationData<UserListDTO>> getUsers(@RequestParam(defaultValue = "1") Long page,
                                                                    @RequestParam(value = "page_size", defaultValue = "50") Long pageSize,
                                                                    @PathVariable("workspace_id") String workspaceId,
                                                                    String searchInfo) {
        PaginationData<UserListDTO> paginationData = userService.getUsersByWorkspaceId(page, pageSize, workspaceId,searchInfo);
        return HttpResultResponse.success(paginationData);
    }

    /**
     * Modify user information. Only mqtt account information is included, nothing else can be modified.
     * @param user
     * @param workspaceId
     * @param userId
     * @return
     */
    @PutMapping("/{workspace_id}/users/{user_id}")
    public HttpResultResponse updateUser(@RequestBody UserListDTO user,
                                         @PathVariable("workspace_id") String workspaceId,
                                         @PathVariable("user_id") String userId) {

        userService.updateUser(workspaceId, userId, user);
        return HttpResultResponse.success();
    }

    @PostMapping("/saveApiUser")
    @Log(title = "流媒体推送API User",businessType = BusinessType.OTHER)
    public HttpResultResponse saveApiUser(HttpServletRequest request,@RequestBody UserLoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        String clientId = loginDTO.getClientId();
        log.info("==========saveApiUser接口调用 ClientId:"+ clientId +",username:" + username + ",password:" + password + ",IP:" + request.getRemoteAddr());
        return userService.saveApiUser(clientId,username, password);
    }

    @PostMapping("/{workspace_id}/savePilotUser")
    public HttpResultResponse savePilotUser(HttpServletRequest request,@RequestBody UserPilotDTO pilotDTO,@PathVariable("workspace_id") String workspaceId) {
        String username = pilotDTO.getUsername();
        String password = pilotDTO.getPassword();
        log.info("==========savePilotUser接口调用  username:" + username + ",password:" + password + ",workspaceId:" + workspaceId + ",IP:" + request.getRemoteAddr());
        return userService.savePilotUser(username, password,workspaceId);
    }

    @GetMapping("/getPwd")
    public HttpResultResponse getPwd(@NotNull String userId) {
        log.info("==========getPwd接口调用  userId:" + userId );
        return userService.getPwd(userId);
    }

}
