# -*- coding: utf-8 -*-
# Copyright (C) 2014-2016 Andrey Antukh <niwi@niwi.nz>
# Copyright (C) 2014-2016 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014-2016 David Barragán <bameda@dbarragan.com>
# Copyright (C) 2014-2016 Alejandro Alonso <alejandro.alonso@kaleidos.net>
# Copyright (C) 2014-2016 Anler Hernández <hello@anler.me>
# 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 unittest import mock
from collections import OrderedDict

from django.core.urlresolvers import reverse

from taiga.base.utils import json

from .. import factories as f

import pytest
pytestmark = pytest.mark.django_db


def test_api_task_add_new_tags_with_error(client):
    project = f.ProjectFactory.create()
    task = f.create_task(project=project, status__project=project, milestone=None, user_story=None)
    f.MembershipFactory.create(project=project, user=task.owner, is_admin=True)
    url = reverse("tasks-detail", kwargs={"pk": task.pk})
    data = {
        "tags": [],
        "version": task.version
    }

    client.login(task.owner)

    data["tags"] = [1]
    response = client.json.patch(url, json.dumps(data))
    assert response.status_code == 400, response.data
    assert "tags" in response.data

    data["tags"] = [["back"]]
    response = client.json.patch(url, json.dumps(data))
    assert response.status_code == 400, response.data
    assert "tags" in response.data

    data["tags"] = [["back", "#cccc"]]
    response = client.json.patch(url, json.dumps(data))
    assert response.status_code == 400, response.data
    assert "tags" in response.data

    data["tags"] = [[1, "#ccc"]]
    response = client.json.patch(url, json.dumps(data))
    assert response.status_code == 400, response.data
    assert "tags" in response.data


def test_api_task_add_new_tags_without_colors(client):
    project = f.ProjectFactory.create()
    task = f.create_task(project=project, status__project=project, milestone=None, user_story=None)
    f.MembershipFactory.create(project=project, user=task.owner, is_admin=True)
    url = reverse("tasks-detail", kwargs={"pk": task.pk})
    data = {
        "tags": [
            ["back", None],
            ["front", None],
            ["ux", None]
        ],
        "version": task.version
    }

    client.login(task.owner)

    response = client.json.patch(url, json.dumps(data))

    assert response.status_code == 200, response.data

    tags_colors = OrderedDict(project.tags_colors)
    assert not tags_colors.keys()

    project.refresh_from_db()

    tags_colors = OrderedDict(project.tags_colors)
    assert "back" in tags_colors and "front" in tags_colors and "ux" in tags_colors


def test_api_task_add_new_tags_with_colors(client):
    project = f.ProjectFactory.create()
    task = f.create_task(project=project, status__project=project, milestone=None, user_story=None)
    f.MembershipFactory.create(project=project, user=task.owner, is_admin=True)
    url = reverse("tasks-detail", kwargs={"pk": task.pk})
    data = {
        "tags": [
            ["back", "#fff8e7"],
            ["front", None],
            ["ux", "#fabada"]
        ],
        "version": task.version
    }

    client.login(task.owner)

    response = client.json.patch(url, json.dumps(data))
    assert response.status_code == 200, response.data

    tags_colors = OrderedDict(project.tags_colors)
    assert not tags_colors.keys()

    project.refresh_from_db()

    tags_colors = OrderedDict(project.tags_colors)
    assert "back" in tags_colors and "front" in tags_colors and "ux" in tags_colors
    assert tags_colors["back"] == "#fff8e7"
    assert tags_colors["ux"] == "#fabada"


