package caseStudy;

public class Stitch extends SaveAsXML {
    // Describes a crochet stitch
    private SaveableField <String> name;
    private SaveableField <String> abbreviation;
    private SaveableField <String> howTo;
    private SaveableField <Integer> stitchWidth; // Refers to how many stitches from the previous row are explicitly or implicitly used by this stitch
    
    // XML Tags
    public static final String STARTCLASSTAG = "<Stitch>";
    public static final String ENDCLASSTAG = "</Stitch>";
    
    //Constructors
    public Stitch(String name, String abbreviation, String howTo) {
        initializeFields();
        this.name.setValue(name.toLowerCase());
        this.abbreviation.setValue(abbreviation.toLowerCase());
        this.howTo.setValue(howTo);
        stitchWidth.setValue(1);
    }
    public Stitch(String name, String abbreviation, String howTo, int stitchWidth) {
        initializeFields();
        this.name.setValue(name.toLowerCase());
        this.abbreviation.setValue(abbreviation.toLowerCase());
        this.howTo.setValue(howTo);
        this.stitchWidth.setValue(stitchWidth);
    }
    public Stitch(Stitch stitch) {
        initializeFields();
        name.setValue(stitch.getName().toLowerCase());
        abbreviation.setValue(stitch.getAbbreviation().toLowerCase());
        howTo.setValue(stitch.getHowTo());
        stitchWidth.setValue(stitch.getWidth());
    }
    public Stitch(String xml) {
        initializeFields();
        this.load(xml);
    }
    
    // Accessors
    public String getName() {
        return name.getValue();
    }
    public String getAbbreviation() {
        return abbreviation.getValue();
    }
    public String getHowTo() {
        return howTo.getValue();
    }
    public int getWidth() {
        return stitchWidth.getValue();
    }
    public String save() {
        return STARTCLASSTAG 
                + name.save()
                + abbreviation.save()
                + howTo.save()
                + stitchWidth.save()
                + ENDCLASSTAG;
    }
    public String toString() {
        return "Stitch name: " + name.getValue() + "\n\tAbbreviation: " + abbreviation.getValue() + "\n\tWidth: " + stitchWidth.getValue() + "\n\tHow to: " + howTo.getValue();
    }
    public boolean equals(Stitch s) {
        if((name.getValue() == s.getName()) 
                && (abbreviation.getValue() == s.getAbbreviation())
                && (howTo.getValue() == s.getHowTo())
                && (stitchWidth.getValue() == s.getWidth())) {
            return true;
        }
        return false;
    }
    
    // Modifiers
    public void setName(String newName) {
        name.setValue(newName.toLowerCase());
    }
    public void setAbbreviation(String newAbbreviation) {
        abbreviation.setValue(newAbbreviation.toLowerCase());
    }
    public void setHowTo(String newHowTo) {
        howTo.setValue(newHowTo);
    }
    public void setWidth(int newWidth) {
        stitchWidth.setValue(newWidth);
    }
    public void load(String xml) {
        name.setValue(name.getStringDataFromXML(xml));
        abbreviation.setValue(abbreviation.getStringDataFromXML(xml));
        howTo.setValue(howTo.getStringDataFromXML(xml));
        stitchWidth.setValue(Integer.parseInt(stitchWidth.getStringDataFromXML(xml)));
    }
    private void initializeFields() {
        name = new SaveableField <String> ("name");
        abbreviation = new SaveableField <String> ("abbreviation");
        howTo = new SaveableField <String> ("howTo");
        stitchWidth = new SaveableField <Integer> ("stitchWidth");
    }
}
