package com.red.circle.external.inner.service.oss.impl;

import com.red.circle.component.oss.OssService;
import com.red.circle.component.oss.aliyun.service.AliYunStsService;
import com.red.circle.external.app.oss.LocalDiskStorageService;
import com.red.circle.external.app.oss.TencentCosStorageService;
import com.red.circle.external.inner.service.oss.OssServiceClientService;
import org.springframework.stereotype.Service;

/**
 * oss 服务api.
 *
 * @author pengliang on 2023/10/15
 */
@Service
public class OssServiceClientServiceImpl implements OssServiceClientService {

  private final OssService ossService;
  private final AliYunStsService aliYunStsService;
  private final LocalDiskStorageService localDiskStorageService;
  private final TencentCosStorageService tencentCosStorageService;

  public OssServiceClientServiceImpl(
      OssService ossService,
      AliYunStsService aliYunStsService,
      LocalDiskStorageService localDiskStorageService,
      TencentCosStorageService tencentCosStorageService) {
    this.ossService = ossService;
    this.aliYunStsService = aliYunStsService;
    this.localDiskStorageService = localDiskStorageService;
    this.tencentCosStorageService = tencentCosStorageService;
  }

  @Override
  public Object sts() {
    return aliYunStsService.getAcsResponse();
  }

  @Override
  public void remove(String url) {
    if (localDiskStorageService.isManagedUrl(url)) {
      localDiskStorageService.remove(url);
      return;
    }
    if (tencentCosStorageService.isManagedUrl(url)) {
      tencentCosStorageService.remove(url);
      return;
    }
    ossService.remove(url);
  }

  @Override
  public String getAccessUrl(String key) {
    if (localDiskStorageService.isManagedUrl(key)) {
      return key;
    }
    if (tencentCosStorageService.isManagedUrl(key)) {
      return key;
    }
    if (tencentCosStorageService.isEnabled()) {
      return tencentCosStorageService.getAccessUrl(key);
    }
    return ossService.getAccessUrl(key);
  }

  @Override
  public String getVideoCover(String key) {
    return ossService.getVideoCover(key);
  }

  @Override
  public void processFileSaveAs(String source, String target, String styleType) {
    if (localDiskStorageService.isManagedUrl(source)) {
      localDiskStorageService.processFileSaveAs(source, target);
      return;
    }
    if (tencentCosStorageService.isManagedUrl(source)) {
      tencentCosStorageService.processFileSaveAs(source, target);
      return;
    }
    ossService.processFileSaveAs(source, target, styleType);
  }

  @Override
  public String processImgSaveAsCompressZoom(String source, String target, int height) {
    if (localDiskStorageService.isManagedUrl(source)) {
      return localDiskStorageService.processImgSaveAsCompressZoom(source, target, height);
    }
    if (tencentCosStorageService.isManagedUrl(source)) {
      return tencentCosStorageService.processImgSaveAsCompressZoom(source, target, height);
    }
    return ossService.processImgSaveAsCompressZoom(source, target, height);
  }

  @Override
  public String processImgSaveAsCompressZoom(String source, int height) {
    if (localDiskStorageService.isManagedUrl(source)) {
      return localDiskStorageService.processImgSaveAsCompressZoom(source, height);
    }
    if (tencentCosStorageService.isManagedUrl(source)) {
      return tencentCosStorageService.processImgSaveAsCompressZoom(source, height);
    }
    return ossService.processImgSaveAsCompressZoom(source, height);
  }

}
