package com.brilliance.isc.common.context;

import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.util.Files;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Streams;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

@Component
public class AppEnv {
    @Value("${envConfig.rootPath}")
    private String rootPath;

    @Value("${envConfig.dataPath}")
    private String dataPath;

    @Value("${envConfig.fileLocalFlag}")
    private String fileLocalFlag;

    public String getRootPath(){
        return new File(rootPath).getAbsolutePath();
    }

    public String getDataPath(){
        return this.dataPath;
    }

    public String getFileLocalFlag(){
        return this.fileLocalFlag;
    }

    public String getSubDataPath(String parentDir,String fileName){
        return getDataPath()+ "/"  + parentDir +"/"+fileName;
    }

    public String getFullName(String parentDir,String fileName){
        return getRootPath() + File.separator + parentDir + File.separator + fileName;
    }

    public String getDataPath(String parentDir,String fileName){
        return getRootPath()+ File.separator + "DATA" + File.separator + parentDir +File.separator+fileName;
    }

    public  String addNodeToPath(String argPth,String argNode)
    {
        //! Function to concatenate a directorypath and a directory or filename
        //! Add"s a node to a relativ or absolute path. A relativ path stays relative.
        //! The resulting path has NO trailing backslash.
        //! CAUTION: Do not use this function for field names, as the used delimiter
        //! might be either "\\" or "/" depending on the execution environment.
        //!
        //! Corresponding functions: AddExtensionToName, GetPathofName, GetLastNode, Basename (TradeDesign function), GetFileExtension, GetNormalizedNodename
        // combine the two arguments depending whether they are empty
        String path = NULLSTR;
        String chr = NULLSTR;
        if( MdaUtils.isEmpty(argNode) )
        {
            path = Strings.trim(argPth);
        }
        else
        {
            // Get the last character of path
            argNode = argNode.toUpperCase().replace("\\", Files.separator);
            chr = Strings.mid(argPth,Strings.len(argPth),1);
            if( MdaUtils.compareTo(chr,"\\") == 0 || MdaUtils.compareTo(chr,"/") == 0 )
            {
                path = Strings.trim(argPth) + Strings.trim(argNode);
            }
            else
            {
                if( MdaUtils.isEmpty(argPth) )
                {
                    path = Strings.trim(argNode);
                }
                else
                {
                    // determine the delimiter used
                    path = Strings.trim(argPth) + getDirDel() + Strings.trim(argNode);
                }
            }
        }
        // Check for trailing delimiter
        chr = Strings.mid(path,Strings.len(path),1);
        if( MdaUtils.compareTo(chr,"\\") == 0 || MdaUtils.compareTo(chr,"/") == 0 )
        {
            path = Strings.mid(path,1,Strings.len(path) - 1);
        }
        return  path;

    }

    public  String getDirDel()
    {
        //! Function to return the directory delimiter of the current server operating system
        return Files.separator;
        // if( Platform.isUnix() )
        // {
        //   return  "/";
        // }
        // else
        // {
        //   return  "\\\\";
        // }
    }

    /**
     * 将 argNam 对应路径的文件内容写入  argStr
     **/
    public  void streamSaveInDirectory(IStream argStr, String argNam)
    {
        try {
            Streams.streamSave(argStr, argNam);
        }catch (Exception e){
            Files.fileMkdirs(getPathofName(argNam));
            Streams.streamSave(argStr,argNam);
        }
        return;
    }

    public  String getPathofName(String argPth)
    {
        //! Function to extract the path portion of a filename or a field reference.
        //! The part of the string up to the last Backslash is returned.
        //! On absolute names the leading backslash is not eliminated.
        //! If no backslash is found an empty string is returned.
        //!
        //! Corresponding functions: AddExtensionToName, AddNodeToPath, GetLastNode, Basename (TradeDesign function), GetFileExtension, GetNormalizedNodename
        // search for a backslash starting at the end
        String chr = NULLSTR;
        for(int  idx = Strings.len(argPth);idx >= 1;idx--)
        {
            chr = Strings.mid(argPth,idx,1);
            if( MdaUtils.compareTo(chr,"\\") == 0 || MdaUtils.compareTo(chr,"/") == 0 )
            {
                if( idx > 1 )
                {
                    // return all characters up to the character before the found backslash
                    return  Strings.trim(Strings.mid(argPth,1,idx - 1));
                }
                else
                {
                    // if the only backslash is the first character, return it
                    return  chr;
                }
            }
        }
        // When no backslash is found, return an empty path
        return  "";

    }
}
