// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package org.wpilib.simulation;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.wpilib.hardware.hal.HAL;
import org.wpilib.hardware.pneumatic.CompressorConfigType;
import org.wpilib.hardware.pneumatic.DoubleSolenoid;
import org.wpilib.hardware.pneumatic.PneumaticHub;
import org.wpilib.hardware.pneumatic.PneumaticsModuleType;
import org.wpilib.simulation.testutils.BooleanCallback;
import org.wpilib.simulation.testutils.DoubleCallback;
import org.wpilib.simulation.testutils.EnumCallback;

class REVPHSimTest {
  @Test
  void testInitialization() {
    HAL.initialize(500, 0);

    REVPHSim sim = new REVPHSim(1);
    sim.resetData();
    assertFalse(sim.getInitialized());

    BooleanCallback callback = new BooleanCallback();

    try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
        PneumaticHub ph = new PneumaticHub(1)) {
      assertTrue(sim.getInitialized());
    }
    assertFalse(sim.getInitialized());
  }

  @Test
  void solenoidOutputTest() {
    HAL.initialize(500, 0);

    try (PneumaticHub ph = new PneumaticHub(1);
        DoubleSolenoid doubleSolenoid = new DoubleSolenoid(1, PneumaticsModuleType.REV_PH, 3, 4)) {
      REVPHSim sim = new REVPHSim(ph);
      sim.resetData();

      BooleanCallback callback3 = new BooleanCallback();
      BooleanCallback callback4 = new BooleanCallback();

      try (CallbackStore cb3 = sim.registerSolenoidOutputCallback(3, callback3, false);
          CallbackStore cb4 = sim.registerSolenoidOutputCallback(4, callback4, false)) {
        // Reverse
        callback3.reset();
        callback4.reset();
        doubleSolenoid.set(DoubleSolenoid.Value.REVERSE);
        assertFalse(callback3.wasTriggered());
        assertNull(callback3.getSetValue());
        assertTrue(callback4.wasTriggered());
        assertTrue(callback4.getSetValue());
        assertFalse(sim.getSolenoidOutput(3));
        assertTrue(sim.getSolenoidOutput(4));
        assertEquals(0x10, ph.getSolenoids());

        // Forward
        callback3.reset();
        callback4.reset();
        doubleSolenoid.set(DoubleSolenoid.Value.FORWARD);
        assertTrue(callback3.wasTriggered());
        assertTrue(callback3.getSetValue());
        assertTrue(callback4.wasTriggered());
        assertFalse(callback4.getSetValue());
        assertTrue(sim.getSolenoidOutput(3));
        assertFalse(sim.getSolenoidOutput(4));
        assertEquals(0x8, ph.getSolenoids());

        // Off
        callback3.reset();
        callback4.reset();
        doubleSolenoid.set(DoubleSolenoid.Value.OFF);
        assertTrue(callback3.wasTriggered());
        assertFalse(callback3.getSetValue());
        assertFalse(callback4.wasTriggered());
        assertNull(callback4.getSetValue());
        assertFalse(sim.getSolenoidOutput(3));
        assertFalse(sim.getSolenoidOutput(4));
        assertEquals(0x0, ph.getSolenoids());
      }
    }
  }

  @Test
  void setCompressorOnTest() {
    HAL.initialize(500, 0);

    REVPHSim sim = new REVPHSim(1);
    BooleanCallback callback = new BooleanCallback();

    try (PneumaticHub ph = new PneumaticHub(1);
        CallbackStore cb = sim.registerCompressorOnCallback(callback, false)) {
      assertFalse(ph.getCompressor());
      assertFalse(sim.getCompressorOn());
      sim.setCompressorOn(true);
      assertTrue(ph.getCompressor());
      assertTrue(sim.getCompressorOn());
      assertTrue(callback.wasTriggered());
      assertTrue(callback.getSetValue());
    }
  }

  @Test
  void setEnableDigital() {
    HAL.initialize(500, 0);

    REVPHSim sim = new REVPHSim(1);
    EnumCallback callback = new EnumCallback();

    try (PneumaticHub ph = new PneumaticHub(1);
        CallbackStore cb = sim.registerCompressorConfigTypeCallback(callback, false)) {
      ph.disableCompressor();
      assertEquals(ph.getCompressorConfigType(), CompressorConfigType.DISABLED);

      ph.enableCompressorDigital();
      assertEquals(sim.getCompressorConfigType(), CompressorConfigType.DIGITAL.getValue());
      assertEquals(ph.getCompressorConfigType(), CompressorConfigType.DIGITAL);
      assertTrue(callback.wasTriggered());
      assertEquals(callback.getSetValue(), CompressorConfigType.DIGITAL.getValue());
    }
  }

  @Test
  void setEnableAnalog() {
    HAL.initialize(500, 0);

    REVPHSim sim = new REVPHSim(1);
    EnumCallback callback = new EnumCallback();

    try (PneumaticHub ph = new PneumaticHub(1);
        CallbackStore cb = sim.registerCompressorConfigTypeCallback(callback, false)) {
      ph.disableCompressor();
      assertEquals(ph.getCompressorConfigType(), CompressorConfigType.DISABLED);

      ph.enableCompressorAnalog(1, 2);
      assertEquals(sim.getCompressorConfigType(), CompressorConfigType.ANALOG.getValue());
      assertEquals(ph.getCompressorConfigType(), CompressorConfigType.ANALOG);
      assertTrue(callback.wasTriggered());
      assertEquals(callback.getSetValue(), CompressorConfigType.ANALOG.getValue());
    }
  }

  @Test
  void setEnableHybrid() {
    HAL.initialize(500, 0);

    REVPHSim sim = new REVPHSim(1);
    EnumCallback callback = new EnumCallback();

    try (PneumaticHub ph = new PneumaticHub(1);
        CallbackStore cb = sim.registerCompressorConfigTypeCallback(callback, false)) {
      ph.disableCompressor();
      assertEquals(ph.getCompressorConfigType(), CompressorConfigType.DISABLED);

      ph.enableCompressorHybrid(1, 2);
      assertEquals(sim.getCompressorConfigType(), CompressorConfigType.HYBRID.getValue());
      assertEquals(ph.getCompressorConfigType(), CompressorConfigType.HYBRID);
      assertTrue(callback.wasTriggered());
      assertEquals(callback.getSetValue(), CompressorConfigType.HYBRID.getValue());
    }
  }

  @Test
  void setPressureSwitchEnabledTest() {
    HAL.initialize(500, 0);

    REVPHSim sim = new REVPHSim(1);
    BooleanCallback callback = new BooleanCallback();

    try (PneumaticHub ph = new PneumaticHub(1);
        CallbackStore cb = sim.registerPressureSwitchCallback(callback, false)) {
      assertFalse(ph.getPressureSwitch());

      sim.setPressureSwitch(true);
      assertTrue(sim.getPressureSwitch());
      assertTrue(ph.getPressureSwitch());
      assertTrue(callback.wasTriggered());
      assertTrue(callback.getSetValue());
    }
  }

  @Test
  void setCompressorCurrentTest() {
    HAL.initialize(500, 0);

    REVPHSim sim = new REVPHSim(1);
    DoubleCallback callback = new DoubleCallback();

    try (PneumaticHub ph = new PneumaticHub(1);
        CallbackStore cb = sim.registerCompressorCurrentCallback(callback, false)) {
      assertFalse(ph.getPressureSwitch());

      sim.setCompressorCurrent(35.04);
      assertEquals(35.04, sim.getCompressorCurrent());
      assertEquals(35.04, ph.getCompressorCurrent());
      assertTrue(callback.wasTriggered());
      assertEquals(35.04, callback.getSetValue());
    }
  }
}
