/**
 * ******************************************************************************
 *  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.router.model.transform;


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

import com.nifty.cloud.sdk.router.model.BackupInformation;
import com.nifty.cloud.sdk.router.model.NetworkInterfaceSet;
import com.nifty.cloud.sdk.router.model.Router;
import com.nifty.cloud.sdk.router.model.Tag;
import com.nifty.cloud.sdk.router.model.VersionInformation;
import com.nifty.cloud.sdk.tools.DateUtils;
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;

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

	private static final String ELEMENT_ROUTER_ID = "routerId";
	private static final String ELEMENT_ROUTER_NAME = "routerName";
	private static final String ELEMENT_STATE = "state";
	private static final String ELEMENT_AVAILABILITY_ZONE = "availabilityZone";
	private static final String ELEMENT_TAG_SET = "tagSet";
	private static final String ELEMENT_ACCOUNTING_TYPE = "accountingType";
	private static final String ELEMENT_NEXT_MONTH_ACCOUNTING_TYPE = "nextMonthAccountingType";
	private static final String ELEMENT_DESCRIPTION = "description";
	private static final String ELEMENT_VERSION_INFORMATION = "versionInformation";
	private static final String ELEMENT_NETWORK_INTERFACE_SET = "networkInterfaceSet";
	private static final String ELEMENT_ROUTE_TABLE_ID = "routeTableId";
	private static final String ELEMENT_ROUTE_TABLE_ASSOCIATION_ID = "routeTableAssociationId";
	private static final String ELEMENT_NAT_TABLE_ID = "natTableId";
	private static final String ELEMENT_NAT_TABLE_ASSOCIATION_ID = "natTableAssociationId";
	private static final String ELEMENT_BACKUP_INFORMATION = "backupInformation";
	private static final String ELEMENT_TYPE = "type";
	private static final String ELEMENT_GROUP_SET = "groupSet";
	private static final String ELEMENT_CREATED_TIME = "createdTime";

	/**
	 * 変換可能なクラスか判定します。
	 *
	 * @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
	 * routerSetをList<Router>クラスに変換します。}
	 *
	 * @param reader XML読込リーダ
	 * @param context
	 * @return {@literal List<Router>オブジェクト}
	 *
	 * @see com.thoughtworks.xstream.converters.Converter#unmarshal(HierarchicalStreamReader, UnmarshallingContext)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

		List<Router> routers = new ArrayList<Router>();
		Converter tagSetConverter = new TagSetConverter();
		Converter versionInformationConverter = new VersionInformationConverter();
		Converter networkInterfaceSetConverter = new NetworkInterfaceSetConverter();
		Converter backupInformationConverter = new BackupInformationConverter();
		Converter groupSetConverter = new GroupSetConverter();

		String name = null;
		while (reader.hasMoreChildren()) {
			reader.moveDown();
			Router router = new Router();
			
			while (reader.hasMoreChildren()) {
				reader.moveDown();

				name = reader.getNodeName();
				if (name.equals(ELEMENT_ROUTER_ID)) {
					router.setRouterId(reader.getValue());
				} else if (name.equals(ELEMENT_ROUTER_NAME)) {
					router.setRouterName(reader.getValue());
				} else if (name.equals(ELEMENT_STATE)) {
					router.setState(reader.getValue());
				} else if (name.equals(ELEMENT_AVAILABILITY_ZONE)) {
					router.setAvailabilityZone(reader.getValue());
				} else if (name.equals(ELEMENT_TAG_SET)) {
					router.setTagSet((List<Tag>) tagSetConverter.unmarshal(reader, context));
				} else if (name.equals(ELEMENT_ACCOUNTING_TYPE)) {
					router.setAccountingType(reader.getValue());
				} else if (name.equals(ELEMENT_NEXT_MONTH_ACCOUNTING_TYPE)) {
					router.setNextMonthAccountingType(reader.getValue());
				} else if (name.equals(ELEMENT_DESCRIPTION)) {
					router.setDescription(reader.getValue());
				} else if (name.equals(ELEMENT_VERSION_INFORMATION)) {
					router.setVersionInformation((VersionInformation) versionInformationConverter.unmarshal(reader, context));
				} else if (name.equals(ELEMENT_NETWORK_INTERFACE_SET)) {
					router.setNetworkInterfaces((List<NetworkInterfaceSet>) networkInterfaceSetConverter.unmarshal(reader, context));
				} else if (name.equals(ELEMENT_ROUTE_TABLE_ID)) {
					router.setRouteTableId(reader.getValue());
				} else if (name.equals(ELEMENT_ROUTE_TABLE_ASSOCIATION_ID)) {
					router.setRouteTableAssociationId(reader.getValue());
				} else if (name.equals(ELEMENT_NAT_TABLE_ID)) {
					router.setNatTableId(reader.getValue());
				} else if (name.equals(ELEMENT_NAT_TABLE_ASSOCIATION_ID)) {
					router.setNatTableAssociationId(reader.getValue());
				} else if (name.equals(ELEMENT_BACKUP_INFORMATION)) {
					router.setBackupInformation((BackupInformation) backupInformationConverter.unmarshal(reader, context));
				} else if (name.equals(ELEMENT_TYPE)) {
					router.setType(reader.getValue());
				} else if (name.equals(ELEMENT_GROUP_SET)) {
					router.setGroupIds((List<String>) groupSetConverter.unmarshal(reader, context));
				} else if (name.equals(ELEMENT_CREATED_TIME)) {
					router.setCreatedTime(DateUtils.toDate(reader.getValue()));
				}
	
				reader.moveUp();
			}
			
			routers.add(router);
			reader.moveUp();
		}

		return routers;
	}

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