# Copyright (C) 2020 OpenMotics BV
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import
import logging
from datetime import datetime, timedelta

import pytest
from pytz import timezone

logger = logging.getLogger(__name__)


@pytest.mark.smoke
def test_health_check(toolbox):
    data = toolbox.dut.get('/health_check')
    assert 'health' in data
    assert data['health']['vpn_service']['state']
    assert data['health']['openmotics']['state']


@pytest.mark.smoke
def test_features(toolbox):
    data = toolbox.dut.get('/get_features')
    assert 'features' in data
    assert 'input_states' in data['features']


@pytest.mark.smoke
def test_version(toolbox):
    data = toolbox.dut.get('/get_version')
    assert 'version' in data
    assert 'gateway' in data


@pytest.fixture
def set_timezone(request, toolbox):
    toolbox.dut.get('/set_timezone', params={'timezone': 'UTC'})
    yield
    toolbox.dut.get('/set_timezone', params={'timezone': 'UTC'})


@pytest.mark.smoke
def test_status_timezone(toolbox, set_timezone):
    data = toolbox.dut.get('/get_timezone')
    assert 'timezone' in data
    assert data['timezone'] == 'UTC'

    now = datetime.strptime(datetime.utcnow().strftime('%H:%M'), '%H:%M')
    data = toolbox.dut.get('/get_status')
    assert 'time' in data
    time = datetime.strptime(data['time'], '%H:%M')
    assert now - timedelta(minutes=1) <= time <= now + timedelta(minutes=1)


@pytest.mark.smoke
def test_timezone_change(toolbox, set_timezone):
    toolbox.dut.get('/set_timezone', params={'timezone': 'America/Bahia'})

    data = toolbox.dut.get('/get_timezone')
    assert 'timezone' in data
    assert data['timezone'] == 'America/Bahia'

    bahia_timezone = timezone('America/Bahia')
    now = datetime.strptime(datetime.now(bahia_timezone).strftime('%H:%M'), '%H:%M')
    data = toolbox.dut.get('/get_status')
    assert 'time' in data
    time = datetime.strptime(data['time'], '%H:%M')
    assert now - timedelta(minutes=1) <= time <= now + timedelta(minutes=1)

@@ -1415,15 +1415,17 @@
 est.
-fixture
+mark.skip
 %0Adef
@@ -1605,36 +1605,35 @@
 %0A%0A@pytest.mark.s
-moke
+kip
 %0Adef test_status
@@ -2056,36 +2056,35 @@
 %0A%0A@pytest.mark.s
-moke
+kip
 %0Adef test_timezo

disable timezone tests