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

import cn.reghao.jutil.jdk.converter.ByteConverter;
import cn.reghao.jutil.jdk.converter.ByteType;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.file.app.zoss.service.UserNodeService;
import cn.reghao.tnb.oss.api.iface.StoreService;
import cn.reghao.tnb.oss.api.constant.ObjectScope;
import cn.reghao.tnb.oss.api.constant.ObjectType;
import cn.reghao.tnb.oss.api.dto.ObjectInfo;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.file.app.zoss.model.po.StoreNode;
import cn.reghao.tnb.file.app.zoss.model.po.UserNode;
import cn.reghao.tnb.file.app.zoss.model.vo.FileInfo;
import cn.reghao.tnb.file.app.zoss.rpc.RpcService;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.stereotype.Service;

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

/**
 * @author reghao
 * @date 2024-07-11 16:49:34
 */
@Service
public class OssFileService {
    private final ByteConverter byteConverter = new ByteConverter();
    private final UserNodeService userNodeService;
    private final RpcService rpcService;

    public OssFileService(UserNodeService userNodeService, RpcService rpcService) {
        this.userNodeService = userNodeService;
        this.rpcService = rpcService;
    }

    public PageList<FileInfo> getFileList(String nodeDomain, String objectName, int pn) {
        try {
            StoreNode storeNode = userNodeService.getStoreNodeByDomain(nodeDomain);
            if (storeNode == null) {
                return PageList.empty();
            }

            String host = storeNode.getNodeAddr();
            int port = storeNode.getRpcPort();
            StoreService storeService = rpcService.getStoreService(host, port);

            long owner = UserContext.getUserId();
            int pz = 10;
            PageList<ObjectInfo> pageList = storeService.getByPrefix((int) owner, objectName, pn, pz);
            List<FileInfo> list = pageList.getList().stream().map(this::getFileInfo).collect(Collectors.toList());
            return PageList.pageList(pn, pz, (int) pageList.getTotalSize(), list);
        } catch (Exception e) {
            if (e instanceof RpcException) {
                //throw new Exception("RPC 调用失败");
            }

            e.printStackTrace();
        }

        return PageList.empty();
    }

    private FileInfo getFileInfo(ObjectInfo objectInfo) {
        String size = byteConverter.convert(ByteType.Bytes, objectInfo.getSize());
        return new FileInfo(objectInfo, size);
    }

    public ObjectInfo getObjectInfo(int userNodeId, String objectId) throws Exception {
        UserNode userNode = userNodeService.getUserNode(userNodeId);
        String nodeDomain = userNode.getDomain();
        StoreNode storeNode = userNodeService.getStoreNodeByDomain(nodeDomain);
        if (storeNode == null) {
            return null;
        }

        String host = storeNode.getNodeAddr();
        int port = storeNode.getRpcPort();
        StoreService storeService = rpcService.getStoreService(host, port);
        ObjectInfo objectInfo = storeService.getObjectInfo(objectId);
        if (objectInfo != null) {
            if (objectInfo.getFileType() == ObjectType.Dir.getCode()) {
                return objectInfo;
            }

            int scope = objectInfo.getScope();
            if (scope == ObjectScope.PRIVATE.getCode()) {
                long owner = UserContext.getUserId();
                int expire = 3600;
                String signedVideoUrl = storeService.getSignedUrl(nodeDomain, (int) owner, objectId, expire);
                objectInfo.setUrl(signedVideoUrl);
            } else {
                String url = String.format("//%s/%s", nodeDomain, objectInfo.getUrl());
                objectInfo.setUrl(url);
            }
        }

        return objectInfo;
    }
}
