package com.bocloud.nms.model.hillstone;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

//地址本
public class AddressBookModel extends HillStoneGenericModel {
    private Long vendorId; // 所属平台
    private String name; // 名称
    private String description; // 描述
    private Integer type; // 类型，页面上暂未展示，当前都是0
    private Boolean isIpv6; // 是否ipv6，页面上暂未展示，当前都是false
    /**
     * flag 0 该成员为被包含的地址成员 1 该成员为被排除的地址成员
     * ip ip地址
     * mask 子网掩码
     */
    private String ipMasks; // ip掩码，格式：JSON
    /**
     * ip ip地址
     * mask 反掩码
     */
    private String ipUnmasks; // ip反掩码，格式：JSON
    /**
     * flag 0 该成员为被包含的地址成员 1 该成员为被排除的地址成员
     * min 起始ip地址
     * max 结束ip地址
     */
    private String ipRanges; // ip范围，格式：JSON
    private String hostJson; // 主机名称，格式为String[]
    private List<RelationModel> addressBooks; // 地址条目
    private List<RelationModel> countryBooks; // 国家地区

    public Long getVendorId() {
        return vendorId;
    }

    public void setVendorId(Long vendorId) {
        this.vendorId = vendorId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Boolean getIsIpv6() {
        return isIpv6;
    }

    public void setIsIpv6(Boolean isIpv6) {
        this.isIpv6 = isIpv6;
    }

    public String getIpMasks() {
        return ipMasks;
    }

    public String getIpMasksString() {
        JSONArray ipMasks = Optional.ofNullable(this.getIpMasks()).map(JSONArray::parseArray).orElse(new JSONArray());
        JSONArray result = new JSONArray();
        for (Object ipMask : ipMasks) {
            JSONObject jsonObject = JSON.parseObject(ipMask.toString());
            String ipString = Ipv4.convertIp(jsonObject.getLong("ip"));
            jsonObject.put("ip", ipString);
            result.add(jsonObject);
        }
        return result.toJSONString();
    }

    //TODO：ipUnmasks/ipRanges转换 暂无数据
    public void setIpMasks(String ipMasks) {
        this.ipMasks = ipMasks;
    }

    public String getIpUnmasks() {
        return ipUnmasks;
    }

    public void setIpUnmasks(String ipUnmasks) {
        this.ipUnmasks = ipUnmasks;
    }

    public String getIpRanges() {
        return ipRanges;
    }

    public void setIpRanges(String ipRanges) {
        this.ipRanges = ipRanges;
    }

    public String getHostJson() {
        return hostJson;
    }

    public void setHostJson(String hostJson) {
        this.hostJson = hostJson;
    }

    public List<RelationModel> getAddressBooks() {
        return addressBooks;
    }

    public void setAddressBooks(List<RelationModel> addressBooks) {
        this.addressBooks = addressBooks;
    }

    public List<RelationModel> getCountryBooks() {
        return countryBooks;
    }

    public void setCountryBooks(List<RelationModel> countryBooks) {
        this.countryBooks = countryBooks;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        AddressBookModel book = (AddressBookModel) o;
        return Objects.equals(vendorId, book.vendorId) &&
            Objects.equals(name, book.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(vendorId, name);
    }
}
