package cn.reghao.tnb.file.app.zoss.service;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.jdk.security.RandomString;
import cn.reghao.tnb.oss.api.constant.ChannelAction;
import cn.reghao.tnb.auth.model.OssPayload;
import cn.reghao.tnb.common.auth.LoginUser;
import cn.reghao.tnb.auth.JwtUtil;
import cn.reghao.tnb.file.app.zoss.db.mapper.UserKeyMapper;
import cn.reghao.tnb.file.app.zoss.model.dto.KeyAuthDto;
import cn.reghao.tnb.file.app.zoss.model.po.UserKey;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2024-02-27 10:25:25
 */
@Service
public class UserKeyService {
    private final String secretKey = "ossconsole.reghao.cn";
    private final UserKeyMapper userKeyMapper;

    public UserKeyService(UserKeyMapper userKeyMapper) {
        this.userKeyMapper = userKeyMapper;
    }

    public Result auth(KeyAuthDto keyAuthDto) {
        String accessKeyId = keyAuthDto.getAccessKeyId();
        UserKey userKey = userKeyMapper.findByAccessKeyId(accessKeyId);
        if (userKey == null) {
            return Result.fail("key not exist");
        }

        String accessKeySecret = keyAuthDto.getAccessKeySecret();
        if (userKey.getAccessKeySecret().equals(accessKeySecret)) {
            long loginUser = userKey.getCreateBy();
            String token = getTokenByUserId(loginUser);
            return Result.success(token);
        }

        return Result.fail("secret not matched");
    }

    private String getTokenByUserId(long loginUser) {
        long timestamp = System.currentTimeMillis() + 3600*24*365*1000L;
        String action = ChannelAction.all.getName();
        int channelCode = 1;
        OssPayload ossPayload = new OssPayload(channelCode, action, (int) loginUser);
        return JwtUtil.createOssToken(ossPayload, timestamp, secretKey);
    }

    public LoginUser getUserFromToken(String token) {
        try {
            OssPayload ossPayload = JwtUtil.getOssPayload(token, secretKey);
            int userId = ossPayload.getUserId();
            return new LoginUser(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void regenerate(int ossUser) {
        UserKey userKey = userKeyMapper.findByCreateBy(ossUser);
        if (userKey == null) {
            return;
        }

        String accessKeyId = RandomString.getString(8);
        String accessKeySecret = RandomString.getString(18);
        userKey.setAccessKeyId(accessKeyId);
        userKey.setAccessKeySecret(accessKeySecret);
        userKeyMapper.updateByCreateBy(userKey);
    }

    public List<UserKey> getUserKeys(int ossUser) {
        UserKey userKey = userKeyMapper.findByCreateBy(ossUser);
        if (userKey == null) {
            userKey = create(ossUser);
        }

        return List.of(userKey);
    }

    private UserKey create(int ossUser) {
        String accessKeyId = RandomString.getString(8);
        String accessKeySecret = RandomString.getString(20);
        UserKey userKey = new UserKey(accessKeyId, accessKeySecret, ossUser);
        userKeyMapper.save(userKey);
        return userKey;
    }
}
