"""The tests for the image_processing component."""
from unittest.mock import patch, PropertyMock

from homeassistant.core import callback
from homeassistant.const import ATTR_ENTITY_PICTURE
from homeassistant.bootstrap import setup_component
from homeassistant.exceptions import HomeAssistantError
import homeassistant.components.http as http
import homeassistant.components.image_processing as ip

from tests.common import (
    get_test_home_assistant, get_test_instance_port, assert_setup_component)


class TestSetupImageProcessing(object):
    """Test class for setup image processing."""

    def setup_method(self):
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()

    def teardown_method(self):
        """Stop everything that was started."""
        self.hass.stop()

    def test_setup_component(self):
        """Setup demo platfrom on image_process component."""
        config = {
            ip.DOMAIN: {
                'platform': 'demo'
            }
        }

        with assert_setup_component(1, ip.DOMAIN):
            setup_component(self.hass, ip.DOMAIN, config)

    def test_setup_component_with_service(self):
        """Setup demo platfrom on image_process component test service."""
        config = {
            ip.DOMAIN: {
                'platform': 'demo'
            }
        }

        with assert_setup_component(1, ip.DOMAIN):
            setup_component(self.hass, ip.DOMAIN, config)

        assert self.hass.services.has_service(ip.DOMAIN, 'scan')


class TestImageProcessing(object):
    """Test class for image processing."""

    def setup_method(self):
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()

        setup_component(
            self.hass, http.DOMAIN,
            {http.DOMAIN: {http.CONF_SERVER_PORT: get_test_instance_port()}})

        config = {
            ip.DOMAIN: {
                'platform': 'demo'
            },
            'camera': {
                'platform': 'demo'
            },
        }

        with patch('homeassistant.components.image_processing.demo.'
                   'DemoImageProcessing.should_poll',
                   new_callable=PropertyMock(return_value=False)):
            setup_component(self.hass, ip.DOMAIN, config)

        state = self.hass.states.get('camera.demo_camera')
        self.url = "{0}{1}".format(
            self.hass.config.api.base_url,
            state.attributes.get(ATTR_ENTITY_PICTURE))

    def teardown_method(self):
        """Stop everything that was started."""
        self.hass.stop()

    @patch('homeassistant.components.camera.demo.DemoCamera.camera_image',
           autospec=True, return_value=b'Test')
    @patch('homeassistant.components.image_processing.demo.'
           'DemoImageProcessing.process_image', autospec=True)
    def test_get_image_from_camera(self, mock_process, mock_camera):
        """Grab a image from camera entity."""
        self.hass.start()

        ip.scan(self.hass, entity_id='image_processing.demo')
        self.hass.block_till_done()

        assert mock_camera.called
        assert mock_process.called

        assert mock_process.call_args[0][1] == b'Test'

    @patch('homeassistant.components.camera.async_get_image',
           side_effect=HomeAssistantError())
    @patch('homeassistant.components.image_processing.demo.'
           'DemoImageProcessing.process_image', autospec=True)
    def test_get_image_without_exists_camera(self, mock_process, mock_image):
        """Try to get image without exists camera."""
        self.hass.states.remove('camera.demo_camera')

        ip.scan(self.hass, entity_id='image_processing.demo')
        self.hass.block_till_done()

        assert mock_image.called
        assert not mock_process.called


class TestImageProcessingAlpr(object):
    """Test class for image processing."""

    def setup_method(self):
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()

        config = {
            ip.DOMAIN: {
                'platform': 'demo'
            },
            'camera': {
                'platform': 'demo'
            },
        }

        with patch('homeassistant.components.image_processing.demo.'
                   'DemoImageProcessingAlpr.should_poll',
                   new_callable=PropertyMock(return_value=False)):
            setup_component(self.hass, ip.DOMAIN, config)

        state = self.hass.states.get('camera.demo_camera')
        self.url = "{0}{1}".format(
            self.hass.config.api.base_url,
            state.attributes.get(ATTR_ENTITY_PICTURE))

        self.alpr_events = []

        @callback
        def mock_alpr_event(event):
            """Mock event."""
            self.alpr_events.append(event)

        self.hass.bus.listen('found_plate', mock_alpr_event)

    def teardown_method(self):
        """Stop everything that was started."""
        self.hass.stop()

    def test_alpr_event_single_call(self, aioclient_mock):
        """Setup and scan a picture and test plates from event."""
        aioclient_mock.get(self.url, content=b'image')

        ip.scan(self.hass, entity_id='image_processing.demo_alpr')
        self.hass.block_till_done()

        state = self.hass.states.get('image_processing.demo_alpr')

        assert len(self.alpr_events) == 4
        assert state.state == 'AC3829'

        event_data = [event.data for event in self.alpr_events if
                      event.data.get('plate') == 'AC3829']
        assert len(event_data) == 1
        assert event_data[0]['plate'] == 'AC3829'
        assert event_data[0]['confidence'] == 98.3
        assert event_data[0]['entity_id'] == 'image_processing.demo_alpr'

    def test_alpr_event_double_call(self, aioclient_mock):
        """Setup and scan a picture and test plates from event."""
        aioclient_mock.get(self.url, content=b'image')

        ip.scan(self.hass, entity_id='image_processing.demo_alpr')
        ip.scan(self.hass, entity_id='image_processing.demo_alpr')
        self.hass.block_till_done()

        state = self.hass.states.get('image_processing.demo_alpr')

        assert len(self.alpr_events) == 4
        assert state.state == 'AC3829'

        event_data = [event.data for event in self.alpr_events if
                      event.data.get('plate') == 'AC3829']
        assert len(event_data) == 1
        assert event_data[0]['plate'] == 'AC3829'
        assert event_data[0]['confidence'] == 98.3
        assert event_data[0]['entity_id'] == 'image_processing.demo_alpr'

    @patch('homeassistant.components.image_processing.demo.'
           'DemoImageProcessingAlpr.confidence',
           new_callable=PropertyMock(return_value=95))
    def test_alpr_event_single_call_confidence(self, confidence_mock,
                                               aioclient_mock):
        """Setup and scan a picture and test plates from event."""
        aioclient_mock.get(self.url, content=b'image')

        ip.scan(self.hass, entity_id='image_processing.demo_alpr')
        self.hass.block_till_done()

        state = self.hass.states.get('image_processing.demo_alpr')

        assert len(self.alpr_events) == 2
        assert state.state == 'AC3829'

        event_data = [event.data for event in self.alpr_events if
                      event.data.get('plate') == 'AC3829']
        assert len(event_data) == 1
        assert event_data[0]['plate'] == 'AC3829'
        assert event_data[0]['confidence'] == 98.3
        assert event_data[0]['entity_id'] == 'image_processing.demo_alpr'
