package com.tandbergtv.spec;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.tandbergtv.metadatamanager.exception.MetadataException;
import com.tandbergtv.metadatamanager.exception.SearchException;
import com.tandbergtv.metadatamanager.factoryImpl.SpecHandlerFactory;
import com.tandbergtv.metadatamanager.model.Spec;
import com.tandbergtv.metadatamanager.spec.IIdentifier;
import com.tandbergtv.metadatamanager.spec.IRuleManager;
import com.tandbergtv.metadatamanager.spec.ISpecHandler;
import com.tandbergtv.metadatamanager.spec.IValidator;
import com.tandbergtv.metadatamanager.specimpl.mstv.MSTVIdentifier;
import com.tandbergtv.metadatamanager.validation.ValidationError;

/**
 * Test class to verify Spec handler base.
 * 
 * @author spuranik
 * 
 */
public class MSTVHandlerTest extends BaseHandlerTest {

	protected static String testFilePathAndName= "tests/com/tandbergtv/spec/sampleInput/mstv-sample";

//	protected static String testFilePathAndName= "tests/com/tandbergtv/spec/sampleInput/mstv-sample_la";

	@Override
	protected ISpecHandler createSpecHandler() {
		return SpecHandlerFactory.getInstance("MSTV");
	}
	
	@Override
	protected String getSpecName() {
		return "MSTV";
	}
	
	protected IIdentifier createIdentifier(){
		MSTVIdentifier id = (MSTVIdentifier) createSpecHandler().getIdentifier();
		id.setAssetId("OutbreakWVC12242528x4802398pWMA129wmv");
		id.setProviderId("NoEncode");
		
		return id;
	}

	public void _testPut(){
		int fileIndexStart=1;
		int fileIndexEnd = 4;
		putMutiple(testFilePathAndName, fileIndexStart, fileIndexEnd);
	}

	public void _testGetAssetByIdentifierAndRevision() {
		getAssetDocByIdentifierAndRevision(createIdentifier(),"." +3);
	}

	public void _testGetAllRevisionsByIdentifier(){
		getAllRevisionsByIdentifier(createIdentifier());
	}

	public void _testRollback(){
		rollback(1, "." +3);
	}

	public void testPutRollbackGetAllRevisions(){
		putMutiple(testFilePathAndName, 2, 2);
		_testGetAllRevisionsByIdentifier();
//		putRollbackGetAllRevisions(testFilePathAndName, 1, 4, 1,EXTERNAL_REVISION_TEST_EXTERNAL + "."  +3, createIdentifier());
	}
	
	public void _testDeleteUnique() {
		MSTVIdentifier id = (MSTVIdentifier) createSpecHandler().getIdentifier();
		id.setAssetId("OutbreakWVC12242528x4802398pWMA129wmv");
		id.setProviderId("NoEncode");

		try {
			ISpecHandler handler = createSpecHandler();
			handler.deleteUnique(id);
		} catch (SearchException e) {
			e.printStackTrace();
		}
	}

	public void _testDeleteAll() {
		List<IIdentifier> ids = new ArrayList<IIdentifier>();

		MSTVIdentifier id1 = (MSTVIdentifier) createSpecHandler().getIdentifier();
		id1.setAssetId("OutbreakWVC12242528x4802398pWMA129wmv");
		id1.setProviderId("NoEncode");
		ids.add(id1);

		MSTVIdentifier id2 = (MSTVIdentifier) createSpecHandler().getIdentifier();
		id2.setAssetId("Group3");
		id2.setProviderId("indemand.com");
		ids.add(id2);

		ISpecHandler handler = createSpecHandler();
		List<IIdentifier> deletedIds = handler.deleteAll(ids);

		if (deletedIds.size() == 0) {
			System.out.println("No assets were deleted.");
		}
		for (IIdentifier deletedId : deletedIds) {
			System.out
					.println("Deleted asset with id: " + deletedId.toString());
		}
	}

	public void _testRuleManager() {
		ISpecHandler handler = createSpecHandler();
		IRuleManager rm = handler.getRuleManager();

		Map<String, Boolean> ruleSet = new HashMap<String, Boolean>();
		ruleSet.put("/go/@apple", true);
		ruleSet.put("/go/@orange", true);
		try {
			rm.saveRuleSet("my rule set", ruleSet);
		} catch (MetadataException e) {
			e.printStackTrace();
		}
		System.out.println("Saved: my rule set");
		System.out.println("Printing saved rule sets: " + rm.getAllRuleSets());
		System.out.println("Printing saved \"my rule set\": "
				+ rm.getRuleSet("my rule set"));
		ruleSet = new HashMap<String, Boolean>();
		ruleSet.put("/go/@banana", false);
		try {
			rm.saveRuleSet("my rule set", ruleSet);
		} catch (MetadataException e) {
			e.printStackTrace();
		}
		System.out.println("Saved: my rule set");
		System.out.println("Printing saved rule sets: " + rm.getAllRuleSets());
		System.out.println("Printing saved \"my rule set\": "
				+ rm.getRuleSet("my rule set"));
		rm.deleteRuleSet("my rule set");
		System.out.println("Deleted: my rule set");
		System.out.println("Printing saved rule sets: " + rm.getAllRuleSets());
	}

	public void _testRuleValidation() {
		ISpecHandler handler = createSpecHandler();
		IRuleManager rm = handler.getRuleManager();

		Map<String, Boolean> ruleSet = new HashMap<String, Boolean>();
		ruleSet.put("/assetPackages/assetPackage/@asset_name", false);
		ruleSet.put("/assetPackages/assetPackage/@verb", true);
		ruleSet.put("/assetPackages/assetPackage/asset[@type='feature']/@verb", true);
		ruleSet.put("/assetPackages/assetPackage/asset[@type='trailer']/@verb", true);
		try {
			rm.saveRuleSet("my rule set", ruleSet);
		} catch (MetadataException e1) {
			e1.printStackTrace();
		}
		System.out.println("Saved: my rule set");
		IValidator baseRules = handler.getRuleValidators().get(
				"MSTV Validator 1");

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		Document doc = null;
		try {
			builder = factory.newDocumentBuilder();
			doc = builder
					.parse("file:tests/com/tandbergtv/validation/Package.xml");
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		List<ValidationError> errList = handler.customValidate(doc, baseRules,
				"my rule set");
		for (ValidationError ve : errList) {
			System.out.println(ve);
		}
		System.out.println("Done");
	}

	public void _testSingleFieldValidation() {
		ISpecHandler handler = createSpecHandler();
		IValidator baseRules = handler.getRuleValidators().get(
				"MSTV Validator 1");

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		Document doc = null;
		try {
			builder = factory.newDocumentBuilder();
			doc = builder
					.parse("file:tests/com/tandbergtv/validation/Package.xml");
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		List<ValidationError> errList = handler.validateField(doc, baseRules,
				"/assetPackages/assetPackage/@asset_name");
		for (ValidationError ve : errList) {
			System.out.println(ve);
		}
		System.out.println("Done");
	}
	
	public void _testGetSpecName() {
		File file = new File("tests/com/tandbergtv/spec/sampleInput/mstv-sample.xml");
		DocumentBuilder builder = null;
		Document input = null;
		try {
			builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			input = builder.parse(file);
			ISpecHandler handler = createSpecHandler();
			Spec spec = handler.getSpecName(input);
			System.out.println("Spec == " + spec.toString());
			assertEquals("MSTV", spec.toString());
			
		} catch (SAXException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (ParserConfigurationException e2) {
			e2.printStackTrace();
		}
	}
}
