/**
 * All rights Reserved, Designed By www.freemud.cn
 *
 * @Title:
 * @Package cn.freemud.enums
 * @Description:
 * @author: ping.wu
 * @date: 2018/12/7 11:07
 * @version V1.0
 * @Copyright: 2018 www.freemud.cn Inc. All rights reserved.
 * 注意：本内容仅限于上海非码科技内部传阅，禁止外泄以及用于其他的商业目
 */
package cn.freemud.utils;

import org.apache.commons.lang.StringUtils;

public class VersionUtil {

    private static final String OLD_VERSION = "1.5.1";

    private static final String NEW_VERSION = "1.5.2,1.5.3,1.5.4";

    /**
     * v1.5.4 新增
     */
    private static final String OLD_VERSION_BANNER = "1.5.1,1.5.2,1.5.3";

    public static boolean checkNewVersion(String version) {
        return VersionUtil.beforeVersion(OLD_VERSION, version);
    }

    public static boolean checkBannerVersion(String version) {
        if (StringUtils.isEmpty(version)) {
            return true;
        }
        return OLD_VERSION_BANNER.contains(version);
    }

    /**
     * v1.5.4新增
     * 如果返回true 代表 oldVersion < newVersion
     * 返回fasle 代表 oldVersion > newVersion
     *
     * @param oldVersion 更小的版本
     * @param newVersion 更大的版本
     * @return
     */
    public static boolean beforeVersion(String oldVersion, String newVersion) {
        boolean flag;
        String[] oldVersions = oldVersion.split("\\.");
        String[] newVersions = newVersion.split("\\.");

        int[] iCur = new int[oldVersions.length];
        int[] iSper = new int[newVersions.length];

        for (int i = 0; i < oldVersions.length; i++) {
            iCur[i] = Integer.parseInt(oldVersions[i]);
        }
        for (int i = 0; i < newVersions.length; i++) {
            iSper[i] = Integer.parseInt(newVersions[i]);
        }

        int iFlag;
        if (oldVersions.length < newVersions.length) {
            iFlag = compareTo(iSper, iCur);
            if (iFlag > 0) {
                flag = true;
            } else {
                flag = false;
            }
        } else {
            iFlag = compareTo(iCur, iSper);
            if (iFlag >= 0) {
                flag = false;
            } else {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * v1.5.4新增
     *
     * @param bigOne   大的数字
     * @param smallOne 小的数字
     * @return 1 说明 bigOne > smallOne
     * -1 说明bigOne < smallOne
     * 0 说明bigOne == smallOne
     */
    public static int compareTo(int[] bigOne, int[] smallOne) {
        int flag = 0;
        int temp;
        for (int i = 0; i < bigOne.length; i++) {
            if (i > smallOne.length - 1) {
                temp = 0;
            } else {
                temp = smallOne[i];
            }
            if (temp < bigOne[i]) {
                flag = 1;
                return flag;
            } else if (temp > bigOne[i]) {
                flag = -1;
                return flag;
            } else {
                if (i == bigOne.length - 1) {
                    flag = 0;
                }
            }
        }
        return flag;
    }


    /**
     * 获取版本号 默认153
     *
     * @param version
     * @return
     */
    public static Integer getVer(String version) {
        Integer versionValue = 153;
        if (StringUtils.isEmpty(version)) {
            return versionValue;
        }
        StringBuilder ver = new StringBuilder();
        try {
            String[] split = version.split("[.]");
            for (String aSplit : split) {
                ver.append(aSplit);
            }
            versionValue = Integer.parseInt(ver.toString());
        } catch (Exception e) {
            return versionValue;
        }
        return versionValue;
    }
}
