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 UserEntity 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() != null) {
            one.getUserDetails().setFirstName(userUserDetails.getFirstName());
        }

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

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

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

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

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

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

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

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

        if (oneAddress.getZipCode() != null) {
            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("john");
        userDetailsEntity.setLastName("doe");
        userDetailsEntity.setEmail("johndoe@nus.edu.sg");
        userDetailsEntity.setPhone("12345678");

        final AddressEntity address = new AddressEntity();
        address.setCreatedDate(ZonedDateTime.now());
        address.setLastModifiedDate(ZonedDateTime.now());
        address.setAddress1("computing drive 12");
        address.setAddress2("another address 2");
        address.setCountry("singapore");
        address.setRegion("west");
        address.setZipCode("123456");

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

        userRepository.save(userEntity);
    }
}

@@ -1514,19 +1514,8 @@
    %7D
-%0A        /*
 %0A%0A  
@@ -1649,35 +1649,36 @@
 );%0A%0A        if (
-one
+user
 UserDetails.getF
@@ -1793,35 +1793,36 @@
  %7D%0A%0A        if (
-one
+user
 UserDetails.getL
@@ -1934,35 +1934,36 @@
  %7D%0A%0A        if (
-one
+user
 UserDetails.getE
@@ -2074,19 +2074,20 @@
     if (
-one
+user
 UserDeta
@@ -2341,35 +2341,76 @@
 );%0A%0A        if (
-one
+userAddress != null) %7B%0A%0A            if (user
 Address.getAddre
@@ -2430,32 +2430,36 @@
 ) %7B%0A            
+   
+ 
 one.getUserDetai
@@ -2520,35 +2520,42 @@
 ));%0A        
-%7D%0A%0A
+    %7D%0A%0A   
         
 if (oneAddre
@@ -2534,39 +2534,41 @@
   %7D%0A%0A           
+ 
 if (
-one
+user
 Address.getAddre
@@ -2588,32 +2588,36 @@
 ) %7B%0A            
+ 
+   
 one.getUserDetai
@@ -2678,35 +2678,42 @@
 ));%0A        
-%7D%0A%0A
+    %7D%0A%0A   
         
 if (oneAddre
@@ -2692,39 +2692,41 @@
   %7D%0A%0A           
+ 
 if (
-one
+user
 Address.getCount
@@ -2745,32 +2745,36 @@
 ) %7B%0A            
+    
 one.getUserDetai
@@ -2829,32 +2829,36 @@
 try());%0A        
+    
 %7D%0A%0A        if (o
@@ -2844,39 +2844,44 @@
      %7D%0A%0A        
+  
+  
 if (
-one
+user
 Address.getRegio
@@ -2887,32 +2887,36 @@
 on() != null) %7B%0A
+    
             one.
@@ -2981,35 +2981,43 @@
 ion());%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         if (oneA
@@ -3012,19 +3012,20 @@
     if (
-one
+user
 Address.
@@ -3040,32 +3040,36 @@
 de() != null) %7B%0A
+    
             one.
@@ -3138,32 +3138,36 @@
 de()));%0A        
+    
 %7D%0A        */%0A%0A  
@@ -3160,18 +3160,17 @@
         
-*/
+%7D
 %0A%0A      

fix bug for update user