package com.tourism.common.additional.utils;

import com.tourism.common.additional.model.FileUrlObject;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class UrlConvertUtils {

    public static List<String> urlConvert(String hostIpPort, String url){
        if(StringUtils.isBlank(url)){
            return new ArrayList<>();
        }
        ArrayList<String> arrayList = new ArrayList<>();
        if(url.contains("https")){
            arrayList.add(url);
        }else {
            List<FileUrlObject> fileUrlObjectList = JsonUtils.parseFileUrlArray(url, FileUrlObject.class);
            if (!CollectionUtils.isEmpty(fileUrlObjectList)) {
                //遍历每个元素的数组对象,对元素的url对象进行拼接
                for (FileUrlObject y : fileUrlObjectList) {
                    String hotPictureUrlsAfterConvert =
                            hostIpPort + "/" + y.getUploadPath() + "/" + y.getFilename();
                    arrayList.add(hotPictureUrlsAfterConvert);
                }
            }
        }
        return arrayList;
    }

    public static String urlConvertSingle(String hostIpPort, String url){
        if(StringUtils.isBlank(url)){
            return "";
        }
        if(url.contains("https")){
            return url;
        }else {
            FileUrlObject fileUrlObject = JsonUtils.parseFileUrlArray(url, FileUrlObject.class).get(0);
            if(fileUrlObject != null){
                return hostIpPort + "/" + fileUrlObject.getUploadPath() + "/" + fileUrlObject.getFilename();
            }
        }
        return "";
    }
}
