package cn.reghao.oss.store.service;

import cn.reghao.jutil.jdk.store.LocalStore;
import cn.reghao.jutil.jdk.store.LocalStores;
import cn.reghao.oss.store.api.dto.ObjectChannel;
import cn.reghao.oss.store.api.dto.StoreDiskDto;
import cn.reghao.oss.store.api.dto.StoreProperties;
import cn.reghao.oss.store.config.SpringProperties;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author reghao
 * @date 2024-03-01 08:53:28
 */
@Service
public class StoreLocalCache {
    private StoreProperties storeProperties;
    private final SpringProperties springProperties;

    public StoreLocalCache(SpringProperties springProperties) {
        this.springProperties = springProperties;
    }

    public void initOssStore(StoreProperties storeProperties) {
        this.storeProperties = storeProperties;
    }

    public void updateStoreProperties(StoreProperties storeProperties) {
        this.storeProperties = storeProperties;
    }

    public String getHost() {
        return springProperties.getHost();
    }

    public String getDomain() {
        return storeProperties != null ? storeProperties.getDomain() : null;
    }

    public String getReferer() {
        return storeProperties != null ? storeProperties.getReferer() : null;
    }

    public String getSecretKey() {
        return storeProperties != null ? storeProperties.getSecretKey() : null;
    }

    public StoreProperties getStoreProperties() {
        return storeProperties;
    }

    public List<StoreDiskDto> getStoreDisks() {
        List<StoreDiskDto> list = new ArrayList<>();
        for (LocalStore localStore : LocalStores.getLocalStores()) {
            String diskDir = localStore.getDiskDir();
            long total = localStore.getTotal();
            long avail = localStore.getAvailable();
            list.add(new StoreDiskDto(diskDir, total, avail));
        }

        return list;
    }
}
