package com.friday.tooldriver.utils;

import com.friday.operation.api.keepalive.constant.taieoc.TaieOcConstants;
import org.apache.commons.lang3.StringUtils;

public class NumberUtils {

    /**
     * 生成版本号信息
     * @param oldVersion
     * @param versionType, versionType值域：large/small/medium
     * @return
     */
    public static String generateVersion(String oldVersion, String versionType){
        if(StringUtils.isEmpty(oldVersion)){
            return TaieOcConstants.DEFAULT_VERSION;
        }
        String[] versionStr = oldVersion.split(TaieOcConstants.VERSION_TYPE_SPLIT_SYMBOL);
        switch (versionType){
            case TaieOcConstants.LARGE_TYPE:
                versionStr[0] = String.valueOf(Integer.parseInt(versionStr[0]) + 1 );
                return StringUtils.join(versionStr,TaieOcConstants.EN_DOT);
            case TaieOcConstants.MEDIUM_TYPE:
                versionStr[1] = String.valueOf(Integer.parseInt(versionStr[1]) + 1 >= 10 ? 0 : Integer.parseInt(versionStr[1]) + 1);
                versionStr[0] = String.valueOf(Integer.parseInt(versionStr[1]) == 0 ? Integer.parseInt(versionStr[0]) :
                        Integer.parseInt(versionStr[0]) + 1);
                return StringUtils.join(versionStr,TaieOcConstants.EN_DOT);
            case TaieOcConstants.SMALL_TYPE:
                versionStr[2] = String.valueOf(Integer.parseInt(versionStr[2]) + 1 >= 10 ? 0 : Integer.parseInt(versionStr[2]) + 1);
                versionStr[1] = String.valueOf(Integer.parseInt(versionStr[2]) != 0 ? Integer.parseInt(versionStr[1]) :
                        (Integer.parseInt(versionStr[1]) + 1 >= 10 ? 0 : Integer.parseInt(versionStr[1]) + 1 ));
                versionStr[0] = String.valueOf(Integer.parseInt(versionStr[2]) == 0 && Integer.parseInt(versionStr[1]) == 0 ? Integer.parseInt(versionStr[0]) + 1:Integer.parseInt(versionStr[0]));
                return StringUtils.join(versionStr,TaieOcConstants.EN_DOT);
            default:
                return null;
        }

    }
}
