/*-
 * ~~~~~~licensing~~~~~~
 * keystore-runtime
 * ==========
 * 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.security.keystore.runtime.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import ru.entaxy.esb.resources.EntaxyResource;
import ru.entaxy.esb.resources.EntaxyResourceMetadata;
import ru.entaxy.security.keystore.runtime.EntaxyKeystore;

public class KeystoreResource {

    EntaxyResource entaxyResource;
    EntaxyResourceMetadata meta;

    JsonObject metaContent;

    String keystoreId;

    String keystoreType = EntaxyKeystore.KEYSTORE_TYPE_DEFAULT;

    public KeystoreResource(EntaxyResource resource) {
        super();
        this.entaxyResource = resource;

        String name = this.entaxyResource.getName();
        int index = name.lastIndexOf('.');
        if (index > 0) {
            this.keystoreId = name.substring(0, index);
            this.keystoreType = name.substring(index + 1);
        } else {
            this.keystoreId = name;
        }

        this.meta = this.entaxyResource.getMetadata(true);
        this.metaContent = this.meta.getSection(EntaxyKeystore.KEYSTORE_META_SECTION_NAME).getContent();
    }

    public boolean isValid() {
        JsonElement je = this.metaContent.get(EntaxyKeystore.KEYSTORE_META_PROP_VALID);
        try {
            return je.getAsBoolean();
        } catch (Exception e) {
            return false;
        }
    }

    public boolean isProcessed() {
        JsonElement je = this.metaContent.get(EntaxyKeystore.KEYSTORE_META_PROP_PROCESSED);
        try {
            return je.getAsBoolean();
        } catch (Exception e) {
            return false;
        }
    }

    public String getType() {
        JsonElement je = this.metaContent.get(EntaxyKeystore.KEYSTORE_META_PROP_TYPE);
        try {
            return je.getAsString();
        } catch (Exception e) {
            return EntaxyKeystore.KEYSTORE_TYPE_DEFAULT;
        }
    }

    public List<String> getAliases() {
        JsonElement je = this.metaContent.get(EntaxyKeystore.KEYSTORE_META_PROP_ALIASES);
        if (!je.isJsonArray())
            return Collections.emptyList();
        List<String> result = new ArrayList<>();
        for (JsonElement elem : je.getAsJsonArray()) {
            if (elem.isJsonPrimitive() && elem.getAsJsonPrimitive().isString())
                result.add(elem.getAsString());
        }
        return result;
    }

}
