# -*- 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"

[((37, 10, 37, 57), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import reverse\n'), ((70, 10, 70, 57), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import reverse\n'), ((86, 18, 86, 50), 'collections.OrderedDict', 'OrderedDict', ({(86, 30, 86, 49): 'project.tags_colors'}, {}), '(project.tags_colors)', False, 'from collections import OrderedDict\n'), ((91, 18, 91, 50), 'collections.OrderedDict', 'OrderedDict', ({(91, 30, 91, 49): 'project.tags_colors'}, {}), '(project.tags_colors)', False, 'from collections import OrderedDict\n'), ((99, 10, 99, 57), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import reverse\n'), ((114, 18, 114, 50), 'collections.OrderedDict', 'OrderedDict', ({(114, 30, 114, 49): 'project.tags_colors'}, {}), '(project.tags_colors)', False, 'from collections import OrderedDict\n'), ((119, 18, 119, 50), 'collections.OrderedDict', 'OrderedDict', ({(119, 30, 119, 49): 'project.tags_colors'}, {}), '(project.tags_colors)', False, 'from collections import OrderedDict\n'), ((131, 10, 131, 31), 'django.core.urlresolvers.reverse', 'reverse', ({(131, 18, 131, 30): '"""tasks-list"""'}, {}), "('tasks-list')", False, 'from django.core.urlresolvers import reverse\n'), ((148, 23, 148, 57), 'collections.OrderedDict', 'OrderedDict', ({(148, 35, 148, 56): "response.data['tags']"}, {}), "(response.data['tags'])", False, 'from collections import OrderedDict\n'), ((154, 18, 154, 50), 'collections.OrderedDict', 'OrderedDict', ({(154, 30, 154, 49): 'project.tags_colors'}, {}), '(project.tags_colors)', False, 'from collections import OrderedDict\n'), ((158, 18, 158, 50), 'collections.OrderedDict', 'OrderedDict', ({(158, 30, 158, 49): 'project.tags_colors'}, {}), '(project.tags_colors)', False, 'from collections import OrderedDict\n'), ((46, 38, 46, 54), 'taiga.base.utils.json.dumps', 'json.dumps', ({(46, 49, 46, 53): 'data'}, {}), '(data)', False, 'from taiga.base.utils import json\n'), ((51, 38, 51, 54), 'taiga.base.utils.json.dumps', 'json.dumps', ({(51, 49, 51, 53): 'data'}, {}), '(data)', False, 'from taiga.base.utils import json\n'), ((56, 38, 56, 54), 'taiga.base.utils.json.dumps', 'json.dumps', ({(56, 49, 56, 53): 'data'}, {}), '(data)', False, 'from taiga.base.utils import json\n'), ((61, 38, 61, 54), 'taiga.base.utils.json.dumps', 'json.dumps', ({(61, 49, 61, 53): 'data'}, {}), '(data)', False, 'from taiga.base.utils import json\n'), ((82, 38, 82, 54), 'taiga.base.utils.json.dumps', 'json.dumps', ({(82, 49, 82, 53): 'data'}, {}), '(data)', False, 'from taiga.base.utils import json\n'), ((111, 38, 111, 54), 'taiga.base.utils.json.dumps', 'json.dumps', ({(111, 49, 111, 53): 'data'}, {}), '(data)', False, 'from taiga.base.utils import json\n'), ((145, 37, 145, 53), 'taiga.base.utils.json.dumps', 'json.dumps', ({(145, 48, 145, 52): 'data'}, {}), '(data)', False, 'from taiga.base.utils import json\n')]