/*-
 * ~~~~~~licensing~~~~~~
 * generator-api
 * ==========
 * 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.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;
	}
	
}
