/*-
 * ~~~~~~licensing~~~~~~
 * basic-auth-impl
 * ==========
 * 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.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;
    }

}
