/**
 * ******************************************************************************
 *  Copyright Fujitsu.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * *****************************************************************************
 *
 *  NIFCLOUD SDK for Java
 *
 */
package com.nifty.cloud.sdk.server.model.transform;

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

import com.nifty.cloud.sdk.server.model.Association;
import com.nifty.cloud.sdk.server.model.Attachment;
import com.nifty.cloud.sdk.server.model.NetworkInterfaceSet;
import com.nifty.cloud.sdk.server.model.PrivateIpAddresses;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 * NetworkInterfaceResponseType変換クラス。<br />
 * このクラスはレスポンスXMLのnetworkInterfaceSet要素からNetworkInterfaceクラスのリストを生成します。
 *
 */
public class NetworkInterfaceSetConverter implements Converter {

	private static final String ELEMENT_NETWORK_INTERFACE_ID = "networkInterfaceId";
	private static final String ELEMENT_SUBNET_ID = "subnetId";
	private static final String ELEMENT_VPC_ID = "vpcId";
	private static final String ELEMENT_DESCRIPTION = "description";
	private static final String ELEMENT_OWNER_ID = "ownerId";
	private static final String ELEMENT_NIFTY_NETWORK_ID = "niftyNetworkId";
	private static final String ELEMENT_NIFTY_NETWORK_NAME = "niftyNetworkName";
	private static final String ELEMENT_STATUS = "status";
	private static final String ELEMENT_MAC_ADDRESS = "macAddress";
	private static final String ELEMENT_PRIVATE_IP_ADDRESS = "privateIpAddress";
	private static final String ELEMENT_PRIVATE_IP_ADDRESS_V6 = "privateIpAddressV6";
	private static final String ELEMENT_PRIVATE_DNS_NAME = "privateDnsName";
	private static final String ELEMENT_SOURCE_DEST_CHECK = "sourceDestCheck";
	private static final String ELEMENT_GROUP = "groupSet";
	private static final String ELEMENT_ATTACHMENT ="attachment";
	private static final String ELEMENT_ASSOCIATION = "association";
	private static final String ELEMENT_PRIVATE_IP_ADDRESS_SET = "privateIpAddressesSet";

	/**
	 * 変換可能なクラスか判定します。
	 *
	 * @param clazz クラス
	 * @return 変換可能な場合true, 変換不可能な場合false
	 *
	 * @see com.thoughtworks.xstream.converters.Converter#canConvert(Class)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public boolean canConvert(Class clazz) {
		Class[] interfaces = clazz.getInterfaces();
		for(Class i: interfaces) {
			if (i==List.class) {
				return true;
			}
		}
		return false;
	}

	/**
	 * {@literal
	 * blockDeviceMappingをList<InstanceLoadBalancerMapping>クラスに変換します。}
	 *
	 * @param reader XML読込リーダ
	 * @param context
	 * @return {@literal List<InstanceLoadBalancerMapping>オブジェクト}
	 *
	 * @see com.thoughtworks.xstream.converters.Converter#unmarshal(HierarchicalStreamReader, UnmarshallingContext)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		List<NetworkInterfaceSet> networkInterfaces = new ArrayList<NetworkInterfaceSet>();

		Converter groupSetConverter = new GroupSetConverter();
		Converter attachmentConverter = new AttachmentConverter();
		Converter associationConverter = new AssociationConverter();
		Converter privateIpAddressConverter = new PrivateIpAddressConverter();

		String name = null;
		while(reader.hasMoreChildren()) {
			reader.moveDown();
			NetworkInterfaceSet networkInterface = new NetworkInterfaceSet();
			while (reader.hasMoreChildren()) {
				reader.moveDown();
				name = reader.getNodeName();

				if (name.equals(ELEMENT_NETWORK_INTERFACE_ID)) {
					networkInterface.setNetworkInterfaceId(reader.getValue());
				}
				else if (name.equals(ELEMENT_SUBNET_ID)) {
					networkInterface.setSubnetId(reader.getValue());
				}
				else if (name.equals(ELEMENT_VPC_ID)) {
					networkInterface.setVpcId(reader.getValue());
				}
				else if (name.equals(ELEMENT_DESCRIPTION)) {
					networkInterface.setDescription(reader.getValue());
				}
				else if (name.equals(ELEMENT_OWNER_ID)) {
					networkInterface.setOwnerId(reader.getValue());
				}
				else if (name.equals(ELEMENT_NIFTY_NETWORK_ID)) {
					networkInterface.setNiftyNetworkId(reader.getValue());
				}
				else if (name.equals(ELEMENT_NIFTY_NETWORK_NAME)) {
					networkInterface.setNiftyNetworkName(reader.getValue());
				}
				else if (name.equals(ELEMENT_STATUS)) {
					networkInterface.setStatus(reader.getValue());
				}
				else if (name.equals(ELEMENT_MAC_ADDRESS)) {
					networkInterface.setMacAddress(reader.getValue());
				}
				else if (name.equals(ELEMENT_PRIVATE_IP_ADDRESS)) {
					networkInterface.setPrivateIpAddress(reader.getValue());
				}
				else if (name.equals(ELEMENT_PRIVATE_IP_ADDRESS_V6)) {
					networkInterface.setPrivateIpAddressV6(reader.getValue());
				}
				else if (name.equals(ELEMENT_PRIVATE_DNS_NAME)) {
					networkInterface.setPrivateDnsName(reader.getValue());
				}
				else if (name.equals(ELEMENT_SOURCE_DEST_CHECK)) {
					networkInterface.setSourceDestCheck(reader.getValue());
				}
				else if (name.equals(ELEMENT_GROUP)) {
					networkInterface.setGroupIds((List<String>) groupSetConverter.unmarshal(reader, context));
				}
				else if (name.equals(ELEMENT_ATTACHMENT)) {
					networkInterface.setAttachment((Attachment) attachmentConverter.unmarshal(reader, context));
				}
				else if (name.equals(ELEMENT_ASSOCIATION)) {
					networkInterface.setAssociation((Association) associationConverter.unmarshal(reader, context));
				}
				else if (name.equals(ELEMENT_PRIVATE_IP_ADDRESS_SET)) {
					networkInterface.setPrivateIpAddresses((List<PrivateIpAddresses>) privateIpAddressConverter.unmarshal(reader, context));
				}

				reader.moveUp();
			}
			networkInterfaces.add(networkInterface);
			reader.moveUp();
		}

		return networkInterfaces;
	}

	/**
	 * 未実装メソッド
	 */
	@Override
	public void marshal(Object paramObject,
			 			HierarchicalStreamWriter paramHierarchicalStreamWriter,
						MarshallingContext paramMarshallingContext) {
	}
}