def test_api_create_new_task_with_tags(client):
    project = f.ProjectFactory.create(tags_colors=[["front", "#aaaaaa"], ["ux", "#fabada"]])
    status = f.TaskStatusFactory.create(project=project)
    project.default_task_status = status
    project.save()
    f.MembershipFactory.create(project=project, user=project.owner, is_admin=True)
    url = reverse("tasks-list")

    data = {
        "subject": "Test user story",
        "project": project.id,
        "tags": [
            ["back", "#fff8e7"],
            ["front", "#bbbbbb"],
            ["ux", None]
        ]
    }

    client.login(project.owner)

    response = client.json.post(url, json.dumps(data))
    assert response.status_code == 201, response.data

    task_tags_colors = OrderedDict(response.data["tags"])

    assert task_tags_colors["back"] == "#fff8e7"
    assert task_tags_colors["front"] == "#aaaaaa"
    assert task_tags_colors["ux"] == "#fabada"

    tags_colors = OrderedDict(project.tags_colors)

    project.refresh_from_db()

    tags_colors = OrderedDict(project.tags_colors)
    assert tags_colors["back"] == "#fff8e7"
    assert tags_colors["ux"] == "#fabada"
    assert tags_colors["front"] == "#aaaaaa"

[((1500, 1547), 'django.core.urlresolvers.reverse', 'reverse', (['"""tasks-detail"""'], {'kwargs': "{'pk': task.pk}"}), "('tasks-detail', kwargs={'pk': task.pk})\n", (1507, 1547), False, 'from django.core.urlresolvers import reverse\n'), ((2645, 2692), 'django.core.urlresolvers.reverse', 'reverse', (['"""tasks-detail"""'], {'kwargs': "{'pk': task.pk}"}), "('tasks-detail', kwargs={'pk': task.pk})\n", (2652, 2692), False, 'from django.core.urlresolvers import reverse\n'), ((3016, 3048), 'collections.OrderedDict', 'OrderedDict', (['project.tags_colors'], {}), '(project.tags_colors)\n', (3027, 3048), False, 'from collections import OrderedDict\n'), ((3133, 3165), 'collections.OrderedDict', 'OrderedDict', (['project.tags_colors'], {}), '(project.tags_colors)\n', (3144, 3165), False, 'from collections import OrderedDict\n'), ((3534, 3581), 'django.core.urlresolvers.reverse', 'reverse', (['"""tasks-detail"""'], {'kwargs': "{'pk': task.pk}"}), "('tasks-detail', kwargs={'pk': task.pk})\n", (3541, 3581), False, 'from django.core.urlresolvers import reverse\n'), ((3914, 3946), 'collections.OrderedDict', 'OrderedDict', (['project.tags_colors'], {}), '(project.tags_colors)\n', (3925, 3946), False, 'from collections import OrderedDict\n'), ((4031, 4063), 'collections.OrderedDict', 'OrderedDict', (['project.tags_colors'], {}), '(project.tags_colors)\n', (4042, 4063), False, 'from collections import OrderedDict\n'), ((4587, 4608), 'django.core.urlresolvers.reverse', 'reverse', (['"""tasks-list"""'], {}), "('tasks-list')\n", (4594, 4608), False, 'from django.core.urlresolvers import reverse\n'), ((4985, 5019), 'collections.OrderedDict', 'OrderedDict', (["response.data['tags']"], {}), "(response.data['tags'])\n", (4996, 5019), False, 'from collections import OrderedDict\n'), ((5186, 5218), 'collections.OrderedDict', 'OrderedDict', (['project.tags_colors'], {}), '(project.tags_colors)\n', (5197, 5218), False, 'from collections import OrderedDict\n'), ((5269, 5301), 'collections.OrderedDict', 'OrderedDict', (['project.tags_colors'], {}), '(project.tags_colors)\n', (5280, 5301), False, 'from collections import OrderedDict\n'), ((1711, 1727), 'taiga.base.utils.json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (1721, 1727), False, 'from taiga.base.utils import json\n'), ((1887, 1903), 'taiga.base.utils.json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (1897, 1903), False, 'from taiga.base.utils import json\n'), ((2072, 2088), 'taiga.base.utils.json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (2082, 2088), False, 'from taiga.base.utils import json\n'), ((2251, 2267), 'taiga.base.utils.json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (2261, 2267), False, 'from taiga.base.utils import json\n'), ((2924, 2940), 'taiga.base.utils.json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (2934, 2940), False, 'from taiga.base.utils import json\n'), ((3823, 3839), 'taiga.base.utils.json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (3833, 3839), False, 'from taiga.base.utils import json\n'), ((4889, 4905), 'taiga.base.utils.json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (4899, 4905), False, 'from taiga.base.utils import json\n')]