package com.backendsys.modules.upload.utils.ObjectKey;

import cn.hutool.core.util.StrUtil;
import com.backendsys.modules.sdk.douyincloud.tos.service.DouyinTosService;
import com.backendsys.modules.sdk.tencentcloud.cos.service.TencentCosService;
import com.backendsys.modules.upload.entity.ObjectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
public class ObjectKeyUtil {

    @Value("${tencent.cos.accessible-domain}")
    private String TENCENT_DOMAIN;
    @Autowired
    private TencentCosService tencentCosService;

    @Value("${douyin.tos.domain}")
    private String DOUYIN_DOMAIN;
    @Autowired
    private DouyinTosService douyinTosService;

    /**
     * 按 target 分组
     */
    public Map<Integer, List<ObjectKey>> groupByTarget(List<ObjectKey> objectKeys) {
        return objectKeys.stream().collect(Collectors.groupingBy(ObjectKey::getTarget));
    }

    /**
     * 根据 url 获取 object_key
     */
    public ObjectKeyEntity urlToObjectKey(String url) {
        String prefix = null;
        Integer target = -1;
        if (url == null) return null;
        if (url.startsWith(TENCENT_DOMAIN)) {
            prefix = TENCENT_DOMAIN;
            target = 1;
        } else if (url.startsWith(DOUYIN_DOMAIN)) {
            prefix = DOUYIN_DOMAIN;
            target = 3;
        }
        // 去掉协议头（http://、https://）后，再取前缀
        if (StrUtil.isNotEmpty(prefix)) {
            int domainStart = url.indexOf(prefix);
            if (domainStart == -1) {
                return null;
            }
            // 去掉域名前缀，取后面的路径
            String object_key = url.substring(domainStart + prefix.length());
            // 去掉可能的前导斜杠
            if (object_key.startsWith("/")) {
                object_key = object_key.substring(1);
            }
            ObjectKeyEntity result = new ObjectKeyEntity();
            result.setObject_key(object_key);
            result.setTarget(target);
            return result;
        } else {
            return null;
        }
    }

    /**
     * 批量删除 ObjectKeys (By target)
     */
    public void deleteObjects(List<ObjectKey> objectKeys) {

        // 按 Target 分组 (-1:本地, 1:腾讯云, 2:阿里云, 3.抖音云)
        Map<Integer, List<ObjectKey>> groups = groupByTarget(objectKeys);

        // [腾讯云]
        List<ObjectKey> tencent_object_keys = groups.get(1);
        if (tencent_object_keys != null && !tencent_object_keys.isEmpty()) {
            List<String> object_keys = tencent_object_keys.stream().map(ObjectKey::getObject_key).collect(Collectors.toList());
            tencentCosService.deleteObjects(object_keys);
        }

        // [阿里云] (未开发)
        List<ObjectKey> ali_object_keys = groups.get(2);
        if (ali_object_keys != null && !ali_object_keys.isEmpty()) {
            List<String> object_keys = ali_object_keys.stream().map(ObjectKey::getObject_key).collect(Collectors.toList());
            System.out.println("(ali) object_keys (未开发) = " + object_keys);
        }

        // [抖音云]
        List<ObjectKey> douyin_object_keys = groups.get(3);
        if (douyin_object_keys != null && !douyin_object_keys.isEmpty()) {
            List<String> object_keys = douyin_object_keys.stream().map(ObjectKey::getObject_key).collect(Collectors.toList());
            douyinTosService.deleteObjects(object_keys);
        }

    }

}
