package br.com.minitwitter.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import br.com.minitwitter.model.Tweet;
import br.com.minitwitter.model.User;
import br.com.minitwitter.service.NotificationService;
import br.com.minitwitter.service.TweetService;
import br.com.minitwitter.service.UserService;
import br.com.minitwitter.util.TweetUtil;

@Controller
public class TweetContoller {
  
  private UserService userService;
  private TweetService tweetService;
  private NotificationService notificationService;
  
  @Autowired
  public void setUserService(UserService userService) {
    this.userService = userService;
  }
  
  @Autowired
  public void setTweetService(TweetService tweetService) {
    this.tweetService = tweetService;
  }
  
  @Autowired
  public void setNotificationService(NotificationService notificationService) {
    this.notificationService = notificationService;
  }

  @PostMapping("/tweet")
  public String newTweet(@RequestParam("content") String content) {
    Authentication authenticated = SecurityContextHolder.getContext()
        .getAuthentication();
    Set<String> mentionedUsers = TweetUtil.getMentionedUsers(content);
    String username = authenticated.getName().toLowerCase();
    User user = new User();
    user.setUsername(username);
    Tweet tweet = new Tweet();
    tweet.setContent(content);
    tweet.setPoster(user);
    tweet.setTimeday(Calendar.getInstance());
    
    tweetService.save(tweet);
    TweetUtil.sendNotification(mentionedUsers, userService, notificationService, tweet);
    
    return "redirect:/feed";
  }


  @GetMapping("/feed")
  public String feed(Model model) {
    Authentication authenticated = SecurityContextHolder.getContext()
        .getAuthentication();
    authenticated.getAuthorities().forEach(a -> System.out.println(a.getAuthority()));
    String username = authenticated.getName().toLowerCase();
    
    User user = userService.loadUserByUsername(username);
    
    List<User> following = user.getFollowing();
    ArrayList<Tweet> feedTweets = new ArrayList<>();
    feedTweets.addAll(user.getTweets());
    
    for(User userFollowed : following) {
      List<Tweet> tweets = userFollowed.getTweets();
      feedTweets.addAll(tweets);
    }
    
    feedTweets.sort((t1,t2) -> t2.getTimeday().compareTo(t1.getTimeday()));
    model.addAttribute("tweets", feedTweets);
    model.addAttribute("content", "");
    
    return "tweets/feed";
  }
  
  @GetMapping("tweet/{id}")
  public String tweetDetails(@PathVariable("id") Long id ,Model model) {
    
    Tweet tweet = tweetService.getTweetBy(id);
    
    model.addAttribute("tweet", tweet);
    
    return "tweets/details";
  }
  
  @GetMapping("allTweets")
  public String allTweets(Model model) {
    
    Iterable<Tweet> tweets = tweetService.allTweets();
    
    model.addAttribute("tweets", tweets);
    
    return "tweets/listTweets";
  }
  
}

@@ -967,19 +967,17 @@
 oller %7B%0A
-  
 %0A
+
   privat
@@ -1091,19 +1091,17 @@
 ervice;%0A
-  
 %0A
+
   @Autow
@@ -1193,35 +1193,33 @@
 serService;%0A  %7D%0A
-  
 %0A
+
   @Autowired%0A  p
@@ -1316,19 +1316,17 @@
 ce;%0A  %7D%0A
-  
 %0A
+
   @Autow
@@ -1983,29 +1983,25 @@
 nstance());%0A
-    
 %0A
+
     tweetSer
@@ -2098,16 +2098,24 @@
 Service,
+%0A       
  tweet);
@@ -2107,36 +2107,32 @@
         tweet);%0A
-    
 %0A    return %22red
@@ -2150,17 +2150,16 @@
 %22;%0A  %7D%0A%0A
-%0A
   @GetMa
@@ -2313,95 +2313,8 @@
 ();%0A
-    authenticated.getAuthorities().forEach(a -%3E System.out.println(a.getAuthority()));%0A
     
@@ -2366,29 +2366,25 @@
 owerCase();%0A
-    
 %0A
+
     User use
@@ -2429,21 +2429,17 @@
 rname);%0A
-    
 %0A
+
     List
@@ -2576,20 +2576,16 @@
 ));%0A
-    
 %0A    for
 (Use
@@ -2580,16 +2580,17 @@
 %0A    for
+ 
 (User us
@@ -2707,20 +2707,16 @@
 ;%0A    %7D%0A
-    
 %0A    fee
@@ -2732,16 +2732,17 @@
 ort((t1,
+ 
 t2) -%3E t
@@ -2870,21 +2870,17 @@
 %22, %22%22);%0A
-    
 %0A
+
     retu
@@ -2897,27 +2897,25 @@
 /feed%22;%0A  %7D%0A
-  
 %0A
+
   @GetMappin
@@ -2990,10 +2990,10 @@
 g id
- 
 ,
+ 
 Mode
@@ -2999,28 +2999,24 @@
 el model) %7B%0A
-    
 %0A    Tweet t
@@ -3051,21 +3051,17 @@
 By(id);%0A
-    
 %0A
+
     mode
@@ -3092,21 +3092,17 @@
 tweet);%0A
-    
 %0A
+
     retu
@@ -3126,19 +3126,17 @@
 s%22;%0A  %7D%0A
-  
 %0A
+
   @GetMa
@@ -3195,21 +3195,17 @@
 odel) %7B%0A
-    
 %0A
+
     Iter
@@ -3251,21 +3251,17 @@
 eets();%0A
-    
 %0A
+
     mode
@@ -3298,13 +3298,9 @@
 s);%0A
-    
 %0A
+
     
@@ -3331,13 +3331,11 @@
 s%22;%0A  %7D%0A
-  
 %0A%7D%0A

Delete log line from TweetContoller