# -*- coding: utf-8 -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
import ulib.base.i_need_py23

__all__ = ('TasksrcFile',)

from os import path

from ulib.base.base import scriptdir, isseq
from ulib.base.uio import _u
from ulib.base.config import ShConfigFile
from ulib.base.paths import abspath2

class TasksrcFile(ShConfigFile):
    TASKSRC = "tasksrc"
    DEFAULT_TASKSFILE = "Tasks.txt"
    DEFAULT_PURGEDFILE = "Purged.txt"

    def __init__(self, file=None, raise_exception=True):
        if file is None:
            testdir = path.join(scriptdir, "../../test")
            utoolsrcdir = path.join(path.expanduser("~"), ".utools")
            if path.isdir(testdir): file = path.join(testdir, self.TASKSRC)
            else: file = path.join(utoolsrcdir, self.TASKSRC)
            raise_exception = False
        ShConfigFile.__init__(self, file=file, raise_exception=raise_exception)

    TASKSDIR = "tasksdir"
    PROJDIRS = "projdirs"
    STORES = "stores"
    TASKSFILE = "tasksfile"
    PURGEDFILE = "purgedfile"
    URGENT_COLOR = "urgent_color"
    NORMAL_COLOR = "normal_color"
    PLANNED_COLOR = "planned_color"
    OBSOLETE_COLOR = "obsolete_color"
    DONE_COLOR = "done_color"
    EVENT_COLOR = "event_color"
    IDEA_COLOR = "idea_color"
    ACTIVITY_COLOR = "activity_color"
    PROJECT_COLOR = "project_color"
    CONTEXT_COLOR = "context_color"
    TAG_COLOR = "tag_color"

    def __p(self, p, refdir=None):
        if refdir is not None:
            if path.isfile(refdir): refdir = path.split(refdir)[0]
        return abspath2(path.expanduser(p), refdir)
    def __ptd(self, p):
        return self.__p(p, self.get_tasksdir())

    def __get(self, name, default, list=False, strip=False, fix_path=True):
        if fix_path is True: fix_path = self.__ptd
        if self.has_key(name):
            if list:
                values = self.get_list(name, strip)
                if fix_path: values = [fix_path(value) for value in values]
                value = values
            else:
                value = self[name]
                if fix_path: value = fix_path(value)
        else:
            value = default
        return value

    def __join(self, values):
        if isseq(values): values = u"\n".join(map(lambda v: _u(v), values))
        else: values = _u(values)
        return values

    def __set(self, name, value, list=False):
        if value is None:
            if self.has_key(name): del self[name]
        else:
            self[name] = self.__join(value)

    def get_tasksdir(self):
        """Retourner le répertoire de base pour les fichiers de tâche
        Par défaut, il s'agit de ~/.utools
        """
        return self.__get(self.TASKSDIR, self.absfiledir, list=True, strip=True, fix_path=self.__p)
    def set_tasksdir(self, tasksdir=None):
        self.__set(self.TASKSDIR, tasksdir, list=True)

    def get_projdirs(self):
        """Retourner la liste des répertoires de base pour les projets.
        """
        return self.__get(self.PROJDIRS, (), list=True, strip=True)
    def set_projdirs(self, projdirs=None):
        self.__set(self.PROJDIRS, projdirs, list=True)

    def get_stores(self):
        """Retourner la liste des stores à considérer pour la lecture des tâches
        """
        return self.__get(self.STORES, (), list=True, strip=True)
    def set_stores(self, stores=None):
        self.__set(self.STORES, stores, list=True)

    def get_default_tasksfile(self):
        return self.__ptd(self.DEFAULT_TASKSFILE)

    def get_tasksfile(self, default=True):
        """Retourner la valeur tasksfile si elle est définie, default sinon.
        Il s'agit du fichier qui contient les tâches par défaut.
        Si default==True, retourner la première valeur de get_stores(), sinon
        la valeur par défaut ~/.utools/Tasks.txt
        """
        if default is True:
            stores = self.get_stores()
            if stores: default = stores[0]
            else: default = self.get_default_tasksfile()
        return self.__get(self.TASKSFILE, default)
    def set_tasksfile(self, tasksfile):
        self.set_stores(tasksfile)
        self.__set(self.TASKSFILE, tasksfile)

    def get_tasksfiles(self):
        """Retourner la valeur de get_stores() si elle définie, sinon la valeur
        [get_tasksfile()]
        """
        stores = self.get_stores()
        if not stores: stores = [self.get_tasksfile()]
        return stores

    def get_default_purgedfile(self):
        return self.__ptd(self.DEFAULT_PURGEDFILE)

    def get_purgedfile(self, default=True):
        """Retourner la valeur purgedfile si elle est définie, default sinon.
        Il s'agit du fichier qui contient les tâches par défaut.
        Si default==True, retourner la valeur par défaut ~/.utools/Purged.txt
        """
        if default is True: default = self.get_default_purgedfile()
        return self.__get(self.TASKSFILE, default)
    def set_purgedfile(self, purgedfile):
        self.__set(self.PURGEDFILE, purgedfile)

    def get_urgent_color(self):
        return self.get(self.URGENT_COLOR, "red")
    def set_urgent_color(self, urgent_color):
        self[self.URGENT_COLOR] = urgent_color

    def get_normal_color(self):
        return self.get(self.NORMAL_COLOR, "") or None
    def set_normal_color(self, normal_color):
        self[self.NORMAL_COLOR] = normal_color

    def get_planned_color(self):
        return self.get(self.PLANNED_COLOR, "") or None
    def set_planned_color(self, planned_color):
        self[self.PLANNED_COLOR] = planned_color

    def get_obsolete_color(self):
        return self.get(self.OBSOLETE_COLOR, "faint") or None
    def set_obsolete_color(self, obsolete_color):
        self[self.OBSOLETE_COLOR] = obsolete_color

    def get_done_color(self):
        return self.get(self.DONE_COLOR, "faint")
    def set_done_color(self, done_color):
        self[self.DONE_COLOR] = done_color

    def get_event_color(self):
        return self.get(self.EVENT_COLOR, "yellow") or None
    def set_event_color(self, event_color):
        self[self.EVENT_COLOR] = event_color

    def get_idea_color(self):
        return self.get(self.IDEA_COLOR, "yellow")
    def set_idea_color(self, idea_color):
        self[self.IDEA_COLOR] = idea_color

    def get_activity_color(self):
        return self.get(self.IDEA_COLOR, "green")
    def set_activity_color(self, activity_color):
        self[self.IDEA_COLOR] = activity_color

    def get_project_color(self):
        return self.get(self.PROJECT_COLOR, "blue")
    def set_project_color(self, project_color):
        self[self.PROJECT_COLOR] = project_color

    def get_context_color(self):
        return self.get(self.CONTEXT_COLOR, "blue")
    def set_context_color(self, context_color):
        self[self.CONTEXT_COLOR] = context_color

    def get_tag_color(self):
        return self.get(self.TAG_COLOR, "faint,blue")
    def set_tag_color(self, tag_color):
        self[self.TAG_COLOR] = tag_color
