package net.rizov.learn.spring.boot.blog.service;

import java.util.Random;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import net.rizov.learn.spring.boot.blog.domain.Author;
import net.rizov.learn.spring.boot.blog.domain.Post;
import net.rizov.learn.spring.boot.blog.domain.PostComment;
import net.rizov.learn.spring.boot.blog.repository.AuthorRepository;
import net.rizov.learn.spring.boot.blog.repository.PostCommentRepository;
import net.rizov.learn.spring.boot.blog.repository.PostRepository;

@Service
@Profile("development")
public class DemoDataLoaderService
{
    private AuthorRepository authorRepository;

    private PostRepository postRepository;

    private PostCommentRepository postCommentRepository;

    private RandomStringService randomStringService;
    
    private DummyDateService dummyDateService;

    private Random random = new Random();
    
    @Autowired
    public DemoDataLoaderService(AuthorRepository authorRepository, PostRepository postRepository,
	    PostCommentRepository postCommentRepository, RandomStringService randomStringService,
	    DummyDateService dummyDateService)
    {
	super();
	this.authorRepository = authorRepository;
	this.postRepository = postRepository;
	this.postCommentRepository = postCommentRepository;
	this.randomStringService = randomStringService;
	this.dummyDateService = dummyDateService;
    }

    @PostConstruct
    public void loadData()
    {
	for (int i = 0; i < 100; i++)
	{
	    Author author = new Author(createRandomName(), createRandomName(), createRandomEmail(), "demo");
	    authorRepository.save(author);
	    
	    for (int j = 0; j < random.nextInt(10); j++)
	    {
		Post post = new Post(createRandomLine(), createRandomText(), author);
		post.setDateCreated(dummyDateService.createDate(random.nextInt(1000)));
		postRepository.save(post);
		
		for (int k = 0; k < random.nextInt(20); k++)
		{
		    PostComment comment = new PostComment(createRandomShortText(), post, author);
		    comment.setDateCreated(dummyDateService.createDate(random.nextInt(1000))); //may be inconsistent with post
		    postCommentRepository.save(comment);
		}
	    }
	}
    }

    private String createRandomName()
    {
	StringBuilder sb = new StringBuilder(randomStringService.nextString(random.nextInt(7) + 4));
	sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
	return sb.toString();
    }

    private String createRandomEmail()
    {
	return
		randomStringService.nextString(random.nextInt(7) + 4) + "@" +
		randomStringService.nextString(random.nextInt(7) + 4) + ".com";
    }
    
    private String createRandomLine()
    {
	StringBuilder sb = new StringBuilder();
	
	for (int i = 0; i < 3 + random.nextInt(12); i++)
	{
	    sb.append(randomStringService.nextString(1 + random.nextInt(10)) + " ");
	}
	
	sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
	
	return sb.toString().trim();
    }
    
    private String createRandomShortText()
    {
	return createRandomText(1, 5);
    }
    
    private String createRandomText()
    {
	return createRandomText(10, 30);
    }
    
    private String createRandomText(int minLines, int maxLines)
    {
	StringBuilder sb = new StringBuilder();
	
	for (int i = 0; i < minLines + random.nextInt(maxLines - minLines); i++)
	{
	    sb.append(createRandomLine() + ". ");
	}
	
	return sb.toString();
    }
    
}

@@ -928,21 +928,17 @@
 ervice;%0A
-    
 %0A
+
     priv
@@ -1019,21 +1019,17 @@
 ndom();%0A
-    
 %0A
+
     @Aut
@@ -1621,16 +1621,110 @@
 author =
+ i == 0 ? new Author(createRandomName(), createRandomName(), %22admin@demo.com%22, %22demo%22)%0A%09%09    :
  new Aut
@@ -1797,16 +1797,17 @@
 demo%22);%0A
+%0A
 %09    aut
@@ -1838,14 +1838,9 @@
 r);%0A
-%09    
 %0A
+
 %09   
@@ -2071,11 +2071,9 @@
 t);%0A
-%09%09
 %0A
+
 %09%09fo
@@ -2290,32 +2290,117 @@
 ; //
+ 
 may
- be inconsistent with
+%0A%09%09%09%09%09%09%09%09%09%09%09       // be%0A%09%09%09%09%09%09%09%09%09%09%09       // inconsistent%0A%09%09%09%09%09%09%09%09%09%09%09       // with%0A%09%09%09%09%09%09%09%09%09%09%09       //
  pos
@@ -2740,19 +2740,17 @@
 %0A%09return
-%0A%09%09
+ 
 randomSt
@@ -2804,13 +2804,13 @@
  %22@%22
- +
 %0A%09%09
++ 
 rand
@@ -2867,37 +2867,33 @@
 + %22.com%22;%0A    %7D%0A
-    
 %0A
+
     private Stri
@@ -2953,34 +2953,33 @@
 tringBuilder();%0A
-%09
 %0A
+
 %09for (int i = 0;
@@ -3096,18 +3096,17 @@
  %22);%0A%09%7D%0A
-%09
 %0A
+
 %09sb.setC
@@ -3152,18 +3152,17 @@
 t(0)));%0A
-%09
 %0A
+
 %09return 
@@ -3181,37 +3181,33 @@
 ).trim();%0A    %7D%0A
-    
 %0A
+
     private Stri
@@ -3269,37 +3269,33 @@
 xt(1, 5);%0A    %7D%0A
-    
 %0A
+
     private Stri
@@ -3362,21 +3362,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     priv
@@ -3474,18 +3474,17 @@
 lder();%0A
-%09
 %0A
+
 %09for (in
@@ -3602,10 +3602,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09ret
@@ -3628,15 +3628,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

add admin@demo.com:demo demo user