/*-
 * ~~~~~~licensing~~~~~~
 * generator-api
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.base.generator.template;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.base.generator.template.impl.ProvidedTemplate;
import ru.entaxy.platform.base.support.CommonUtils;

public class TemplateImpl implements Template {

	private static final Logger log = LoggerFactory.getLogger(TemplateImpl.class);
	
	protected BundleContext bundleContext;
	
	protected String id;
	protected String name;
	protected String filename;
	protected String fullname;
	protected String type;
	protected String path;
	protected String description;
	
	protected String templateLocation;
	
	protected Map<String, String> additionalProperties = null;
	
	public void load(ProvidedTemplate providedTemplate) {
		id = providedTemplate.getId();
		name = providedTemplate.getName();
		filename = providedTemplate.getFilename();
		fullname = providedTemplate.getFullname();
		type = providedTemplate.getType();
		path = providedTemplate.getPath();
		if (!path.endsWith("/"))
			path += "/";
		description = providedTemplate.getDescription();
		additionalProperties = providedTemplate.getAdditionalProperties();
	}
	
	@Override
	public URL getTemplateLocation() {
		if (CommonUtils.isValid(templateLocation))
			try {
				return new URL(templateLocation);
			} catch (MalformedURLException e) {
				log.warn("Invalid template location: [" + templateLocation + "]", e);
			}
		return this.bundleContext.getBundle().getEntry(path);
	}

	@Override
    public String getTemplateFileName() {
        return !CommonUtils.isValid(filename) ? name : filename;
    }

	@Override
	public String getTemplateFullName() {
		return CommonUtils.getValid(fullname, getTemplateFileName() + "." + type);
	};

	@Override
	public String getType() {
		return type;
	}
	
	@Override
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setTemplateFullName(String fullname) {
		this.fullname = fullname;
	}
	
	public void setTemplateLocation(String templateLocation) {
		this.templateLocation = templateLocation;
	}
	
	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public String getTemplateName() {
		return name;
	}

	public void setTemplateName(String name) {
		this.name = name;
	}

	public void setBundleContext(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
	}

	public Map<String, String> getAdditionalProperties() {
		return additionalProperties;
	}

	public void setAdditionalProperties(Map<String, String> additionalProperties) {
		this.additionalProperties = additionalProperties;
	}
	
}
