package scriptbuilder.structures;

import java.util.ArrayList;

/**
 * Aggregation of several information fields relevant to each timeslice.
 *
 * @author Bryan McGuffin
 */
public class CadData implements I_XML_Writable
{

    public CadData()
    {
        locInfo = new ArrayList<Location_Info>();
    }

    public TimeSlice tSlice;

    public String Header_Type = "";

    public String Header_Beat = "";

    public String Header_TruncLoc = "";

    public String Header_FullLoc = "";

    public String Master_Inc_Num = "";

    public String P = "";

    public String Info_Type = "";

    public String Info_Type_Code = "";

    public String Agy = "";

    public String General_Title = "";

    public String General_Text = "";

    public String Location_Beat = "";

    public String Location_Address = "";

    public String Location_Loc = "";

    public String Location_City = "";

    public String Location_Area = "";

    public String Location_Fire = "";

    public String Location_Law = "";

    public String Location_Ems = "";

    public ArrayList<Location_Info> locInfo;

    @Override
    public String toXML()
    {
        String output = "";

        if (!Master_Inc_Num.equals(""))
        {
            output += XMLWriter.simpleTag(Master_Inc_Num, ELEMENT.MASTER_INC_NUM);
        }

        if (!P.equals(""))
        {
            output += XMLWriter.simpleTag(P, ELEMENT.P);
        }

        if (hasAdditionalInfo())
        {
            output += XMLWriter.openTag(ELEMENT.ADDITIONAL_INFO.tag);
            output += XMLWriter.simpleTag(Info_Type_Code, ELEMENT.TYPE_CODE);
            output += XMLWriter.simpleTag(Info_Type, ELEMENT.TYPE);
            output += XMLWriter.closeTag(ELEMENT.ADDITIONAL_INFO.tag);
        }

        if (hasLocation())
        {
            output += XMLWriter.openTag(ELEMENT.LOCATION.tag);
            if (!Location_Beat.equals(""))
            {
                output += XMLWriter.simpleTag(Location_Beat, ELEMENT.BEAT);
            }

            if (!Location_Address.equals(""))
            {
                output += XMLWriter.simpleTag(Location_Address, ELEMENT.ADDRESS);
            }

            if (!Location_Loc.equals(""))
            {
                output += XMLWriter.simpleTag(Location_Loc, ELEMENT.LOC);
            }

            if (!Location_City.equals(""))
            {
                output += XMLWriter.simpleTag(Location_City, ELEMENT.CITY);
            }

            if (!Location_Area.equals(""))
            {
                output += XMLWriter.simpleTag(Location_Area, ELEMENT.AREA);
            }

            if (!Location_Fire.equals(""))
            {
                output += XMLWriter.simpleTag(Location_Fire, ELEMENT.FIRE);
            }

            if (!Location_Law.equals(""))
            {
                output += XMLWriter.simpleTag(Location_Law, ELEMENT.LAW);
            }

            if (!Location_Ems.equals(""))
            {
                output += XMLWriter.simpleTag(Location_Ems, ELEMENT.EMS);
            }

            output += XMLWriter.closeTag(ELEMENT.LOCATION.tag);
        }

        if (!Agy.equals(""))
        {
            output += XMLWriter.openTag(ELEMENT.GENERAL.tag);
            output += XMLWriter.simpleTag(Agy, ELEMENT.AGY);
            output += XMLWriter.closeTag(ELEMENT.GENERAL.tag);
        }

        if (hasHeaderInfo())
        {
            output += XMLWriter.openTag(ELEMENT.HEADER_INFO.tag);

            output += XMLWriter.simpleTag(Header_Type, ELEMENT.Type);

            output += XMLWriter.simpleTag(Header_Beat, ELEMENT.Beat);

            output += XMLWriter.simpleTag(Header_TruncLoc, ELEMENT.TruncLoc);

            output += XMLWriter.simpleTag(Header_FullLoc, ELEMENT.FullLoc);

            output += XMLWriter.closeTag(ELEMENT.HEADER_INFO.tag);
        }

        if (locInfo.size() > 0)
        {
            for (Location_Info li : locInfo)
            {
                output += li.toXML();
            }
        }

        return output;
    }

    public boolean hasHeaderInfo()
    {
        return !Header_Type.equals("") || !Header_Beat.equals("")
                || !Header_FullLoc.equals("") || !Header_TruncLoc.equals("");
    }

    public boolean hasLocation()
    {
        return !Location_Address.equals("") || !Location_Area.equals("")
                || !Location_Beat.equals("") || !Location_City.equals("")
                || !Location_Ems.equals("") || !Location_Fire.equals("")
                || !Location_Law.equals("") || !Location_Loc.equals("");
    }

    public boolean hasGeneralInfo()
    {
        return !General_Text.equals("") || !General_Title.equals("");
    }

    public boolean hasAdditionalInfo()
    {
        return !Info_Type.equals("") || !Info_Type_Code.equals("");
    }

    public boolean hasCadData()
    {
        return hasHeaderInfo() || hasLocation() || hasAdditionalInfo()
                || !Master_Inc_Num.equals("") || !P.equals("") || !Agy.equals("")
                || locInfo.size() > 0;
    }
}
