/*-
 * ~~~~~~licensing~~~~~~
 * artifact-management
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * 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.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.artifact;

import java.io.File;
import java.nio.charset.StandardCharsets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import ru.entaxy.platform.base.support.xml.CommonXMLUtils;
import ru.entaxy.platform.core.artifact.annotation.ArtifactSupport;

@ArtifactSupport(supportedCategory = Artifact.ARTIFACT_CATEGORY_BLUEPRINT
	, supportedContentClasses = {String.class, Document.class, byte[].class}
	, defaultArtifactClassifier = Artifact.ARTIFACT_CATEGORY_BLUEPRINT
	, defaultArtifactType = "xml")
public class Blueprint extends DefaultArtifact implements Manifested {

	private final static Logger log = LoggerFactory.getLogger(Blueprint.class);
	
	public static final String ARTIFACT_CATEGORY_BLUEPRINT = "blueprint";
	
	protected BlueprintManifest manifest = null;

	@Override
	public void setContent(Object content) {
		super.setContent(content);
		if (this.content.getClass().equals(byte[].class)) {
			String s = new String((byte[])this.content, StandardCharsets.UTF_8);
			this.content = s;
		}
		if (this.content.getClass().equals(String.class)) {
			try {
				Document d = CommonXMLUtils.parseString(false, (String)this.content);
				this.content = d;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}	
	
	@Override
	public ArtifactManifest getManifest() {
		if (this.manifest == null)
			this.manifest = new BlueprintManifest(this);
		if (this.content instanceof Document)
			this.manifest.load((Document)this.content);
		return this.manifest;
	}

	@Override
	public void toFile(File destination) {
		if (this.content instanceof String)
			super.toFile(destination);
		else if (this.content instanceof Document) {
			Document doc = (Document)this.content;
			try {
				asByteArray();
				CommonXMLUtils.saveDocument(doc, destination);
			} catch (Exception e) {
				log.error("Error saving to file", e);
			}
		}
		else if (this.content instanceof byte[]) {
			// TODO to be implemented
		}
		
	}	
	
	@Override
	public byte[] asByteArray() {
		if (this.content instanceof byte[])
			return (byte[])this.content;
		if (this.content instanceof String)
			return ((String)this.content).getBytes();
		if (this.content instanceof Document)
			try {
				Document doc = (Document)this.content;
				if (!this.manifest.isLoaded())
					this.manifest.load(doc);
				this.manifest.save(doc);
				return CommonXMLUtils.doc2string(doc).getBytes();
			} catch (Exception e) {
				log.error("Error getting as byte[]: " + this.getCoordinates().toString(), e);
			}
		return new byte[] {};
	}
	
}
