package ev3dev.actuators.ev3;

import ev3dev.hardware.EV3DevFileSystem;
import ev3dev.hardware.EV3DevPlatform;
import fake_ev3dev.ev3dev.actuators.FakeLed;
import fake_ev3dev.ev3dev.sensors.FakeBattery;
import lejos.hardware.LED;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;

public class EV3LedTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Before
    public void resetTest() throws IOException, NoSuchFieldException, IllegalAccessException {

        //Reset the singleton
        //https://stackoverflow.com/questions/8256989/singleton-and-unit-testing
        //Field instance = Sound.class.getDeclaredField("instance");
        //instance.setAccessible(true);
        //instance.set(null, null);

        FakeBattery.resetEV3DevInfrastructure();

        System.setProperty(EV3DevFileSystem.EV3DEV_TESTING_KEY, FakeBattery.EV3DEV_FAKE_SYSTEM_PATH);

    }

    @Test
    public void constructorLeftTest() throws Exception {

        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);

        LED led = new EV3Led(EV3Led.LEFT);
        led = new EV3Led(EV3Led.Direction.LEFT);
    }

    @Test
    public void constructorRightTest() throws Exception {

        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);

        LED led = new EV3Led(EV3Led.RIGHT);
        led = new EV3Led(EV3Led.Direction.RIGHT);
    }

    @Ignore("Review how to reset a Static classic in JUnit")
    @Test
    public void usingLedOnEV3BrickPlatformTest() throws Exception {

        thrown.expect(RuntimeException.class);

        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.BRICKPI);

        LED led = new EV3Led(EV3Led.LEFT);
    }

    @Test
    public void badButtonTest() throws Exception {

        thrown.expect(RuntimeException.class);

        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);

        LED led = new EV3Led(2);
    }

    @Test
    public void badDirectionTest() throws Exception {

        thrown.expect(IllegalArgumentException.class);

        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);

        LED led = new EV3Led(null);
    }

    @Test
    public void leftLedPatternsTest() throws Exception {

        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
        final FakeLed fakeLed = new FakeLed(EV3DevPlatform.EV3BRICK);

        LED led = new EV3Led(EV3Led.LEFT);
        led.setPattern(1);
        led.setPattern(2);
        led.setPattern(3);
        led.setPattern(4);

        led = new EV3Led(EV3Led.Direction.LEFT);
        led.setPattern(1);
        led.setPattern(2);
        led.setPattern(3);
        led.setPattern(4);
    }

    @Test
    public void rightLedPatternsTest() throws Exception {

        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
        final FakeLed fakeLed = new FakeLed(EV3DevPlatform.EV3BRICK);

        LED led = new EV3Led(EV3Led.RIGHT);
        led.setPattern(1);
        led.setPattern(2);
        led.setPattern(3);
        led.setPattern(4);

        led = new EV3Led(EV3Led.Direction.RIGHT);
        led.setPattern(1);
        led.setPattern(2);
        led.setPattern(3);
        led.setPattern(4);
    }

}

@@ -244,85 +244,9 @@
 nit.
-Before;%0Aimport org.junit.Ignore;%0Aimport org.junit.Rule;%0Aimport org.junit.Test
+*
 ;%0Aim
@@ -3322,10 +3322,479 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void getDirectionTest() throws Exception %7B%0A%0A        final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);%0A        final FakeLed fakeLed = new FakeLed(EV3DevPlatform.EV3BRICK);%0A%0A        EV3Led led = new EV3Led(EV3Led.RIGHT);%0A        Assert.assertEquals(EV3Led.Direction.RIGHT, led.getDirection());%0A%0A        led = new EV3Led(EV3Led.Direction.RIGHT);%0A        Assert.assertEquals(EV3Led.Direction.RIGHT, led.getDirection());%0A    %7D%0A%0A
 %7D%0A

Add test for EV3Led.getDirection() method