package com.tofly.auth.controller;

import com.tofly.common.core.constant.CommonConstants;
import com.tofly.common.core.constant.SecurityConstants;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/10/22  13:10
 */
@RestController
@RequestMapping("/oauth")
@Api(tags="登陆接口")
@Slf4j
public class OauthController {
    @Autowired
    private TokenEndpoint tokenEndpoint;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ValueOperations<String,String> valueOperations;

    private final Long SUCCESS=1L;

    @GetMapping("/token")
//    @ApiOperation(value = "使用get方式登陆")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
//    })
    public ResultRespone getAccessToken(Principal principal,@ApiParam(hidden = true) @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        return custom(tokenEndpoint.getAccessToken(principal, parameters).getBody());
    }

    @PostMapping("/token")
    @ApiOperation(value = "使用post方式登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })
    @ToFlyAppLog(title = "登录",type = CommonConstants.LOG_LOGIN)
    public ResultRespone postAccessToken(@ApiParam(hidden = true)Principal principal,@ApiParam(hidden = true) @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        return custom(tokenEndpoint.postAccessToken(principal, parameters).getBody());
    }

    @PostMapping("/login")
    @ApiOperation(value = "使用post方式登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })
    @ToFlyAppLog(title = "登录",type = CommonConstants.LOG_LOGIN)
    public ResultRespone login() throws HttpRequestMethodNotSupportedException {
        return null;
    }

    //自定义返回格式
    private ResultRespone custom(OAuth2AccessToken accessToken) {
        DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
        Map<String, Object> data = new LinkedHashMap(token.getAdditionalInformation());
        String tokenValue=token.getValue();
        data.put("accessToken", token.getValue());
        data.put("expiresIn",token.getExpiresIn());
        if (token.getRefreshToken() != null) {
            data.put("refreshToken", token.getRefreshToken().getValue());
        }

        String o = valueOperations.get(SecurityConstants.REDIS_LOGIN_LOCK);
        log.info("当前用户为：{},获取project为:{},token为：{}",data.get("username"),o,tokenValue);
        if(!StringUtil.isEmpty(o)){
            valueOperations.getOperations().delete(SecurityConstants.REDIS_LOGIN_LOCK);
            valueOperations.set(tokenValue,o);
            redisTemplate.expireAt(tokenValue, Date8Util.getEndTime());
        }
        return ResultRespone.success(data);
    }
//
//    private Boolean unlock(String key) {
//        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
//        RedisScript<String> redisScript = new DefaultRedisScript<>(script, String.class);
//        Object result = redisTemplate.execute(redisScript, Collections.singletonList(key));
//        if(SUCCESS.equals(result)) {
//            return true;
//        }
//        return false;
//    }
}
