package hws.hw3;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import org.junit.jupiter.api.BeforeEach;

/**
 * Tests for the Simulator Class.
 *
 * @author CS159 Instructors
 * @version f25
 *
 */

public class SimulatorTest {

    private Product candy;
    private Product chips;
    private ProductType[] types;
    private VendingMachine vm1;
    private VendingMachine vm2;
    ArrayList<VendingMachine> list;
    private Simulator simulator;

    @BeforeEach
    public void resetProfits() {
        VendingMachine.resetTotalProfit();
    }

    @BeforeEach
    public void setUp() throws Exception {

        candy = new Product(ProductType.CANDY, "M&Ms");
        chips = new Product(ProductType.CHIPS, "Lays");

        types = new ProductType[] {ProductType.CANDY, ProductType.CHIPS, ProductType.CHIPS};

        vm1 = new VendingMachine(types);
        vm1.load(0, 7, candy);

        list = new ArrayList<>();
        list.add(vm1);

        simulator = new Simulator(list);
    }

    @Test
    public void testSimulateAddVM() {

        vm2 = new VendingMachine(types);
        vm2.load(1, 10, chips);

        simulator.addVM(vm2);

        // 7 (candies) * 0.40 (cost) = 2.8
        // 10 (chips) * 0.70 (cost) = 7
        // total costs =  9.8 (profit thus is -9.8)
        // sold = 1.25 (candy price) + 2.00 (chips price) = 3.25
        // total profit = -9.8 + 3.25
        assertEquals(-6.55, simulator.simulate(1), 0.0001);
    }

    @Test
    public void testSimulateOneItemOneSlotLoaded() {

        double profit = simulator.simulate(1);

        // vm1 has 7 candies loaded in slot 1 (index 0)
        // 7 (candies loaded) * 0.40 (cost of each)
        double totalProfitBeforeSimulation = -2.8;
        // added price of one candy that was sold to profit: 1.25
        double totalProfitAfterSimulation = totalProfitBeforeSimulation + 1.25;

        assertEquals(totalProfitAfterSimulation, profit, 0.0001);
    }

}
