import unittest

from mpf.core.machine import MachineController
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
import time


class TestBallController(MpfTestCase):
    def setUp(self):
        super().setUp()
        self.machine.ball_controller.num_balls_known = 0

    def getConfigFile(self):
        return 'config.yaml'

    def getMachinePath(self):
        return 'tests/machine_files/ball_controller/'

    def get_platform(self):
        return 'smart_virtual'

    def test_game_start(self):
        # min balls is set to 3
        self.assertEqual(None, self.machine.game)
        self.advance_time_and_run(1)
        self.assertEqual(None, self.machine.game)
        self.assertEqual(0, self.machine.ball_controller.num_balls_known)

        self.machine.switch_controller.process_switch("s_ball_switch1", 1)
        self.machine.switch_controller.process_switch("s_ball_switch2", 1)
        self.advance_time_and_run(1)
        self.assertEqual(2, self.machine.ball_controller.num_balls_known)

        # not enough balls
        self.assertEqual(None, self.machine.game)
        self.machine.switch_controller.process_switch("s_start", 1)
        self.machine.switch_controller.process_switch("s_start", 0)
        self.advance_time_and_run(1)
        self.assertEqual(None, self.machine.game)

        self.machine.switch_controller.process_switch("s_ball_switch1", 0)
        self.machine.switch_controller.process_switch("s_ball_switch3", 1)
        self.machine.switch_controller.process_switch("s_ball_switch4", 1)
        self.advance_time_and_run(1)
        self.assertEqual(3, self.machine.ball_controller.num_balls_known)

        self.machine.switch_controller.process_switch("s_ball_switch_launcher",
                                                      1)
        self.advance_time_and_run(1)
        self.assertEqual(4, self.machine.ball_controller.num_balls_known)

        # not all balls are home
        self.assertEqual(None, self.machine.game)
        self.machine.switch_controller.process_switch("s_start", 1)
        self.machine.switch_controller.process_switch("s_start", 0)
        self.advance_time_and_run(1)
        self.assertEqual(None, self.machine.game)

        self.machine.switch_controller.process_switch("s_ball_switch1", 1)
        self.advance_time_and_run(1)
        self.assertEqual(4, self.machine.ball_controller.num_balls_known)

        self.assertEqual(None, self.machine.game)
        self.machine.switch_controller.process_switch("s_start", 1)
        self.machine.switch_controller.process_switch("s_start", 0)
        self.advance_time_and_run(1)
        self.assertNotEqual(None, self.machine.game)

@@ -1,68 +1,4 @@
-import unittest%0A%0Afrom mpf.core.machine import MachineController%0A
 from
@@ -70,20 +70,8 @@
 ock%0A
-import time%0A
 %0A%0Acl
@@ -2588,28 +2588,1554 @@
 al(None, self.machine.game)%0A
+%0A    def test_ball_collect(self):%0A        self.machine.switch_controller.process_switch(%22s_ball_switch1%22, 1)%0A        self.machine.switch_controller.process_switch(%22s_ball_switch3%22, 1)%0A        self.machine.switch_controller.process_switch(%22s_ball_switch4%22, 1)%0A        self.advance_time_and_run(1)%0A        self.assertEqual(3, self.machine.ball_controller.num_balls_known)%0A%0A        # prevent smart_virtual from %22ejecting%22 the ball%0A        self.machine.coils.eject_coil2.pulse = MagicMock()%0A%0A        self.machine.switch_controller.process_switch(%22s_ball_switch_launcher%22,%0A                                                      1)%0A        self.advance_time_and_run(.5)%0A        self.assertEqual(4, self.machine.ball_controller.num_balls_known)%0A%0A        self.assertEqual(%22idle%22, self.machine.ball_devices.test_trough._state)%0A        self.assertEqual(%22ejecting%22, self.machine.ball_devices.test_launcher._state)%0A%0A        # not all balls are home. it should trigger a collect%0A        self.machine.switch_controller.process_switch(%22s_start%22, 1)%0A        self.machine.switch_controller.process_switch(%22s_start%22, 0)%0A        self.advance_time_and_run(.1)%0A        self.assertEqual(None, self.machine.game)%0A%0A        # ball is still in launcher and the collect ball should not try to eject it again (which would request a ball%0A        # at the trough. so check that trough is idle%0A        self.assertEqual(%22idle%22, self.machine.ball_devices.test_trough._state)%0A        self.assertEqual(%22ejecting%22, self.machine.ball_devices.test_launcher._state)%0A

add test for BallController bug