# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2016 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Tests for widgets.
"""

from django.core.urlresolvers import reverse

from weblate.trans.models import Translation
from weblate.trans.tests.test_views import ViewTestCase
from weblate.trans.views.widgets import WIDGETS


class WidgetsTest(ViewTestCase):
    '''
    Testing of widgets.
    '''
    def test_view_widgets_root(self):
        response = self.client.get(
            reverse('widgets_root')
        )
        self.assertContains(response, 'Test')

    def test_view_widgets(self):
        response = self.client.get(
            reverse('widgets', kwargs=self.kw_project)
        )
        self.assertContains(response, 'Test')

    def test_view_widgets_lang(self):
        response = self.client.get(
            reverse('widgets', kwargs=self.kw_project),
            {'lang': 'cs'}
        )
        self.assertContains(response, 'Test')

    def test_view_engage(self):
        response = self.client.get(
            reverse('engage', kwargs=self.kw_project)
        )
        self.assertContains(response, 'Test')

    def test_view_engage_lang(self):
        response = self.client.get(
            reverse('engage-lang', kwargs=self.kw_lang_project)
        )
        self.assertContains(response, 'Test')

    def assert_widget(self, widget, response):
        if hasattr(WIDGETS[widget], 'redirect'):
            self.assertEqual(response.status_code, 302)
        elif 'svg' in WIDGETS[widget].content_type:
            self.assertSVG(response)
        else:
            self.assertPNG(response)

    def test_view_widget_percents(self):
        for translated in (0, 3, 4):
            Translation.objects.update(translated=translated)
            for widget in WIDGETS:
                color = WIDGETS[widget].colors[0]
                response = self.client.get(
                    reverse(
                        'widget-image',
                        kwargs={
                            'project': self.project.slug,
                            'widget': widget,
                            'color': color,
                            'extension': 'png',
                        }
                    )
                )

                self.assert_widget(widget, response)

    def test_view_widget_image(self):
        for widget in WIDGETS:
            for color in WIDGETS[widget].colors:
                response = self.client.get(
                    reverse(
                        'widget-image',
                        kwargs={
                            'project': self.project.slug,
                            'widget': widget,
                            'color': color,
                            'extension': 'png',
                        }
                    )
                )

                self.assert_widget(widget, response)

    def test_view_widget_image_lang(self):
        for widget in WIDGETS:
            for color in WIDGETS[widget].colors:
                response = self.client.get(
                    reverse(
                        'widget-image-lang',
                        kwargs={
                            'project': self.project.slug,
                            'widget': widget,
                            'color': color,
                            'lang': 'cs',
                            'extension': 'png',
                        }
                    )
                )

                self.assert_widget(widget, response)
