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

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

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

    public SliverTypeBuilder() {
        this.name = null;
        diskImages = new ArrayList<>();
        other = new ArrayList<>();
    }
    public SliverTypeBuilder(String name) {
        this.name = name;
        diskImages = new ArrayList<>();
        other = new ArrayList<>();
    }
    public SliverTypeBuilder(SliverType base) {
        this.name = base.getName();
        this.diskImages = new ArrayList<>(base.getDiskImages());
        this.other = new ArrayList<>(base.getExtraXml());
        this.xenCores = base.getXenCores();
        this.xenRam = base.getXenRam();
        this.xenDisk = base.getXenDisk();
    }

    public SliverType build() {
        if (name == null)
            throw new RuntimeException("should not make sliver_type without name");
        return new SliverType(name, diskImages, xenCores, xenRam, xenDisk, other);
    }

    public boolean isValid() {
        return name != null;
    }

    public SliverTypeBuilder xenCores(Integer i) {
        this.xenCores = i;
        return this;
    }

    public SliverTypeBuilder xenRam(Integer i) {
        this.xenRam = i;
        return this;
    }

    public SliverTypeBuilder xenDisk(Integer i) {
        this.xenDisk = i;
        return this;
    }

    public SliverTypeBuilder name(String name) {
        this.name = name;
        return this;
    }

    public SliverTypeBuilder setExtraXml(List<ExtraXml> extraXmlList) {
        this.other = new ArrayList<>(extraXmlList);
        return this;
    }

    public SliverTypeBuilder addExtraXml(ExtraXml extraXml) {
        this.other.add(extraXml);
        return this;
    }

    public SliverTypeBuilder setDiskImage(DiskImage di) {
        this.diskImages.clear();
        if (di != null)
            this.diskImages.add(di);
        return this;
    }

    public SliverTypeBuilder addDiskImage(DiskImage diskImage) {
        if (diskImage == null) throw new IllegalArgumentException("diskImage argument may not be null");
        this.diskImages.add(diskImage);
        return this;
    }
    public SliverTypeBuilder addDiskImage(String diskImageName) {
        if (diskImageName == null) throw new IllegalArgumentException("diskImageName argument may not be null");
        this.diskImages.add(new DiskImage(diskImageName));
        return this;
    }

    public SliverTypeBuilder setDiskImages(Collection<DiskImage> diList) {
        assert diList != null;
        this.diskImages = new ArrayList<>(diList);
        return this;
    }
}
