package cn.reghao.oss.store.service;

import cn.reghao.oss.api.dto.NodeProperties;
import cn.reghao.oss.api.iface.ConsoleService;
import cn.reghao.oss.api.util.AuthContext;
import cn.reghao.oss.store.config.props.OssProperties;
import cn.reghao.oss.api.dto.StoreNodeDto;
import cn.reghao.oss.api.dto.ObjectChannel;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2024-03-08 09:20:27
 */
@Service
public class StoreLocalService {
    private final ConsoleService consoleService;
    private final String nodeAddress;

    public StoreLocalService(OssProperties ossProperties, ConsoleService consoleService) {
        this.nodeAddress = ossProperties.getHost();
        this.consoleService = consoleService;
    }

    public void register(StoreNodeDto storeNodeDto) {
        consoleService.registerNode(storeNodeDto);
    }

    public String getDomain() {
        int loginUser = AuthContext.getUserId();
        NodeProperties nodeProperties = consoleService.getNodeProperties(loginUser, nodeAddress);
        return nodeProperties != null ? nodeProperties.getDomain() : null;
    }

    public String getReferer() {
        int loginUser = AuthContext.getUserId();
        NodeProperties nodeProperties = consoleService.getNodeProperties(loginUser, nodeAddress);
        return nodeProperties != null ? nodeProperties.getReferer() : null;
    }

    public String getSecretKey() {
        int loginUser = AuthContext.getUserId();
        NodeProperties nodeProperties = consoleService.getNodeProperties(loginUser, nodeAddress);
        return nodeProperties != null ? nodeProperties.getSecretKey() : null;
    }

    public ObjectChannel getChannelById(int loginUser, int channelId) {
        return consoleService.getChannelById(loginUser, channelId);
    }

    public int getChannelIdByUrl(int loginUser, String url) {
        return consoleService.getChannelIdByUrl(loginUser, url);
    }
}
