package com.dji.sample.manage.controller;

import com.dji.sample.common.error.CommonErrorEnum;
import com.dji.sample.manage.log.annotation.Log;
import com.dji.sample.manage.log.enums.BusinessType;
import com.dji.sample.manage.model.dto.SignLoginDTO;
import com.dji.sample.manage.model.dto.UserDTO;
import com.dji.sample.manage.model.dto.UserLoginDTO;
import com.dji.sample.manage.service.IUserService;
import com.dji.sdk.common.HttpResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

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

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

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public HttpResultResponse login(@RequestBody UserLoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        return userService.userLogin(username, password, loginDTO.getFlag(),loginDTO.getGateway_sn());
    }

    @PostMapping("/getToken")
    @Log(title = "流媒体获取Token",businessType = BusinessType.OTHER)
    public HttpResultResponse getToken(HttpServletRequest request,@RequestBody UserLoginDTO loginDTO) {

        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        String clientId = loginDTO.getClientId();
        log.info("==========获取Token接口调用 ClientId:"+ clientId +",username:" + username + ",password:" + password + ",IP:" + request.getRemoteAddr());
        return userService.getToken(clientId, username, password);
    }

    @PostMapping("/token/refresh")
    public HttpResultResponse refreshToken(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader(PARAM_TOKEN);
        Optional<UserDTO> user = userService.refreshToken(token);

        if (user.isEmpty()) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return HttpResultResponse.error(CommonErrorEnum.NO_TOKEN.getMessage());
        }

        return HttpResultResponse.success(user.get());
    }

    @Log(title = "单点登录",businessType = BusinessType.OTHER)
    @PostMapping("/signLogin")
    public HttpResultResponse signLogin(@RequestBody SignLoginDTO loginDTO) {
        return userService.userLogin(loginDTO);
    }
}
