package com.tandbergtv.metadatamanager.specimpl;

import java.util.List;

import com.tandbergtv.metadatamanager.model.Asset;
import com.tandbergtv.metadatamanager.model.Item;
import com.tandbergtv.metadatamanager.model.NextRevision;
import com.tandbergtv.metadatamanager.model.Relation;
import com.tandbergtv.metadatamanager.model.Item.ItemType;

/**
 * cl1.1 and mstv have only 1 title in a package. this handler handles the merge
 * process for such specs
 * 
 * @author vgoyal
 * 
 * TODO modify the logic to handle child-group scenario (see commented out MSTVHandler code)
 * 
 */
public abstract class SingleTitleSpecHandler extends SpecHandlerBase {

	/**
	 * this  is overridden here because it a TITLE is present in the
	 * newAsset, merge it with the existing TITLE (if any). dont search for this
	 * asset in the db. search for the existence of an item of type TITLE in the
	 * existing Asset.
	 */
	@Override
	protected void specSpecificMerging(Asset oldAsset, NextRevision nextRevision,
			Relation newRelation, Asset newTargetAsset) {
		List<Asset> oldTitleAssets = oldAsset
				.getTargetAssets(ItemType.TITLE.toString());

		if (oldTitleAssets != null && oldTitleAssets.size() > 0) {
			mergeFields(newTargetAsset, oldTitleAssets.get(0), ItemType.TITLE
					.toString(), nextRevision);
			if (nextRevision != null) {
				if (oldTitleAssets.get(0).isRevisionNumberUpdated()) {
					oldAsset.setLatestRevisionNumber(nextRevision.getRevisionNumber());
				}
			}
		} else {
			if(nextRevision == null) {
				NextRevision tempNextRevision = new NextRevision();
				tempNextRevision.setRevisionNumber(oldAsset.getLatestRevisionNumber());

				oldAsset.addChild(newRelation.getTargetAsset(), tempNextRevision, true);
			} else {
				oldAsset.addChild(newRelation.getTargetAsset(), nextRevision, true);
			}
		}
	}
	
	//if newAsset has a title, let it be. else add this title to that asset.
	@Override
	protected void specSpecificMergingNoSave(Asset newAsset, Relation oldRelation, Asset oldTitle) {
		List<Asset> newTitleAssets = newAsset.getTargetAssets(ItemType.TITLE.toString());
		if(newTitleAssets != null && newTitleAssets.size() > 0) {
			newTitleAssets.get(0).setTTVId(oldTitle.getTTVId());
			mergeFields(newTitleAssets.get(0), oldTitle, getAssetType(oldTitle));
		} else {
			Item i = new Item();
			i.setType(ItemType.TITLE);
			i.setFields(oldTitle.getFields());
			i.setTTVId(oldTitle.getTTVId());
			newAsset.addChild(i);
		}
	}
	
	/**
	 * Special handing for Title asset
	 */
	@Override
	protected boolean requireSpecSpecificMerging(String assetType) {
		return ItemType.TITLE.toString().equalsIgnoreCase(assetType);
	}

	/**
	 * prunes existing asset tree based on incoming tree. this is overriden here
	 * coz n this case, it does not prune title based on ID (coz ID might be
	 * blank for MSTV). so if a TITLE is present in the incoming saset and is
	 * present in the old asset, it is not pruned.
	 */
	@Override
	protected boolean requireSpecSpecificSearchForPruning(String assetType) {
		return (ItemType.TITLE.toString().equalsIgnoreCase(assetType));
	}

	@Override
	protected Asset specSpecificSearchForPruning(String assetType, Asset newAsset,
			Asset oldTargetAsset) {
		Asset newTargetAsset = null;
		List<Asset> titleAssets = newAsset.getTargetAssets(ItemType.TITLE
				.toString());
		if (titleAssets != null && titleAssets.size() > 0) {
			newTargetAsset = titleAssets.get(0);
		}
		return newTargetAsset;
	}
	
	
	
	
}
