package com.bonus.core.common.utils;

import cn.hutool.core.text.CharSequenceUtil;
import com.bonus.oss.OssProperties;
import com.bonus.oss.service.OssTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.stream.Collectors;

@Component
public class SysUtil {
   private static final Logger log = LoggerFactory.getLogger(SysUtil.class);
   @Value("${system.sysfile.prefixOffline}")
   private String prefixOffline;
   @Value("${system.sysfile.bucket}")
   private String prefixBucket;
   @Value("${system.sysfile.prefixOnline}")
   private String prefixOnline;
   private static final SysUtilProxy sysUtilProxy = new SysUtilProxy();
   private static String prefixOn;
   private static String bucket;
   private static String prefixOff;

   @PostConstruct
   public void init() {
      prefixOn = this.prefixOnline;
      bucket = this.prefixBucket;
      prefixOff = this.prefixOffline;
   }

   public static String getPrefix() {
      return sysUtilProxy.getPrefix(prefixOn, prefixOff);
   }

   public static String getBucket() {
      return bucket;
   }

   public static String getCutFileUrl(String fileUrl) {
      log.info("出参_补图_原路径:{}，前缀：{},存储桶：{}", new Object[]{fileUrl, getPrefix(), getBucket()});
      String cutFileUrl = null;
      try {
         cutFileUrl = getCutFileUrl(fileUrl, getPrefix(), getBucket());
      } catch (Exception e) {
         log.error("获取存储桶图片地址失败, " + e.getMessage());
         cutFileUrl = fileUrl;
      }
      log.info("出参_补图_补图后路径:{}", cutFileUrl);
      return cutFileUrl;
   }

   public static String getCutPath(String fileUrl) {
      log.info("入参_裁图_原路径:{}，前缀：{},存储桶：{}", new Object[]{fileUrl, getPrefix(), getBucket()});
      if (!CharSequenceUtil.isBlank(fileUrl) && !fileUrl.startsWith(getBucket())) {
         if (Boolean.FALSE.equals(getOssProperties().getUseToken())) {
            return fileUrl.replace(getPrefix(), getBucket());
         } else {
            fileUrl = getUnEncodeUrl(fileUrl);
            fileUrl = fileUrl.replace(getPrefix(), getBucket());
            fileUrl = getCutSignUrl(fileUrl);
            log.info("入参_裁图_裁剪后路径:{}", fileUrl);
            return fileUrl;
         }
      } else {
         return fileUrl;
      }
   }

   public static String getCutPathMulti(String fileUrl) {
      return CharSequenceUtil.contains(fileUrl, ",") ? (String)CharSequenceUtil.split(fileUrl, ",", true, true).stream().map(SysUtil::getCutPath).collect(Collectors.joining(",")) : getCutPath(fileUrl);
   }

   public static String getFilePath(String fileUrl) {
      if (CharSequenceUtil.isBlank(fileUrl)) {
         return fileUrl;
      } else {
         String cutPath = getCutPath(fileUrl);
         return cutPath.replace(getBucket(), "");
      }
   }

   public static String getObjectName(String fileUrl) {
      if (CharSequenceUtil.isBlank(fileUrl)) {
         return fileUrl;
      } else {
         String cutPath = getCutPath(fileUrl);
         String replace = cutPath.replace(getBucket(), "");
         if (CharSequenceUtil.startWith(replace, "/")) {
            replace = replace.substring(1);
         }

         return replace;
      }
   }

   public static OssTemplate getOssTemplate() {
      return (OssTemplate)SpringContextHolder.getBean(OssTemplate.class);
   }

   public static OssProperties getOssProperties() {
      return (OssProperties)SpringContextHolder.getBean(OssProperties.class);
   }

   public static String getUnEncodeUrl(String url) {
      if (CharSequenceUtil.isBlank(url)) {
         return url;
      } else {
         try {
            String decodedUrl = URLDecoder.decode(url, String.valueOf(StandardCharsets.UTF_8));
            return !decodedUrl.equals(url) ? decodedUrl : url;
         } catch (IllegalArgumentException var2) {
            log.info("图片url解码失败,图片url:{},异常信息_:{},详情_:", new Object[]{url, var2.getMessage(), var2});
            return url;
         } catch (UnsupportedEncodingException e) {
             throw new RuntimeException(e);
         }
      }
   }

   public static String getCutSignUrl(String fileUrl) {
      return CharSequenceUtil.isNotBlank(fileUrl) && fileUrl.contains("?") ? fileUrl.substring(0, fileUrl.indexOf("?")) : fileUrl;
   }

   public static String getCutFileUrl(String fileUrl, String filePrefix, String prefixBucket) {
      if (CharSequenceUtil.isBlank(fileUrl)) {
         return fileUrl;
      } else if (Boolean.FALSE.equals(getOssProperties().getUseToken())) {
         return !fileUrl.startsWith("http") && !fileUrl.startsWith("https") ? fileUrl.replace(prefixBucket, filePrefix) : fileUrl;
      } else {
         if (fileUrl.startsWith("http") || fileUrl.startsWith("https")) {
            fileUrl = fileUrl.replace(filePrefix + "/", "");
         }

         fileUrl = getCutSignUrl(fileUrl);
         fileUrl = fileUrl.replace(prefixBucket + "/", "");
         return getOssTemplate().getObjectURL(prefixBucket, fileUrl, getOssProperties().getExpiresTime());
      }
   }

   public static void removeOssObject(String fileUrl) {
      if (!CharSequenceUtil.isBlank(fileUrl)) {
         OssTemplate ossTemplate = (OssTemplate)SpringContextHolder.getBean(OssTemplate.class);
         OssProperties ossProperties = (OssProperties)SpringContextHolder.getBean(OssProperties.class);

         try {
            ossTemplate.removeObject(ossProperties.getBucketName(), getObjectName(fileUrl));
         } catch (Exception var4) {
            log.error("路径:{}删除异常:{}", new Object[]{fileUrl, var4.getMessage(), var4});
         }

      }
   }
}
