package com.feedilicious.core.services;

import com.feedilicious.core.domain.User;
import com.feedilicious.core.repository.UsersRepository;
import com.feedilicious.exceptions.FeediliciousException;
import com.feedilicious.integration.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * User: Khaled.Essghaier
 * Date: 13-11-26
 * Time: 09:58
 * Copyright feedilicious 2013
 */
@Service
@Transactional
public class AppDirectEventHandler implements AppDirectService {

    private static Logger LOG = LoggerFactory.getLogger(AppDirectEventHandler.class);

    @Autowired
    private UsersRepository usersRepository;

    @Override
    public String createSubscription(Event event) {

        Creator creator = event.getCreator();

        if (creator != null) {
            String email = creator.getEmail();
            User user = usersRepository.findByEmail(email);
            if (user != null) {
                throw new FeediliciousException("User already exists!" + email, ErrorCode.USER_ALREADY_EXISTS);
            } else {
                user = new User();
                BeanUtils.copyProperties(creator, user);
                usersRepository.save(user);
                return user.getEmail();
            }
        }
        throw new FeediliciousException("Something went wrong with the persistence layer!");
    }

    @Override
    public void changeSubscription(Event event) {

        Payload payload = event.getPayload();

        if (payload != null) {
            Account account = payload.getAccount();
            if (account != null) {
                String accountId = account.getAccountIdentifier();
                User user = usersRepository.findByEmail(accountId);
                if (user == null) {
                    throw new FeediliciousException("User Not Found! " + accountId, ErrorCode.ACCOUNT_NOT_FOUND);
                } else {
                    //TODO Manage subscription change?
                }
            }
        }
    }

    @Override
    public void cancelSubscription(Event event) {

        Payload payload = event.getPayload();

        if (payload != null) {
            Account account = payload.getAccount();
            if (account != null) {
                String accountId = account.getAccountIdentifier();
                User user = usersRepository.findByEmail(accountId);
                if (user == null) {
                    throw new FeediliciousException("User Not Found! " + accountId, ErrorCode.ACCOUNT_NOT_FOUND);
                } else {
                    usersRepository.delete(user);
                }
            }
        }
    }

    @Override
    public void subscriptionStatusChange(Event event) {

        Payload payload = event.getPayload();

        if (payload != null) {
            Account account = payload.getAccount();
            Notice notice = payload.getNotice();

            if (account != null && notice != null) {
                String accountId = account.getAccountIdentifier();
                User user = usersRepository.findByEmail(accountId);

                if (user == null) {
                    throw new FeediliciousException("User Not Found! " + accountId, ErrorCode.ACCOUNT_NOT_FOUND);
                } else {
                    if ("CLOSED".equalsIgnoreCase(notice.getType())) {
                        this.cancelSubscription(event);
                    }
                }
            }
        }
    }
}
