import { DataModelEventType } from "../models/DataModelEventType";

export default class StringUtils
{

    /**
     * 是否为空
     * @param str 
     */
    public static isEmpty(str:string):boolean{
        if(str==null||str==undefined||str.length==0){
            return true;
        }
        return false;
    }
     /** 
     * 替换全部字符串 
     * @param string src 源串 
     * @param string from_ch 被替换的字符 
     * @param string to_ch 替换的字符 
     *  
     * @return string 结果字符串 
     */  
    public static replaceAll(src:string,from_ch:string,to_ch:string):string
    { 
        return src.split(from_ch).join(to_ch);
    }

    /**
     * 参数替换
     *  @param  str
     *  @param  rest
     *  
     *  @example
     *
     *  var str:string = "here is some info '{0}' and {1}";
     *  trace(StringUtil.substitute(str, 15.4, true));
     *
     *  // this will output the following string:
     *  // "here is some info '15.4' and true"
     */
    public static substitute(str:string, ... rest):string
    {
        if (str == null) return '';
        
        // Replace all of the parameters in the msg string.
        var len:number = rest.length;
        var args:any[];
        if (len == 1 && rest[0] instanceof Array)
        {
            args = rest[0];
            len = args.length;
        }
        else
        {
            args = rest;
        }
        
        for (var i:number = 0; i < len; i++)
        {
            str = str.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
        }
        
        return str;
    }

    /**
     * 拆分字符串
     * @param str 
     */
    public static splitString(str:string):string[][]{
        let args:string[][] = new Array<string[]>();
        let tmp:string[] = str.split(",");
        tmp.forEach((val, key) => {
            let s:string[] = val.split(":");
            args.push(s);
        });
        return args;
    }

    /**
     * 转换成万，千万
     * @param num 
     */
    public static numberUtilsTenThousand(num:number,fixed:number=1):string{
        let args:string;
        let unit:string;
        if(num < 10000){
            unit = "";
        }else if(num < 10000000){
            num = num / 10000;
            unit = "万";
        }else{
            num = num / 10000000;
            unit = "千万";
        }
        args = String(num.toFixed(fixed));
        return (args + unit);
    }

    /**
     * 转换为KMB   10100 = 10.1k
     * @param num 
     */
    public static numberUtilsEn(num:number,fixed:number=1):string{
        let args:string;
        let unit:string;
        if(num < 10000){
            unit = "";
        }else if(num < 10000000){
            num = num / 1000;
            unit = "K";
        }else if(num < 100000000000){
            num = num / 1000000;
            unit = "M";
        }else{
            num = num / 10000000000;
            unit = "B";
        }
        args = String(num.toFixed(fixed));
        return (args + unit);
    }
    private static SecondsTime:number=1000;
    private static MinutesTime:number=60*1000;
    private static HoursTime:number=60*60*1000;
    private static DayTime:number=24*60*60*1000;
    private static MonthTime:number=30*24*60*60*1000;
    private static YearTime:number=365*30*24*60*60*1000;
    
  
    /**
     * 时间格式化
     * @param time 
     * @param yearSeparator         年
     * @param monthSeparator        月
     * @param daySeparator          天
     * @param hoursSeparator        时
     * @param minutesSeparator      分
     * @param secondsSeparator      秒
     */
    public static TimeFormatting(time:number,yearSeparator:string="y",monthSeparator:string="m",daySeparator:string="d",hoursSeparator:string="h",minutesSeparator:string="m",secondsSeparator:string="s"):string{
        let timeStr:string="";
        let index:number=0;
        //年
        let Year:number=0;
        while(time>this.YearTime){
            time-=this.YearTime;
            Year++;
        }
        if(Year>0){
            timeStr+=Year+yearSeparator;
            index++;
        }
        //月
        let Month:number=0;
        while(time>this.MonthTime){
            time-=this.MonthTime;
            Month++;
        }
        if(Month>0){
            if(Month<10){
                timeStr+="0"+monthSeparator;
            }else{
                timeStr+=Month+monthSeparator;
            }
            index++;
        }
        //天
        let Day:number=0;
        while (time>this.DayTime) {
            time-=this.DayTime;
            Day++;
        }
        if(Day>0){
            if(Day<10){
                timeStr+="0"+Day+daySeparator;
            }else{
                timeStr+=Day+daySeparator;
            }
            index++;
        }
        //时
        if(index<2){
            let Hours:number=0;
            while (time>this.HoursTime) {
                time-=this.HoursTime;
                Hours++;
            }
            if(Hours>0){
                if(Hours<10){
                    timeStr+="0"+Hours+hoursSeparator;
                }else{
                    timeStr+=Hours+hoursSeparator;
                }
                index++;
            }
        }else{
            return timeStr;
        }
        //分
        if(index<2){
            let Minute:number=0;
            while (time>this.MinutesTime) {
                time-=this.MinutesTime;
                Minute++;
            }
            if(Minute>0){
                if(Minute<10){
                    timeStr+="0"+Minute+minutesSeparator;
                }else{
                    timeStr+=Minute+minutesSeparator;
                }
                index++;
            }
        }else{
            return timeStr;
        }
        //秒
        if(index<2){
            let Seconds:number=0;
            while (time>this.SecondsTime) {
                time-=this.SecondsTime;
                Seconds++;
            }
            if(Seconds>0){
                if(Seconds<10){
                    timeStr+="0"+Seconds+secondsSeparator;
                }else{
                    timeStr+=Seconds+secondsSeparator;
                }
            }
        }
        return timeStr;
    }

    /**
     * 获取文件后缀名
     * @param url 
     */
    static getFileSuffix(url: string): string {
        let index:number=url.lastIndexOf(".");
        if(index<0){
            throw new Error(url+"没有后缀！！！");
        }
        let suixx: string = url.substring(index+1);
        return suixx.toLocaleUpperCase();
    }
    /**
     * 更换后缀
     */
    static changeFileSuffix(url: string, suff: string): string {
        let index:number=url.lastIndexOf(".");
        if(index<0){
            throw new Error(url+"没有后缀！！！");
        }
        let suixx:string = url.substring(index+1);
        let changeUrl:string = url.replace(suixx,suff);
        return changeUrl
	}

    /**
     * 换行文本
     */
    static getLineText(str:string):string{
        let text:string = "";
        let tmp:string[] = str.split("&");
        for(let i:number=0; i<tmp.length;i++){
            text = text + tmp[i];
            if(i != tmp.length-1){
                text = text+"\n"
            }
        }
        return text;
    }
}