#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2013-2014,  Adrian Matejov

import sys
import pynotify
import os

TASKS_PATH = os.path.join(os.path.dirname(__file__), "tasks.txt")


class Task:
    def __init__(self, text, ID, priority):
        self.text = text
        self.ID = ID
        self.priority = priority


class Pytodo:
    def __init__(self, args):
        self.args = args
        self.f = None

        if not os.path.exists(os.path.dirname(TASKS_PATH)):
            os.mkdir(os.path.dirname(TASKS_PATH))

        if not os.path.isfile(TASKS_PATH):
            f = open(TASKS_PATH, 'w')
            f.close()

    def add_task(self, words):
        """Adds a task to the .txt file"""
        task = ''
        for i in words:
            task += i + ' '

        self._open_file('a')
        self._write_file(task + '\n')
        self._close_file()

    def rm_task(self, numbers):
        """Removes a task from .txt file"""
        self._open_file('r')
        lines = self.f.readlines()
        self._close_file()

        for i in numbers:
            try:
                if lines[int(i) - 1]:
                    lines[int(i) - 1] = ''
            except IndexError:
                print "Could not remove task number %d. Task does not exist!" % int(i)
                return

        tasks = ''
        for task in lines:
            tasks += task

        self._open_file('w')
        self._write_file(tasks)
        self._close_file()

    def edit_task(self, number, task):
        """Edits current task"""
        self._open_file('r')
        lines = self.f.readlines()
        self._close_file()

        try:
            if lines[int(number) - 1]:
                lines[int(number) - 1] = ''
                for i in task:
                    lines[int(number) - 1] += i + ' '
                lines[int(number) - 1] += '\n'
        except IndexError:
            print "Could not edit task number %d. Task does not exist!" % int(number)
            return
        
        tasks = ''
        for t in lines:
            tasks += t

        self._open_file('w')
        self._write_file(tasks)
        self._close_file()
        
    def show_notification(self):
        """Shows notification window"""
        self._open_file('r+')
        tasks = self.f.read()
        self._close_file()

        tasks = tasks.split('\n')
        tasks.pop()

        tasklist = ''
        for i, j in enumerate(tasks):
            if i + 1 == len(tasks):
                tasklist += '%d. %s' % (i+1, j)
            else:
                tasklist += '%d. %s\n' % (i+1, j)

        n = pynotify.Notification ("ToDo Notes", tasklist)

        n.show()

    def print_help(self):
        """Prints available commands on the screen"""
        print "ToDo Notes, (c) 2013 Adman"
        print "Available arguments:"
        print "     help - show available commands"
        print "     add <task> - add a task to the todo list"
        print "     rm/remove/done <number> - remove a task from the todo list"


    def process(self):
        cmd = self.args[0].lower() if len(self.args) else 0

        if len(self.args):
            if cmd == 'help':
                self.print_help()
                sys.exit(1)
            elif cmd == 'add':
                self.add_task(self.args[1:])
            elif cmd == 'rm' or cmd == 'remove' or cmd == 'done':
                self.rm_task(self.args[1:])
            elif cmd == 'edit':
                self.edit_task(self.args[1], self.args[2:])
            else:
                self.print_help()
                sys.exit(1)

        self.show_notification()

    def _open_file(self, mode="r"):
        self.f = open(TASKS_PATH, mode)

    def _write_file(self, text):
        self.f.write(text)

    def _close_file(self):
        self.f.close()

      
if __name__ == '__main__':
    pynotify.init("summary-only")
    args = sys.argv[1:]

    todo = Pytodo(args)
    todo.process()

@@ -116,16 +116,26 @@
 mport os
+%0Aimport re
 %0A%0ATASKS_
@@ -204,24 +204,154 @@
 class Task:%0A
+    %22%22%22%0A        @param string text%0A        @param int ID%0A        @param string priority (low - L/normal - N/critical - C)%0A    %22%22%22%0A
     def __in
@@ -555,16 +555,57 @@
 = None%0A%0A
+        self.tasks = self._read_tasks()%0A%0A
         
@@ -784,24 +784,24 @@
 _PATH, 'w')%0A
-
             
@@ -807,24 +807,680 @@
  f.close()%0A%0A
+    def _read_tasks(self):%0A        %22%22%22Reads tasks from .txt file and parse them%22%22%22%0A        self._open_file('r')%0A        lines = self.f.readlines()%0A        self._close_file()%0A%0A        for i in range(len(lines)):%0A            lines%5Bi%5D = lines%5Bi%5D.replace(%22 %5Cn%22, %22%22)%0A%0A        tasks = %5B%5D%0A        for i, line in enumerate(lines):%0A            if %22@priorityL@%22 in line:%0A                priority = %22L%22%0A            elif %22@priorityC@%22 in line:%0A                priority = %22C%22%0A            else:%0A                priority = %22N%22%0A%0A            ID = i + 1%0A            text = re.sub(%22@priority%5BLCN%5D@%22, %22%22, line)%0A            tasks.append(Task(text, ID, priority))%0A            %0A%0A
     def add_

parsing task