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

import cn.reghao.file.api.iface.OssService;
import cn.reghao.tnb.file.app.zoss.rpc.StoreServiceWrapper;
import cn.reghao.tnb.oss.api.iface.ConsoleService;
import cn.reghao.tnb.oss.api.constant.ObjectScope;
import cn.reghao.tnb.oss.api.dto.ObjectInfo;
import cn.reghao.tnb.oss.api.dto.ServerInfo;
import cn.reghao.tnb.oss.api.dto.media.AudioInfo;
import cn.reghao.tnb.oss.api.dto.media.ConvertedImageInfo;
import cn.reghao.tnb.oss.api.dto.media.ImageInfo;
import cn.reghao.tnb.oss.api.dto.media.VideoInfo;
import cn.reghao.tnb.file.app.zoss.db.repository.StoreRepository;
import cn.reghao.tnb.file.app.model.constant.OssType;
import cn.reghao.tnb.file.app.zoss.model.po.UploadChannel;
import cn.reghao.tnb.file.app.service.StoreConfigService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2024-07-03 14:40:25
 */
@DubboService
@Service
public class StoreServiceWrapperRouter implements OssService {
    private final int ossType = 1;
    private final ConsoleService consoleService;
    private final StoreServiceWrapper storeServiceWrapper;
    private final StoreConfigService storeConfigService;
    private final StoreRepository storeRepository;

    public StoreServiceWrapperRouter(ConsoleService consoleService, StoreServiceWrapper storeServiceWrapper,
                                     StoreConfigService storeConfigService, StoreRepository storeRepository) {
        this.consoleService = consoleService;
        this.storeServiceWrapper = storeServiceWrapper;
        this.storeConfigService = storeConfigService;
        this.storeRepository = storeRepository;
    }

    @Override
    public ServerInfo getUploadStore(String channelName) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        UploadChannel uploadChannel = storeRepository.getUploadChannel(ossUser, channelName);
        int channelCode = uploadChannel.getChannelCode();
        return consoleService.getUploadStore(channelCode);
    }

    @Override
    public Integer getChannelScope(int channelCode) {
        /*try {
            if (ossType == OssType.localOss.getCode()) {
                OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
                return ossConsoleClient.getChannelScope(channelCode);
            } else if (ossType == OssType.aliyunOss.getCode()) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return ObjectScope.PRIVATE.getCode();
    }

    @Override
    public void setObjectUpload(int channelCode, String objectId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            storeServiceWrapper.setObjectUpload(channelCode, objectId, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    @Override
    public void setObjectScope(int channelCode, String objectId, int scope) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            storeServiceWrapper.setObjectScope(channelCode, objectId, scope, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    @Override
    public void deleteByObjectId(int channelCode, String objectId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            storeServiceWrapper.deleteByObjectId(channelCode, objectId, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    @Override
    public void deleteByObjectUrl(String objectUrl) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            storeServiceWrapper.deleteByObjectUrl(objectUrl, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
    }

    @Override
    public ObjectInfo getObjectInfo(int channelCode, String objectId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            return storeServiceWrapper.getObjectInfo(channelCode, objectId, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }

    @Override
    public String getSignedUrl(int channelCode, String objectId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            return storeServiceWrapper.getSignedUrl(channelCode, objectId, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }

    @Override
    public String getSignedUrlByUrl(int channelCode, String objectUrl) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            return storeServiceWrapper.getSignedUrlByUrl(channelCode, objectUrl, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }

    @Override
    public VideoInfo getVideoInfo(int channelCode, String videoFileId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        return storeServiceWrapper.getVideoInfo(channelCode, videoFileId, ossUser);
    }

    @Override
    public ImageInfo getImageInfo(int channelCode, String imageFileId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            return storeServiceWrapper.getImageInfo(channelCode, imageFileId, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }

    @Override
    public ConvertedImageInfo getWebpInfo(int channelCode, String imageFileId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            return storeServiceWrapper.getWebpInfo(channelCode, imageFileId, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }
        throw new Exception("OSSType not found");
    }

    @Override
    public AudioInfo getAudioInfo(int channelCode, String audioFileId) throws Exception {
        int ossUser = storeConfigService.getLocalOssUser();
        if (ossType == OssType.localOss.getCode()) {
            return storeServiceWrapper.getAudioInfo(channelCode, audioFileId, ossUser);
        } else if (ossType == OssType.aliyunOss.getCode()) {
        }

        throw new Exception("OSSType not found");
    }
}
