/*
 * This file (Label.java) is part of DebGen-LabelPrintLib  developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 *  Copyright ASTRID Research, Hungary, 2010.
 *
 *  ASTRID Research
 *  Headquaters: Csapó utca 42. Debrecen H-4029 HUNGARY
 *  Phone: +36-52-533-173        Fax: +36-52-533-174
 *  Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DEBGEN-Biobank-Project.
 * File last modified at 2010.01.08 14:22
 */
package hu.astrid.debgen.labelprint.domain;

import hu.astrid.debgen.labelprint.exception.LabelPrintException;
import hu.astrid.debgen.labelprint.util.ReadFromXML;

public abstract class Label {

    /**
     * The number of copies
     */
    protected int copies = 1;

    /**
     * Get the value of copies
     *
     * @return the value of copies
     */
    public int getCopies() {
        return copies;
    }

    /**
     * Set the value of copies
     *
     * @param copies new value of copies
     */
    public void setCopies(int copies) {
        this.copies = copies;
    }

    /**
     * Returns the codeType of this label
     * @return the codeType
     */
    //TODO: Same as below, use enum
    public abstract String getType();

    /**
     * Return new Label instance.
     * @param codeType to create the appropiate type of Label.
     * @param copies the needed number of copies of this Label.
     */
    // TODO: use enum instead of string here
    public static Label getInstance(String codeType, int copies) {
        if (codeType == null) {
            throw new IllegalArgumentException("Parameter code type must be set");
        }
        if (codeType.equals("PATIENT")) {
            return new PatientLabel(copies);
        } else if (codeType.equals("SAMPLE")) {
            return new SampleLabel(copies);
        } else {
            throw new IllegalArgumentException("Code type must be PATIENT or SAMPLE");
        }
    }
    /**
     * The codeValue.
     */
    private String codeValue;

    /**
     * Return new Label instance.
     * @param codeType to create the appropiate type of Label.
     */
    // TODO: use enum instead of string here
    public static Label getInstance(String codeType) {
        return Label.getInstance(codeType, 1);
    }

    /**
     * Return the codeValue.
     */
    public String getCodeValue() {
        return codeValue;
    }

    /**
     * Sets the codeValue.
     * @param codeValue The value to set.
     */
    public void setCodeValue(String codeValue) {
        this.codeValue = codeValue;
    }

    /**
     * Abstract method codeValue.
     */
    public abstract String getResourceName();

    /**
     * Return processed template as String. 
     */
    public String processTemplate() throws LabelPrintException {
        String s = readAsResource();
        s = s.replaceAll("\\[CODEVALUE\\]", codeValue);
        return s;
    }

    /**
     * Return resource as String.
     */
    public String readAsResource() throws LabelPrintException {
        return ReadFromXML.readResource(getResourceName());
    }
}
