/*
 * Copyright 2009 Inspire-Software.com
 *
 *    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.
 */

package org.yes.cart.service.domain.aspect.impl;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.yes.cart.constants.AttributeNamesKeys;
import org.yes.cart.domain.entity.Manager;
import org.yes.cart.domain.message.RegistrationMessage;
import org.yes.cart.domain.message.consumer.ManagerRegistrationMessageListener;
import org.yes.cart.domain.message.impl.RegistrationMessageImpl;
import org.yes.cart.service.domain.HashHelper;
import org.yes.cart.service.domain.MailService;
import org.yes.cart.service.domain.PassPhraseGenerator;
import org.yes.cart.service.domain.SystemService;
import org.yes.cart.service.mail.MailComposer;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Arrays;

/**
 * User: Igor Azarny iazarny@yahoo.com
 * Date: 09-May-2011
 * Time: 14:12:54
 */
@Aspect
public class ManagerRegistrationAspect extends BaseNotificationAspect {


    private final Logger LOG = LoggerFactory.getLogger(ManagerRegistrationAspect.class);


    private final HashHelper passwordHashHelper;

    private final PassPhraseGenerator phraseGenerator;

    private final MailService mailService;

    private final MailComposer mailComposer;

    private final SystemService systemService;



    /**
     * Constructor for aspect with asynchronous notification.
     */
    public ManagerRegistrationAspect(final TaskExecutor taskExecutor,
                                     final PassPhraseGenerator phraseGenerator,
                                     final HashHelper passwordHashHelper,
                                     final MailService mailService,
                                     final MailComposer mailComposer,
                                     final SystemService systemService) {
        super(taskExecutor);
        this.passwordHashHelper = passwordHashHelper;
        this.phraseGenerator = phraseGenerator;
        this.mailService = mailService;
        this.mailComposer = mailComposer;

        this.systemService = systemService;
    }

    /**
     *  Constructor for aspect with synchronous notification.
     */
    public ManagerRegistrationAspect(final PassPhraseGenerator phraseGenerator,
                                     final HashHelper passwordHashHelper,
                                     final MailService mailService,
                                     final MailComposer mailComposer,
                                     final SystemService systemService) {
        super(null);
        this.passwordHashHelper = passwordHashHelper;
        this.phraseGenerator = phraseGenerator;
        this.mailService = mailService;
        this.mailComposer = mailComposer;

        this.systemService = systemService;
    }

    /*
     * Handle manager creation.
     *
     * @param pjp {@link org.aspectj.lang.ProceedingJoinPoint}
     * @return Object
     * @throws Throwable in case of target method errors
     */
    @Around("execution(* org.yes.cart.service.domain.impl.ManagerServiceImpl.create(..))")
    public Object doCreateManager(final ProceedingJoinPoint pjp) throws Throwable {
        final Manager manager = (Manager) pjp.getArgs()[0];

        setNewPassword(manager);

        return pjp.proceed();
    }

    private void setNewPassword(Manager manager) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        final String generatedPassword  = phraseGenerator.getNextPassPhrase();
        final String passwordHash = passwordHashHelper.getHash(generatedPassword);
        manager.setPassword(passwordHash);
        manager.setPasswordExpiry(Instant.now()); // Set password for immediate expiry as it is auto generated by the system
        final RegistrationMessage registrationMessage = new RegistrationMessageImpl();
        registrationMessage.setLogin(manager.getLogin());
        registrationMessage.setEmail(manager.getEmail());
        registrationMessage.setPhone(manager.getPhone());
        registrationMessage.setSalutation(manager.getSalutation());
        registrationMessage.setFirstname(manager.getFirstname());
        registrationMessage.setLastname(manager.getLastname());
        registrationMessage.setMiddlename(manager.getMiddlename());
        registrationMessage.setCompanyName1(manager.getCompanyName1());
        registrationMessage.setCompanyName2(manager.getCompanyName2());
        registrationMessage.setCompanyDepartment(manager.getCompanyDepartment());
        registrationMessage.setPassword(generatedPassword);
        registrationMessage.setMailTemplatePathChain(Arrays.asList("/admin/mail/", "/default/mail/"));

        registrationMessage.setTemplateName("adm-passwd");

        registrationMessage.setShopMailFrom(determineFromEmail());

        sendNotification(registrationMessage);

        LOG.info("Manager message was send to queue {}", registrationMessage);
    }


    private String determineFromEmail() {
        final String attrVal = this.systemService.getAttributeValue(AttributeNamesKeys.System.MAIL_SERVER_FROM);
        if (StringUtils.isNotBlank(attrVal)) {
            return attrVal;
        }
        return null;
    }


    /** Handle reset password operation.
     *
     * @param pjp {@link ProceedingJoinPoint}
     * @return Object
     * @throws Throwable in case of target method errors
     */
   @Around("execution(* org.yes.cart.service.domain.impl.ManagerServiceImpl.resetPassword(..))")
    public Object doResetPassword(final ProceedingJoinPoint pjp) throws Throwable {

       final Manager manager = (Manager) pjp.getArgs()[0];

       setNewPassword(manager);

       return pjp.proceed();
    }

    @Override
    public Runnable getTask(Serializable serializableMessage) {
        return new ManagerRegistrationMessageListener(
                mailService,
                mailComposer,
                (RegistrationMessage) serializableMessage
        );
    }

}
