package powerups.core.clubimage;

import com.avaje.ebean.Ebean;
import com.fasterxml.jackson.databind.JsonNode;
import models.Club;
import models.PowerupModel;
import play.mvc.Results;
import play.twirl.api.Html;
import powerups.Powerup;
import powerups.core.clubimage.html.powerup;
import powerups.models.ClubImage;
import utils.MembershipLevel;

public class ClubImagePowerup extends Powerup {

    public static final String DEFAULT_IMAGE = "/assets/images/no_club_image.jpg";

    private final ClubImage clubImage;

    public ClubImagePowerup(Club club, PowerupModel model) {
        super(club, model);

        clubImage = ClubImage.find.byId(new ClubImage(this.getClub(), "").key);
    }

    @Override
    public Html render() {
        return powerup.render(this.clubImage.imageUrl, false);
    }

    @Override
    public Html renderAdmin() {
        return powerup.render(this.clubImage.imageUrl, true);
    }

    @Override
    public void activate() {
        ClubImage clubImage = new ClubImage(this.getClub(), DEFAULT_IMAGE);
        Ebean.save(clubImage);
    }

    @Override
    public void deActivate() {
        Ebean.delete(clubImage);
    }

    @Override
    public play.mvc.Result update(JsonNode updateContent) {
        if (!(this.getContext().getMemberLevel().getLevel() >= MembershipLevel.BOARD.getLevel())) {
            return Results.unauthorized();
        }
        this.clubImage.imageUrl = updateContent.get("link").asText();
        if(this.clubImage.imageUrl.equals("")) {
            this.clubImage.imageUrl = DEFAULT_IMAGE;
        }
        Ebean.update(clubImage);
        return Results.ok("Bilde endret!");
    }

}

@@ -153,16 +153,40 @@
 pModel;%0A
+import play.mvc.Result;%0A
 import p
@@ -202,16 +202,16 @@
 esults;%0A
-
 import p
@@ -363,16 +363,77 @@
 ipLevel;
+%0Aimport utils.imaging.ImageLinkValidator;%0A%0Aimport java.awt.*;
 %0A%0Apublic
@@ -1269,25 +1269,16 @@
  public 
-play.mvc.
 Result u
@@ -1470,37 +1470,24 @@
         
-this.clubImage.imageU
+String u
 rl = upd
@@ -1531,44 +1531,232 @@
     
-if(this.clubImage.imageUrl.equals(%22%22
+ImageLinkValidator validator = new ImageLinkValidator(new Dimension(800, 300), new Dimension(1600, 600));%0A        ImageLinkValidator.StatusMessage statusMessage = validator.validate(url);%0A%0A        if(statusMessage.isSuccess(
 )) %7B
@@ -1768,21 +1768,16 @@
         
-this.
 clubImag
@@ -1793,33 +1793,17 @@
 l = 
-DEFAULT_IMAGE;%0A        %7D%0A
+url;%0A    
     
@@ -1831,32 +1831,36 @@
 Image);%0A        
+  
+  
 return Results.o
@@ -1865,25 +1865,130 @@
 .ok(
-%22Bilde endret!%22);
+statusMessage.getMessage());%0A        %7D else %7B%0A            return Results.badRequest(statusMessage.getMessage());%0A        %7D
 %0A   

Make ClubImagePowerup use the new ImageLinkValidator