package sg.ncl.service.user;

import org.springframework.stereotype.Service;
import sg.ncl.service.user.data.jpa.entities.AddressEntity;
import sg.ncl.service.user.data.jpa.entities.UserDetailsEntity;
import sg.ncl.service.user.data.jpa.entities.UserEntity;
import sg.ncl.service.user.data.jpa.repositories.UserRepository;
import sg.ncl.service.user.domain.Address;
import sg.ncl.service.user.domain.User;
import sg.ncl.service.user.domain.UserDetails;
import sg.ncl.service.user.exceptions.UserNotFoundException;

import javax.inject.Inject;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Christopher Zhong
 */
@Service
public class UserService {

    private final UserRepository userRepository;

    @Inject
    protected UserService(final UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public List<User> get() {
        final List<User> result = new ArrayList<>();
        for (UserEntity user : userRepository.findAll()) {
            result.add(user);
        }

        return result;
    }

    public User find(final String id) {
        final UserEntity one = userRepository.findOne(id);
        if (one == null) {
            throw new UserNotFoundException();
        }
        return one;
    }

    public void update(final String id, final UserEntity user) {
        final UserEntity one = userRepository.findOne(id);
        if (one == null) {
            throw new UserNotFoundException();
        }

        final UserDetails oneUserDetails = one.getUserDetails();
        final UserDetails userUserDetails = user.getUserDetails();

        if (! oneUserDetails.getFirstName().equals(userUserDetails.getFirstName())) {
            one.getUserDetails().setFirstName(userUserDetails.getFirstName());
        }

        if (! oneUserDetails.getLastName().equals(userUserDetails.getLastName())) {
            one.getUserDetails().setLastName(userUserDetails.getLastName());
        }

        if (! oneUserDetails.getEmail().equals(userUserDetails.getEmail())) {
            one.getUserDetails().setEmail(userUserDetails.getEmail());
        }

        if (! oneUserDetails.getPhone().equals(userUserDetails.getPhone())) {
            one.getUserDetails().setPhone(userUserDetails.getPhone());
        }

        final Address oneAddress = one.getUserDetails().getAddress();
        final Address userAddress = user.getUserDetails().getAddress();

        if (! oneAddress.getAddress1().equals(userAddress.getAddress1())) {
            one.getUserDetails().getAddress().setAddress1(userAddress.getAddress1());
        }

        if (! oneAddress.getAddress2().equals(userAddress.getAddress2())) {
            one.getUserDetails().getAddress().setAddress2(userAddress.getAddress2());
        }

        if (! oneAddress.getCountry().equals(userAddress.getCountry())) {
            one.getUserDetails().getAddress().setCountry(userAddress.getCountry());
        }

        if (! oneAddress.getRegion().equals(userAddress.getRegion())) {
            one.getUserDetails().getAddress().setRegion(userAddress.getRegion());
        }

        if (! oneAddress.getZipCode().equals(userAddress.getZipCode())) {
            one.getUserDetails().getAddress().setZipCode((userAddress.getZipCode()));
        }

        userRepository.save(one);
    }

    public void seedData() {

        final UserEntity userEntity = new UserEntity();
        userEntity.setApplicationDate(ZonedDateTime.now());
        userEntity.setCreatedDate(ZonedDateTime.now());
        userEntity.setLastModifiedDate(ZonedDateTime.now());

        final UserDetailsEntity userDetailsEntity = new UserDetailsEntity();
        userDetailsEntity.setCreatedDate(ZonedDateTime.now());
        userDetailsEntity.setLastModifiedDate(ZonedDateTime.now());
        userDetailsEntity.setFirstName("first");
        userDetailsEntity.setLastName("last");
        userDetailsEntity.setEmail("email");
        userDetailsEntity.setPhone("phone");

        final AddressEntity address = new AddressEntity();
        address.setCreatedDate(ZonedDateTime.now());
        address.setLastModifiedDate(ZonedDateTime.now());
        address.setAddress1("address1");
        address.setAddress2("address2");
        address.setCountry("country");
        address.setRegion("region");
        address.setZipCode("zipcode");

        userDetailsEntity.setAddress(address);
        userEntity.setUserDetails(userDetailsEntity);

        userRepository.save(userEntity);
    }
}

@@ -1496,32 +1496,43 @@
 ion();%0A        %7D
+%0A        /*
 %0A%0A        final 
@@ -3302,32 +3302,43 @@
 e()));%0A        %7D
+%0A        */
 %0A%0A        userRe
@@ -3880,13 +3880,12 @@
 me(%22
-first
+john
 %22);%0A
@@ -3927,12 +3927,11 @@
 me(%22
-last
+doe
 %22);%0A
@@ -3970,13 +3970,26 @@
 il(%22
-email
+johndoe@nus.edu.sg
 %22);%0A
@@ -4028,13 +4028,16 @@
 ne(%22
-phone
+12345678
 %22);%0A
@@ -4240,16 +4240,26 @@
 s1(%22
-address1
+computing drive 12
 %22);%0A
@@ -4287,23 +4287,32 @@
 dress2(%22
+another 
 address
+ 
 2%22);%0A   
@@ -4340,15 +4340,17 @@
 ry(%22
-country
+singapore
 %22);%0A
@@ -4380,14 +4380,12 @@
 on(%22
-region
+west
 %22);%0A
@@ -4416,15 +4416,14 @@
 de(%22
-zipcode
+123456
 %22);%0A

change fake user details (#DEV-237)