package cn.reghao.oss.console.app.service;

import cn.reghao.jutil.jdk.converter.ByteConverter;
import cn.reghao.jutil.jdk.converter.ByteType;
import cn.reghao.oss.api.dto.ObjectChannel;
import cn.reghao.oss.console.app.db.repository.UploadChannelRepository;
import cn.reghao.oss.console.app.model.dto.ChannelScopeDto;
import cn.reghao.oss.console.app.model.po.StoreNode;
import cn.reghao.oss.console.app.model.po.UploadChannel;
import cn.reghao.oss.console.app.model.po.UserNode;
import cn.reghao.oss.console.app.model.vo.UploadChannelVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-02-23 15:14:53
 */
@Service
public class UploadChannelService {
    private final ByteConverter byteConverter = new ByteConverter();
    private final UploadChannelRepository uploadChannelRepository;
    private final UserNodeService userNodeService;

    public UploadChannelService(UploadChannelRepository uploadChannelRepository, UserNodeService userNodeService) {
        this.uploadChannelRepository = uploadChannelRepository;
        this.userNodeService = userNodeService;
    }

    public void updateScope(ChannelScopeDto channelScopeDto) {
        int id = channelScopeDto.getId();
        int newScope = channelScopeDto.getNewScope();
        UploadChannel uploadChannel = uploadChannelRepository.findById(id);
        if (uploadChannel != null) {
            int currentScope = uploadChannel.getScope();
            if (currentScope != newScope) {
                uploadChannel.setScope(newScope);
                uploadChannelRepository.save(uploadChannel);
            }
        }
    }

    public List<UploadChannelVo> getUploadChannels(int createBy) {
        List<UploadChannel> page = uploadChannelRepository.findByCreateBy(createBy);
        List<UploadChannelVo> list = page.stream().map(uploadChannel -> {
            long maxSize = uploadChannel.getMaxSize();
            String maxSizeStr = byteConverter.convert(ByteType.Bytes, maxSize);
            int nodeId = uploadChannel.getNodeId();
            UserNode userNode = userNodeService.getUserNode(nodeId, createBy);
            String bindDomain = userNode.getDomain();
            return new UploadChannelVo(uploadChannel, maxSizeStr, bindDomain);
        }).collect(Collectors.toList());

        return list;
    }

    public List<UploadChannel> getUploadChannelsByCreateBy(int createBy) {
        return uploadChannelRepository.findByCreateBy(createBy);
    }

    public UploadChannel getUploadChannel(int id) {
        return uploadChannelRepository.findById(id);
    }

    public UploadChannel getByChannelIdAndCreateBy(int channelId, int createBy) {
        return uploadChannelRepository.findByCreateByAndChannelId(createBy, channelId);
    }

    public ObjectChannel getObjectChannelByChannelId(int channelId, int createBy) {
        UploadChannel uploadChannel = uploadChannelRepository.findByCreateByAndChannelId(createBy, channelId);
        return getObjectChannel(uploadChannel);
    }

    private ObjectChannel getObjectChannel(UploadChannel uploadChannel) {
        int channelId = uploadChannel.getChannelId();
        //String name = uploadChannel.getName();
        String channelPrefix = uploadChannel.getPrefix();
        long maxSize = uploadChannel.getMaxSize();
        int fileType = uploadChannel.getFileType();
        boolean seturl = uploadChannel.getSeturl();
        int scope = uploadChannel.getScope();
        int createBy = uploadChannel.getCreateBy();

        UserNode userNode = userNodeService.getUserNode(uploadChannel.getNodeId(), createBy);
        String domain = userNode.getDomain();
        return new ObjectChannel(channelId, channelPrefix, maxSize, fileType, seturl, scope, domain, createBy);
    }

    public StoreNode getStoreNodeByChannelId(int loginUser, int channelId) throws Exception {
        UploadChannel uploadChannel = uploadChannelRepository.findByCreateByAndChannelId(loginUser, channelId);
        if (uploadChannel == null) {
            String errMsg = String.format("channel_id %s not exist", channelId);
            throw new Exception(errMsg);
        }

        UserNode userNode = userNodeService.getUserNode(uploadChannel.getNodeId(), uploadChannel.getCreateBy());
        String domain = userNode.getDomain();
        String ossUrl = String.format("//%s", domain);
        long maxSize = uploadChannel.getMaxSize();

        StoreNode storeNode = userNodeService.getByDomain(domain);
        if (storeNode == null) {
            String errMsg = String.format("store_node %s not exist", domain);
            throw new Exception(errMsg);
        }

        return storeNode;
    }

    public List<UploadChannel> getByDomainAndCreateBy(String domain, int createBy) {
        StoreNode storeNode = userNodeService.getByDomain(domain);
        int nodeId = storeNode.getId();
        return uploadChannelRepository.findByCreateByAndNodeId(createBy, nodeId);
    }
}
