package com.fundynamic.d2tm.game.controls;

import com.fundynamic.d2tm.game.map.MapCell;
import com.fundynamic.d2tm.game.math.Vector2D;
import com.fundynamic.d2tm.game.structures.Structure;
import junit.framework.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.newdawn.slick.Image;

import static com.fundynamic.d2tm.game.map.CellFactory.makeCell;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static org.junit.Assert.assertNull;

public class MouseTest {

    @Test
    public void returnsCellItHoversOver() {
        MapCell hoverCell = makeCell();
        Mouse mouse = new Mouse(hoverCell);
        Assert.assertSame(hoverCell, mouse.getHoverCell());
    }

    @Test
    public void updatesHoverCell() {
        Mouse mouse = new Mouse(makeCell());
        MapCell hoverCell = makeCell();
        mouse.setHoverCell(hoverCell);
        Assert.assertSame(hoverCell, mouse.getHoverCell());
    }

    @Test (expected = IllegalArgumentException.class)
    public void throwsIllegalArgumentWhenHoverCellIsNull() {
        Mouse mouse = new Mouse(makeCell());
        mouse.setHoverCell(null);
    }

    @Test
    public void selectStructureOnCell() {
        MapCell hoverCell = makeCell();
        Structure structure = new Structure(Vector2D.zero(), Mockito.mock(Image.class), 64, 64);
        hoverCell.setStructure(structure);
        Mouse mouse = new Mouse(hoverCell);
        mouse.selectStructure();
        assertEquals(structure, mouse.getLastSelectedStructure());
    }

    @Test
    public void lastSelectedStructureIsNullWhenNoStructureIsSelected() {
        assertNull(new Mouse(makeCell()).getLastSelectedStructure());
    }

    @Test
    public void selectStructureOnCellWithoutStructureWillNotInfluenceLastSelectedStructure() {
        MapCell hoverCell = makeCell();
        Mouse mouse = new Mouse(hoverCell);

        Structure structure = new Structure(Vector2D.zero(), Mockito.mock(Image.class), 64, 64);
        hoverCell.setStructure(structure);
        mouse.selectStructure();

        MapCell hoverCellWithoutStructure = makeCell();
        mouse.setHoverCell(hoverCellWithoutStructure);
        mouse.selectStructure(); // this should ignore the fact that the new hover cell has no structure

        assertEquals(structure, mouse.getLastSelectedStructure());
    }

    @Test
    public void deselectStructure() {
        MapCell hoverCell = makeCell();
        Mouse mouse = new Mouse(hoverCell);

        Structure structure = new Structure(Vector2D.zero(), Mockito.mock(Image.class), 64, 64);
        hoverCell.setStructure(structure);
        mouse.selectStructure();
        mouse.deselectStructure();

        assertNull(mouse.getLastSelectedStructure());
        assertFalse(structure.isSelected());
    }

}
@@ -214,16 +214,41 @@
 Assert;%0A
+import org.junit.Before;%0A
 import o
@@ -567,53 +567,101 @@
     
-@Test%0A    public void returnsCellItHoversOver
+private MapCell defaultHoverCell;%0A    private Mouse mouse;%0A%0A    @Before%0A    public void setUp
 () %7B
@@ -665,33 +665,32 @@
 ) %7B%0A        
-MapCell h
+defaultH
 overCell = m
@@ -700,38 +700,32 @@
 Cell();%0A        
-Mouse 
 mouse = new Mous
@@ -718,33 +718,40 @@
 use = new Mouse(
-h
+defaultH
 overCell);%0A     
@@ -737,32 +737,93 @@
 aultHoverCell);%0A
+    %7D%0A%0A    @Test%0A    public void returnsCellItHoversOver() %7B%0A
         Assert.a
@@ -824,33 +824,40 @@
 sert.assertSame(
-h
+defaultH
 overCell, mouse.
@@ -940,61 +940,19 @@
    M
-ouse mouse = new Mouse(makeCell());%0A        MapCell h
+apCell newH
 over
@@ -989,33 +989,36 @@
 se.setHoverCell(
-h
+newH
 overCell);%0A     
@@ -1038,17 +1038,20 @@
 ertSame(
-h
+newH
 overCell
@@ -1201,53 +1201,8 @@
 ) %7B%0A
-        Mouse mouse = new Mouse(makeCell());%0A
     
@@ -1294,48 +1294,8 @@
 ) %7B%0A
-        MapCell hoverCell = makeCell();%0A
     
@@ -1387,33 +1387,40 @@
 4, 64);%0A        
-h
+defaultH
 overCell.setStru
@@ -1441,52 +1441,8 @@
 e);%0A
-        Mouse mouse = new Mouse(hoverCell);%0A
     
@@ -1650,29 +1650,13 @@
 ull(
-new Mouse(makeCell())
+mouse
 .get
@@ -1797,93 +1797,8 @@
 ) %7B%0A
-        MapCell hoverCell = makeCell();%0A        Mouse mouse = new Mouse(hoverCell);%0A%0A
     
@@ -1890,33 +1890,40 @@
 4, 64);%0A        
-h
+defaultH
 overCell.setStru
@@ -2312,97 +2312,48 @@
 ture
-() %7B%0A        MapCell hoverCell = makeCell();%0A        Mouse mouse = new Mouse(hoverCell);%0A
+DeselectsPreviouslySelectedStructure() %7B
 %0A   
@@ -2454,17 +2454,24 @@
         
-h
+defaultH
 overCell
@@ -2671,9 +2671,397 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void deselectStructureIgnoresNullHoverCell() %7B%0A        mouse = new Mouse(null);%0A        mouse.deselectStructure();%0A        assertNull(mouse.getLastSelectedStructure());%0A    %7D%0A%0A    @Test%0A    public void deselectStructureIgnoresWhenNoStructureWasSelectedInTheFirstPlace() %7B%0A        mouse.deselectStructure();%0A        assertNull(mouse.getLastSelectedStructure());%0A    %7D%0A
 %7D

refactor so that mouse is constructed in @Before and add 2 missing test cases