/*
Controller for Accounts Page
 */

package samuelb.capripol.Controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import samuelb.capripol.*;
import samuelb.capripol.Repositories.*;
import samuelb.capripol.Services.GroupService;
import samuelb.capripol.Services.UserDetailsServiceImpl;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Controller
public class AccountController {

    @Autowired
    private GroupRepository groupRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private FocusRepository focusRepository;

    @Autowired
    private FrameworkRepository frameworkRepository;

    @Autowired
    private GroupService groupService;

    @Autowired
    private SightingRepository sightingRepository;

    @Autowired
    private GroupRoleRepository groupRoleRepository;

    private static Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    /*
    Loading claim data for a user. If 'view' is clicked on the admin panel, the username
    of the selected user is passed, if not then the current user is used
     */
    @GetMapping("/Account")
    public ModelAndView viewAccount(@RequestParam (required = false) String username, Model model, Principal principal){
        if(username == null) {
            username = principal.getName();
        }
        User user = userRepository.findByUserName(username);
        if(user != null){
            List<Sighting> self = sightingRepository.findSelfClaims(user.getUserId());
            List<Sighting> peer = sightingRepository.findByRelationship("peer", user.getUserId());
            List<Sighting> peerGiven = sightingRepository.findByRelationshipGiven("peer", user.getUserId());
            List<Sighting> other = sightingRepository.findByRelationship("other", user.getUserId());
            List<Sighting> otherGiven = sightingRepository.findByRelationshipGiven("other", user.getUserId());
            int total = self.size() + peer.size() + other.size();
            int totalGiven = peerGiven.size() + otherGiven.size();
            model.addAttribute("total", total);
            model.addAttribute("totalGiven", totalGiven);
            model.addAttribute("self", self);
            model.addAttribute("peer", peer);
            model.addAttribute("peerGiven", peerGiven);
            model.addAttribute("other", other);
            model.addAttribute("otherGiven", otherGiven);
            model.addAttribute("user", user);
            return new ModelAndView("Account");
        }else{
            model.addAttribute("error", "An error has occured, please log in again.");
            return new ModelAndView("login");
        }
    }

    //Getting data for the DataTable, JSON is returned in the response body
    @GetMapping(value = "/AccountData", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<String[]> accountData(@RequestParam(required = false) String clicked, Model model, Principal principal) {
        List<String[]> data = new ArrayList<>();
        User user = userRepository.findByUserName(principal.getName());
        if(clicked == null || clicked.equals("group")) {//which link/table is clicked
            for (Group group : user.getGroups()) {
                String[] userDetails = {group.getGroupName(), user.rolesForGroup(group)};
                data.add(userDetails);
            }
            model.addAttribute("user", user);
        }
        return data;
    }
}
