package com.bocloud.cmp.provider.utils;

import java.math.BigInteger;
import java.util.List;

public abstract class IPAddress {
    /**
     * 验证IP地址
     *
     * @param address
     * @return
     */
    public abstract boolean validate(String address);

    /**
     * 得到默认网关
     *
     * @param cidr
     * @return
     */
    public abstract String getDefaultGeteway(String cidr);

    /**
     * 判断IP地址是否在cidr所划定的地址范围内
     *
     * @param address
     * @param cidr
     * @return
     */
    public abstract boolean isInRange(String address, String cidr);

    /**
     * 判断IP地址池是否在cidr所划定的地址范围内
     *
     * @param address
     * @param cidr
     * @return
     */
    public abstract boolean isInRange(List<String> pools, String cidr);

    /**
     * 判断IP地址是否在IP地址池范围内
     *
     * @param address
     * @param pools
     * @return
     */
    public abstract boolean isInPools(String address, List<String> pools);

    /**
     * 判断IP开始和结束地址是否在cidr所划定的地址范围内
     *
     * @param startIp
     * @param endIp
     * @param cidr
     * @return
     */
    public abstract boolean checkIpMask(String startIp, String endIp, String cidr);

    /**
     * 通过cidr和网关得到IP地址池
     *
     * @param gateway
     * @param cidr
     * @return
     */
    public abstract String getPools(String gateway, String cidr);

    /**
     * 得到IP掩码
     *
     * @param prefix
     * @return
     */
    public abstract String getMask(String prefix);

    /**
     * 根据掩码等到掩码位数
     *
     * @param mask
     * @return
     */
    public abstract int getMaskLength(String mask);

    public abstract BigInteger count(String cidr);

    public abstract BigInteger ipToInteger(String address);

    public abstract String integerToIp(BigInteger integer);

    public static IPAddress get(String version) {
        if ("v6".equalsIgnoreCase(version)) {
            return new IPv6Address();
        }
        return new IPv4Address();
    }

    public static String getVersion(String address) {
        IPAddress ipAddress = new IPv4Address();
        if (ipAddress.validate(address)) {
            return "V4";
        }
        return "V6";
    }

    public static IPAddress getIPAddress(String address) {
        IPAddress ipAddress = new IPv4Address();
        if (ipAddress.validate(address)) {
            return ipAddress;
        }
        return new IPv6Address();
    }

    public static boolean ipv4Validate(String address) {
        return new IPv4Address().validate(address);
    }

    public static boolean ipv6Validate(String address) {
        return new IPv6Address().validate(address);
    }
}
