package be.iminds.ilabt.jfed.rspec.model;

import be.iminds.ilabt.jfed.rspec.parser.ExtraXml;

import java.util.*;

/**
* SliverType
*/
public class SliverType {
    private final String name;
    private final List<DiskImage> diskImages;
    private final Integer xenCores;
    private final Integer xenRam;
    private final Integer xenDisk;
    private final List<ExtraXml> other;

//    SliverType(String name) {
//        this.name = name;
//        this.diskImages = new ArrayList<DiskImage>();
//        this.other = new ArrayList<ExtraXml>();
//        this.xenCores = null;
//        this.xenRam = null;
//        this.xenDisk = null;
//    }
//
//    SliverType(String name, Collection<ExtraXml> other) {
//        this.name = name;
//        this.diskImages = new ArrayList<DiskImage>();
//        this.other = other == null ? new ArrayList<ExtraXml>() : new ArrayList<ExtraXml>(other);
//        this.xenCores = null;
//        this.xenRam = null;
//        this.xenDisk = null;
//    }
//
//    SliverType(String name, Collection<DiskImage> diskImages, Collection<ExtraXml> other) {
//        this.name = name;
//        this.diskImages = diskImages == null ? new ArrayList<DiskImage>() : new ArrayList<DiskImage>(diskImages);
//        this.other = other == null ? new ArrayList<ExtraXml>() : new ArrayList<ExtraXml>(other);
//        this.xenCores = null;
//        this.xenRam = null;
//        this.xenDisk = null;
//    }

    //package local: use SliverTypeBuilder
    protected SliverType(String name, Collection<DiskImage> diskImages, Integer xenCores, Integer xenRam, Integer xenDisk, Collection<ExtraXml> other) {
        this.name = name;
        this.diskImages = diskImages == null ? new ArrayList<>() : new ArrayList<>(diskImages);
        this.other = other == null ? new ArrayList<>() : new ArrayList<>(other);
        this.xenCores = xenCores;
        this.xenRam = xenRam;
        this.xenDisk = xenDisk;
    }

    public String getName() {
        return name;
    }

    public List<ExtraXml> getExtraXml() {
        return Collections.unmodifiableList(other);
    }
    public List<DiskImage> getDiskImages() {
        return Collections.unmodifiableList(diskImages);
    }

    public Integer getXenCores() {
        return xenCores;
    }

    public Integer getXenRam() {
        return xenRam;
    }

    public Integer getXenDisk() {
        return xenDisk;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SliverType)) return false;

        SliverType that = (SliverType) o;

        if (!Objects.equals(diskImages, that.diskImages)) return false;
        if (!Objects.equals(name, that.name)) return false;
        if (!Objects.equals(other, that.other)) return false;
        if (xenCores != null ? !Objects.equals(xenCores, that.xenCores) : that.xenCores != null) return false;
        if (xenDisk != null ? !Objects.equals(xenDisk, that.xenDisk) : that.xenDisk != null) return false;
        if (xenRam != null ? !Objects.equals(xenRam, that.xenRam) : that.xenRam != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + diskImages.hashCode();
        result = 31 * result + (xenCores != null ? xenCores.hashCode() : 0);
        result = 31 * result + (xenRam != null ? xenRam.hashCode() : 0);
        result = 31 * result + (xenDisk != null ? xenDisk.hashCode() : 0);
        result = 31 * result + other.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "SliverType{" +
                "name='" + name + '\'' +
                ", diskImages=" + diskImages +
                (xenCores == null ? "" : ", xenCores=" + xenCores )+
                (xenRam == null ? "" : ", xenRam=" + xenRam )+
                (xenDisk == null ? "" : ", xenDisk=" + xenDisk )+
                ", other=" + other +
                '}';
    }

    public boolean hasXen() {
        return xenCores != null || xenDisk != null || xenRam != null;
    }
}
