# -*- coding: utf-8 -*-
from datetime import datetime
from base import MockVim, MockCache
import sys

from taskwiki.constants import DEFAULT_SORT_ORDER, DEFAULT_VIEWPORT_VIRTUAL_TAGS


class TestParsingVimwikiTask(object):
    def setup(self):
        self.mockvim = MockVim()
        self.cache = MockCache()

        # Setup fake entries for custom TaskWarrior instance and Sort order
        self.cache.warriors.update({'T': 'extra'})
        self.mockvim.vars.update({'taskwiki_sort_orders': dict(T='extra')})

        sys.modules['vim'] = self.mockvim
        from taskwiki.viewport import ViewPort
        self.ViewPort = ViewPort

    def teardown(self):
        self.mockvim.reset()
        self.cache.reset()

    def test_simple(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home"]
        assert port.name == "Test"
        assert port.sort == DEFAULT_SORT_ORDER
        assert port.tw == 'default'

    def test_defaults(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home | +home =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home"]
        assert port.name == "Test"
        assert port.defaults == {'tags':['home']}
        assert port.sort == DEFAULT_SORT_ORDER
        assert port.tw == 'default'

    def test_different_tw(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home #T =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home"]
        assert port.name == "Test"
        assert port.sort == DEFAULT_SORT_ORDER
        assert port.tw == 'extra'

    def test_different_sort(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home $T =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home"]
        assert port.name == "Test"
        assert port.sort == 'extra'
        assert port.tw == 'default'

    def test_different_sort_with_complex_filter(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home or project:Work $T =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home", "or", "project:Work"]
        assert port.name == "Test"
        assert port.sort == 'extra'
        assert port.tw == 'default'

    def test_different_sort_tw(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home #T $T =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home"]
        assert port.name == "Test"
        assert port.sort == 'extra'
        assert port.tw == 'extra'

    def test_defaults_different_tw(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home | +home #T =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home"]
        assert port.name == "Test"
        assert port.defaults == {'tags':['home']}
        assert port.sort == DEFAULT_SORT_ORDER
        assert port.tw == 'extra'

    def test_defaults_different_tw_sort(self):
        self.mockvim.current.buffer[0] = "== Test | project:Home | +home #T $T =="
        port = self.ViewPort.from_line(0, self.cache)

        assert port.taskfilter == list(DEFAULT_VIEWPORT_VIRTUAL_TAGS) + ["project:Home"]
        assert port.name == "Test"
        assert port.defaults == {'tags':['home']}
        assert port.sort == 'extra'
        assert port.tw == 'extra'

@@ -3837,28 +3837,1325 @@
   assert port.tw == 'extra'%0A
+%0A    def test_override_default_virtual_tags_neutral(self):%0A        self.mockvim.current.buffer%5B0%5D = %22== Test %7C project:Home !?DELETED ==%22%0A        port = self.ViewPort.from_line(0, self.cache)%0A%0A        assert port.taskfilter == %5B%22-PARENT%22, %22project:Home%22%5D%0A        assert port.name == %22Test%22%0A        assert port.defaults == %7B'project':'Home'%7D%0A        assert port.sort == DEFAULT_SORT_ORDER%0A        assert port.tw == 'default'%0A%0A    def test_override_default_virtual_tags_positive(self):%0A        self.mockvim.current.buffer%5B0%5D = %22== Test %7C project:Home !+DELETED ==%22%0A        port = self.ViewPort.from_line(0, self.cache)%0A%0A        assert port.taskfilter == %5B%22+DELETED%22, %22-PARENT%22, %22project:Home%22%5D%0A        assert port.name == %22Test%22%0A        assert port.defaults == %7B'project':'Home'%7D%0A        assert port.sort == DEFAULT_SORT_ORDER%0A        assert port.tw == 'default'%0A%0A    def test_override_default_virtual_tags_negative(self):%0A        self.mockvim.current.buffer%5B0%5D = %22== Test %7C project:Home !-DELETED ==%22%0A        port = self.ViewPort.from_line(0, self.cache)%0A%0A        assert port.taskfilter == %5B%22-DELETED%22, %22-PARENT%22,%22project:Home%22%5D%0A        assert port.name == %22Test%22%0A        assert port.defaults == %7B'project':'Home'%7D%0A        assert port.sort == DEFAULT_SORT_ORDER%0A        assert port.tw == 'default'%0A

Add tests for forcing virtual tags