package net.northking.cctp.device.dto.model;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.base.Objects;
import io.swagger.annotations.ApiModel;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.StringUtils;

@JsonSerialize
@ApiModel(description = "设备系统版本Dto")
public class SystemVersionDto implements Comparable<SystemVersionDto> {
    private String versionType;
    private String versionCode;

    public String getVersionType() {
        return versionType;
    }

    public void setVersionType(String versionType) {
        this.versionType = versionType;
    }

    public String getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(String versionCode) {
        this.versionCode = versionCode;
    }

    public int compareVersionCode(String versionCodeStr) {
        String[] split1 = versionCodeStr.split("\\.");
        String[] split2 = this.getVersionCode().split("\\.");
        String[] thisVersionCodes;
        String[] otherVersionCodes;
        if (split1.length == 0) {
            otherVersionCodes = new String[]{versionCodeStr};
        } else {
            otherVersionCodes = split1;
        }
        if (split2.length == 0) {
            thisVersionCodes = new String[]{this.getVersionCode()};
        } else {
            thisVersionCodes = split2;
        }

        int i = 0;
        while (true) {
            if (i > thisVersionCodes.length - 1) {
                return 1;
            }
            if (i > otherVersionCodes.length - 1) {
                return -1;
            }
            if (Integer.valueOf(thisVersionCodes[i]) > Integer.valueOf(otherVersionCodes[i])) {
                return -1;
            } else if (Integer.valueOf(thisVersionCodes[i]) < Integer.valueOf(otherVersionCodes[i])) {
                return 1;
            } else {
                i++;
            }
        }
    }

    @Override
    public int compareTo(@NotNull SystemVersionDto o) {
        if (StringUtils.isEmpty(o.getVersionType()) || StringUtils.isEmpty(o.getVersionCode())) {
            return -1;
        }
        if (StringUtils.isEmpty(this.getVersionType()) || StringUtils.isEmpty(this.getVersionCode())) {
            return 1;
        }

        //A,B,C
        if (Double.valueOf(this.getVersionType()) - Double.valueOf(o.getVersionType()) > 0) {//A<B<C
            return 1;
        } else if (Double.valueOf(this.getVersionType()) - Double.valueOf(o.getVersionType()) < 0) {
            return -1;
        } else {
            return compareVersionCode(o.getVersionCode());
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SystemVersionDto that = (SystemVersionDto) o;
        return Objects.equal(versionType, that.versionType) &&
                Objects.equal(versionCode, that.versionCode);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(versionType, versionCode);
    }
}
