/*-
 * ~~~~~~licensing~~~~~~
 * test-producers
 * ==========
 * 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.platform.base.objects.profile.storage;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.osgi.service.component.annotations.CollectionType;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import ru.entaxy.esb.system.jpa.SystemService;
import ru.entaxy.esb.system.management.bundle.jpa.entity.BundleEntity;
import ru.entaxy.platform.base.objects.EntaxyObject;
import ru.entaxy.platform.base.objects.EntaxyObjectStorage;

@Component(service = EntaxyObjectStorage.class, immediate = true)
public class SystemObjectStorage implements EntaxyObjectStorage {

    private static final Logger log = LoggerFactory.getLogger(SystemObjectStorage.class);

    @Reference(cardinality = ReferenceCardinality.MANDATORY, collectionType = CollectionType.SERVICE)
    protected SystemService systemService;

    @SuppressWarnings("serial")
    protected Map<String, String> fieldMap = new HashMap<>() {
        {
            put("uuid", "objectId");
            put("name", "systemName");
            put("description", "description");
            put("version", "artifact.version");
            put("isDefaultRoute", "useDefaultRoute");
        }
    };

    protected Map<String, String> fieldBundleMap = new HashMap<>() {
        {
            put("name", "bundleId");
            put("type", "artifact.type");
            put("status", "artifact.status");
            put("version", "artifact.version");
            put("url", "bundleLocation");
        }
    };

    @Override
    public String[] getObjectTypes() {
        return new String[] {EntaxyObject.OBJECT_TYPES.PROFILE};
    }

    @Override
    public Object store(Map<String, Object> data) {

        JsonObject jsonObject = new JsonObject();
        Gson gson = new Gson();

        for (Entry<String, String> entry : fieldMap.entrySet()) {
            if (data.containsKey(entry.getValue()))
                jsonObject.add(entry.getKey(), gson.toJsonTree(data.get(entry.getValue())));
        }
        JsonObject bundleJsonObject = new JsonObject();
        for (Entry<String, String> entry : fieldBundleMap.entrySet()) {
            if (data.containsKey(entry.getValue()))
                bundleJsonObject.add(entry.getKey(), gson.toJsonTree(data.get(entry.getValue())));
        }

        log.debug("JSON :: " + jsonObject.toString());
        ru.entaxy.esb.system.jpa.entity.System system =
                gson.fromJson(jsonObject, ru.entaxy.esb.system.jpa.entity.System.class);
        system.setCreateDate(new Date());
        system.setCreatedBy("admin");

        log.debug("JSON Bundle :: " + bundleJsonObject.toString());
        BundleEntity bundleEntity = gson.fromJson(bundleJsonObject, BundleEntity.class);
        system.setBundleEntity(bundleEntity);
        systemService.create(system);

        return null;
    }

}
