package com.inkasso.auth.aspect;

import com.inkasso.adapter.bean.AbstractUser;
import com.inkasso.adapter.web.LoginUserHolder;
import com.inkasso.auth.annotation.Log;
import com.inkasso.factoring.org.model.request.AddClickLogRequest;
import com.inkasso.factoring.org.service.ClickLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class LogAspect {

    @Autowired
    private ClickLogService clickLogService;

    @SuppressWarnings("unused")
    @Before("execution(public * com.inkasso.factoring..*.*(..)) && @annotation(log)")
    public void testToken(final JoinPoint joinPoint, Log log) {
        AbstractUser abstractUser = LoginUserHolder.get();
        if (abstractUser != null){
            AddClickLogRequest addClickLogRequest = new AddClickLogRequest();
            addClickLogRequest.setUserId(abstractUser.getId());
            addClickLogRequest.setSource(log.value());
            clickLogService.addClickLog(addClickLogRequest);
        }
    }


}
