package com.qreal.robots.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.qreal.robots.dao.UserDAO;
import com.qreal.robots.model.auth.User;
import com.qreal.robots.model.robot.Message;
import com.qreal.robots.model.robot.Robot;
import com.qreal.robots.socket.SocketClient;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by dageev on 07.03.15.
 */

@Controller
public class MainController {

    public static final String HOST_NAME = "127.0.0.1";
    public static final int PORT = 9002;

    private static final Logger LOG = Logger.getLogger(MainController.class);

    private static final ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private UserDAO userDao;


    @RequestMapping("/")
    public ModelAndView home() throws IOException {
        User user = userDao.findByUserName(getUserName());
        ModelAndView model = new ModelAndView();
        model.addObject("user", user);
        SocketClient socketClient = new SocketClient(HOST_NAME, PORT);
        String response = socketClient.sendMessage(getUserOnlineRobots(user));
        Map<String, Boolean> onlineUserRobots = mapper.readValue(response,
                new TypeReference<Map<String, Boolean>>() {
                });

        setRobotsStatuses(user.getRobots(), onlineUserRobots);


        model.setViewName("index");
        return model;
    }

    private void setRobotsStatuses(Collection<Robot> robots, Map<String, Boolean> onlineRobots) {
        for (Robot robot : robots) {
            String status = onlineRobots.get(robot.getSecretCode()) ? "Online" : "Offline";
            robot.setStatus(status);
        }
    }

    private String getUserOnlineRobots(User user) throws JsonProcessingException {
        List<String> secretCodes = Lists.newArrayList();
        for (Robot robot : user.getRobots()) {
            secretCodes.add(robot.getSecretCode());
        }

        Message message = new Message("WebApp", "getOnlineRobots", user.getUsername(), secretCodes);
        return mapper.writeValueAsString(message);
    }


    private String getUserName() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

}

@@ -834,24 +834,50 @@
 Collection;%0A
+import java.util.HashMap;%0A
 import java.
@@ -1346,27 +1346,8 @@
 me()
- throws IOException
  %7B%0A 
@@ -1492,16 +1492,278 @@
  user);%0A
+        Map%3CString, Boolean%3E onlineUserRobots = getOnlineRobots(user);%0A%0A        setRobotsStatus(user.getRobots(), onlineUserRobots);%0A%0A%0A        model.setViewName(%22index%22);%0A        return model;%0A    %7D%0A%0A    private Map%3CString, Boolean%3E getOnlineRobots(User user) %7B%0A
         
@@ -1825,16 +1825,34 @@
  PORT);%0A
+        try %7B%0A    
         
@@ -1934,47 +1934,18 @@
     
-Map%3CString, Boolean%3E onlineUserRobots =
+    return
  map
@@ -1968,16 +1968,20 @@
 sponse,%0A
+    
         
@@ -2052,21 +2052,24 @@
     
+    
 %7D);%0A
-%0A
         
 setR
@@ -2068,100 +2068,100 @@
     
-setRobotsStatuses(user.getRobots(), onlineUserRobots);%0A%0A%0A        model.setViewName(%22index%22);
+%7D catch (IOException e) %7B%0A            LOG.error(%22Error getting online robots%22, e);%0A        %7D
 %0A   
@@ -2164,37 +2164,45 @@
 %0A        return 
-model
+new HashMap()
 ;%0A    %7D%0A%0A    pri
@@ -2226,18 +2226,16 @@
 tsStatus
-es
 (Collect
@@ -2339,22 +2339,23 @@
         
-String
+boolean
  status 
@@ -2400,30 +2400,58 @@
 ()) 
-? %22Online%22 : %22Offline%22
+!= null && onlineRobots.get(robot.getSecretCode())
 ;%0A  
@@ -2482,16 +2482,39 @@
 s(status
+ ? %22Online%22 : %22Offline%22
 );%0A     

fix exception