package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.inject.Inject;
import services.*;
import models.*;
import play.Logger;
import play.mvc.Controller;
import play.mvc.Result;
import powerups.Powerup;
import utils.ActivationSorter;
import utils.Authorize;
import utils.MembershipLevel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class Clubs extends Controller {

    @Inject
    private ClubService clubService;
    @Inject
    private ActivationService activationService;
    @Inject
    private UserService userService;
    @Inject
    private LocationService locationService;
    @Inject
    private PowerupService powerupService;
    @Inject
    private MembershipService membershipService;

    public Result index() {
        List<Location> locations = locationService.findAll();
        List<Club> byLocation = clubService.findByLocation(null);

        // Set up pseudo-location(null in database) to hold all global clubs
        Location global = new Location();
        global.setName("Felles");
        global.getClubs().addAll(byLocation);
        locations.add(0, global);

        int cssId = 0;
        for (Location location : locations) {
            cssId++;
            location.setCssId(cssId);
        }
        return ok(views.html.club.index.render(locations));
    }

    public Result show(Long id) {
        Club club = clubService.findById(id);
        if (club == null) {
            return redirect(routes.Clubs.index());
        }

        ArrayList<Powerup> powerups = new ArrayList<>();
        club.setPowerups(powerups);
        // Sort the activations by weight:
        Collections.sort(club.getActivations(), new ActivationSorter());

        for (Activation activation : club.getActivations()) {
            Powerup powerup = activation.getPowerup();
            powerups.add(powerup);
        }
        return ok(views.html.club.show.render(club));
    }

    public Result update() {
        final Map<String, String[]> postValues = request().body().asFormUrlEncoded();

        final Long id = Long.valueOf(postValues.get("id")[0]);
        final String newName = postValues.get("name")[0];
        final Club club = clubService.findById(id);

        club.setName(newName);
        //club.description = newDescription;

        Club.update(club);

        return redirect(routes.Clubs.index());
    }

    public Result create() {
        try {
            User user = new Authorize.UserSession().getUser();
            boolean authorized = user.isAdmin();
            if (!authorized) {
                return unauthorized();
            }
        } catch (Authorize.SessionException e) {
            e.printStackTrace();
        }

        Map<String, String[]> form = request().body().asFormUrlEncoded();
        for (String key : form.keySet()) {
            Logger.warn(key + ":" + form.get(key)[0]);
        }
        String name = form.get("name")[0];
        String shortName = form.get("shortName")[0];
        String email = form.get("leader")[0] + form.get("postfix")[0];
        Long locationId = Long.valueOf(form.get("location")[0]);

        Location location = locationService.findById(locationId);

        User leaderUser = userService.findByEmail(email);

        if (leaderUser == null) {
            return badRequest("Det finnes ingen bruker tilknyttet den emailen, " +
                    "vennligst skriv inn en email knyttet til en bruker");
        }

        if (name.equals("") || shortName.equals("")) {
            return badRequest("Utvalget kan ikke ha et tomt Navn eller tom forkortelse");
        }

        Club club = new Club(name, shortName, location);
        clubService.save(club);

        Membership membership = new Membership(club, leaderUser, MembershipLevel.LEADER);
        club.getMembers().add(membership);

        ArrayList<Activation> activations = new ArrayList<>();
        powerupService.findAllMandatory().forEach(model -> {
            Activation activation = new Activation(club, model, model.getDefaultWeight());
            club.getActivations().add(activation);
            activations.add(activation);
        });

        membershipService.save(membership);

        for (Activation activation : activations) {
            activationService.save(activation);
            activation.getPowerup().activate();
        }
        return redirect(routes.Clubs.show(club.getId()));
    }


    public Result updatePowerup(Long clubId, Long powerupId) {
        JsonNode json = request().body().asJson();
        Logger.warn("RECEIVED JSON");
        if (json == null || json.isNull()) {
            return badRequest("Expecting Json data");
        }
        final Club club = clubService.findById(clubId);
        Powerup powerup = null;
        for (Activation activation : club.getActivations()) {
            if (activation.getPowerupModel().getId().equals(powerupId)) {
                powerup = activation.getPowerup();
            }
        }
        if (powerup == null) {
            return badRequest("No such powerup for " + club.getShortName());
        } else return powerup.update(json);
    }

    /**
     * WARNING: THIS RETURNS THE ADMIN FUNCTION
     */
    public Result getPowerupContent(Long clubId, Long powerupId) {
        final Club club = clubService.findById(clubId);

        Powerup powerup = null;

        if (club == null)
            return notFound(views.html.index.render("Utvalget du leter etter finnes ikke."));

        for (Activation activation : club.getActivations()) {
            if (activation.getPowerupModel().getId().equals(powerupId)) {
                powerup = activation.getPowerup();
            }
        }
        if (powerup == null) {
            return notFound(views.html.index.render("Poweruppen du leter etter finnes ikke for" + club.getShortName()));
        } else return ok(powerup.renderAdmin());
    }

}

@@ -100,27 +100,8 @@
 ct;%0A
-import services.*;%0A
 impo
@@ -113,16 +113,16 @@
 dels.*;%0A
+
 import p
@@ -185,16 +185,47 @@
 Result;%0A
+import play.twirl.api.Content;%0A
 import p
@@ -241,16 +241,35 @@
 owerup;%0A
+import services.*;%0A
 import u
@@ -295,32 +295,8 @@
 er;%0A
-import utils.Authorize;%0A
 impo
@@ -1339,32 +1339,42 @@
       return ok(
+(Content) 
 views.html.club.
@@ -1966,16 +1966,26 @@
 turn ok(
+(Content) 
 views.ht
@@ -2501,26 +2501,8 @@
 ) %7B%0A
-        try %7B%0A    
     
@@ -2521,35 +2521,44 @@
 r = 
-new Authorize.
+userService.getCurrent
 User
-S
+(s
 ession()
 .get
@@ -2557,24 +2557,11 @@
 on()
-.getUser();%0A    
+);%0A
     
@@ -2593,24 +2593,35 @@
 user
-.isAdmin();%0A    
+Service.isUserAdmin(user);%0A
     
@@ -2651,28 +2651,24 @@
             
-    
 return unaut
@@ -2682,104 +2682,8 @@
 ();%0A
-            %7D%0A        %7D catch (Authorize.SessionException e) %7B%0A            e.printStackTrace();%0A
     
@@ -5388,32 +5388,42 @@
 return notFound(
+(Content) 
 views.html.index
@@ -5707,32 +5707,32 @@
 erup == null) %7B%0A
-
             retu
@@ -5743,16 +5743,26 @@
 otFound(
+(Content) 
 views.ht

fix ambigious casting in club controller