package com.nifty.cloud.ncs.service.parser;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.nifty.cloud.ncs.NiftyCloudResponse;
import com.nifty.cloud.ncs.http.HttpResponse;
import com.nifty.cloud.ncs.service.NcsBucket;
import com.nifty.cloud.ncs.service.Owner;
import com.nifty.cloud.ncs.util.DateUtils;

/**
 * GetServiceの応答XML解析用クラスです。
 *
 */
public class GetServiceParser extends AbstractStorageResponseParser<List<NcsBucket>>
{
	public GetServiceParser()
	{
		super();
	}

	/**
	 *
	 */
	@Override
	public NiftyCloudResponse<List<NcsBucket>> parse(HttpResponse response) throws Exception
	{
		NiftyCloudResponse<List<NcsBucket>> ncsResponse = super.parseMetaData(response);
		GetServiceUnmarshaller unmarshaller = new GetServiceUnmarshaller();
		parseXml(unmarshaller, response.getContent());
		List<NcsBucket> bucketList = unmarshaller.getBucketList();

		ncsResponse.setResult(bucketList);

		return (ncsResponse);
	}


    /**
     * Unmarshaller for the GetService XML response.
     *
     */
	private class GetServiceUnmarshaller extends DefaultHandler
	{
		private Owner owner = null;
		private NcsBucket currentBucket = null;
		private List<NcsBucket> bucketList = null;
		private StringBuffer text = null;

		public GetServiceUnmarshaller()
		{
			super();
			bucketList = new ArrayList<NcsBucket>();
			text = new StringBuffer();
		}

		/**
		 * Get the list of buckets.
		 *
		 * @return
		 *  The list of buckets.
		 */
		public List<NcsBucket> getBucketList()
		{
			return (bucketList);
		}

		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
		 */
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException
		{
			text.append(ch, start, length);
		}

		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#startDocument()
		 */
		@Override
		public void startDocument() throws SAXException
		{
		}

		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#endDocument()
		 */
		@Override
		public void endDocument() throws SAXException
		{
		}

		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
		 */
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
		{
			if (localName.equals("Bucket")) {
				currentBucket = new NcsBucket();
			} else if (localName.equals("Owner")) {
				owner = new Owner();
			}
		}

		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
		 */
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException
		{
			String element = text.toString();

			if (localName.equals("ID")) {
				owner.setUserId(element);
			} else if (localName.equals("DisplayName")) {
				owner.setDisplayName(element);
			} else if (localName.equals("Bucket")) {
				currentBucket.setOwner(owner);
				bucketList.add(currentBucket);
			} else if (localName.equals("Name")) {
				currentBucket.setName(element);
			} else if (localName.equals("CreationDate")) {
				element += ".000Z";
				try {
					currentBucket.setCreated(DateUtils.parseIso8601Date(element));
				} catch (ParseException e) {
					throw (new RuntimeException("Non-ISO8601 date format in list buckets input: " + element, e));
				}
			}
			text = new StringBuffer();
		}

	}
}
