/*-
 * ~~~~~~licensing~~~~~~
 * template-service-test
 * ==========
 * 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.shell;

import java.util.HashMap;
import java.util.Map;

import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import ru.entaxy.base.generator.template.Template;
import ru.entaxy.base.generator.template.TemplateAwareGenerator;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generator;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.factory.GeneratorFactory;
import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.base.support.JSONUtils;

@Service
@Command(scope = "templates", name = "generate")
public class Generate extends TemplateServiceSupport implements Action {

	@Argument(index = 0, name = "template-id", required = true)
	String templateId;
	
	@Argument(index = 1, name = "parameters", required = false)
	String params;
	
	@Override
	public Object execute() throws Exception {
		Template t = templateService.getTemplateById(templateId);
		if (t == null) {
			System.out.println("Template [" + templateId + "] not found");
			return null;
		}
		TemplateAwareGenerator g = GeneratorFactory.createGenerator(t);
		if (g == null) {
			System.out.println("Generator for template [" + templateId + "] not found");
			return null;
		}
		Generated result = null;
		if (CommonUtils.isValid(params)) {
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();

			JsonElement je = (new JsonParser()).parse(params);
			Map<String, Object> map = JSONUtils.element2map(je);

			result = g.generate(t, map);
		} else {
			result = g.generate(t, new HashMap<>()); 
		}
		if (result == null) {
			System.out.println("Generated is empty, template: [" + templateId + "] not found");
			return null;
		} else {
			System.out.println("Generated: ");
			System.out.println("\ttype: " + result.getType());
			System.out.println("\tcontent: " + (result.getObject()==null?"null":"[==\n" + result.getObject().toString() + "\n==]"));
		}
		return null;
	}
	
}
