/*-
 * ~~~~~~licensing~~~~~~
 * basic-auth-impl
 * ==========
 * 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.esb.system.auth.basic.jpa.listener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.event.spi.*;
import org.hibernate.persister.entity.EntityPersister;
import ru.entaxy.esb.system.auth.basic.htpasswd.HtpasswdGenerator;
import ru.entaxy.esb.system.auth.basic.jpa.api.BasicAuthService;
import ru.entaxy.esb.system.auth.basic.jpa.api.entity.BasicAuthAccount;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class BasicAuthPostEventListener implements PostCommitInsertEventListener, PostCommitUpdateEventListener, PostCommitDeleteEventListener {

    private static final long serialVersionUID = -6603994118756820823L;
    private static final Log LOG = LogFactory.getLog(BasicAuthPostEventListener.class);

    private BasicAuthService basicAuthService;
    private HtpasswdGenerator htpasswdGenerator;
    private String encryptionSalt;

    public void setEncryptionSalt(String encryptionSalt) {
        this.encryptionSalt = encryptionSalt;
    }

    @Override
    public boolean requiresPostCommitHanding(EntityPersister persister) {
        return true;
    }

    @Override
    public void onPostDelete(PostDeleteEvent event) {
        LOG.debug("Delete handler event " + event.getSession());
        LOG.debug("Delete handler event " + event.getEntity());
        if (event.getEntity() instanceof BasicAuthAccount) {
            generateHtpasswd();
        }

    }

    @Override
    public void onPostUpdate(PostUpdateEvent event) {
        LOG.debug("Update handler event " + event);
        LOG.debug("Update handler event " + event.getEntity());
        generateHtpasswd();
    }

    @Override
    public void onPostInsert(PostInsertEvent event) {
        LOG.debug("Insert handler event " + event);
        LOG.debug("Insert handler event " + event.getEntity());
        generateHtpasswd();
    }

    private void generateHtpasswd() {
        List<BasicAuthAccount> accounts = basicAuthService.list();

        try {
            htpasswdGenerator.generateHtpasswd(accounts, encryptionSalt);
        } catch (NoSuchAlgorithmException | IOException e) {
            LOG.error(e);
        }
    }

    @Override
    public void onPostDeleteCommitFailed(PostDeleteEvent event) {
        LOG.debug("Delete failed event " + event.getEntity());
    }

    @Override
    public void onPostUpdateCommitFailed(PostUpdateEvent event) {
        LOG.debug("Update failed event " + event.getEntity());

    }

    @Override
    public void onPostInsertCommitFailed(PostInsertEvent event) {
        LOG.debug("Insert failed event " + event.getEntity());
    }

    public BasicAuthService getBasicAuthService() {
        return basicAuthService;
    }

    public void setBasicAuthService(BasicAuthService basicAuthService) {
        this.basicAuthService = basicAuthService;
    }

    public HtpasswdGenerator getHtpasswdGenerator() {
        return htpasswdGenerator;
    }

    public void setHtpasswdGenerator(HtpasswdGenerator htpasswdGenerator) {
        this.htpasswdGenerator = htpasswdGenerator;
    }

}
