import datetime
import os

import markdown2
import pytz
from varsnap import varsnap

from app.util import cached_function


# See https://github.com/trentm/python-markdown2/wiki/Extras
MARKDOWN_EXTRAS = [
    'code-friendly',
    'fenced-code-blocks',
    'smarty-pants',
    'tables',
]


class Note(object):
    def __init__(self):
        self.title = ''
        self.slug = ''
        self.time = ''
        self.note = ''
        self.markdown = ''

    @staticmethod
    def parse_note(lines, timezone):
        lines = [line.strip("\n") for line in lines]
        if len(lines) < 4 or not lines[4].isdigit():
            return None
        note_parsed = Note()
        note_parsed.title = lines[0]
        note_parsed.slug = lines[2]
        note_parsed.parse_time(lines[4], timezone)
        note_parsed.parse_markdown(lines[6:])
        note_parsed.markdown = '\n'.join(lines[6:])
        return note_parsed

    @staticmethod
    def get_note_file_data(note_file, timezone):
        with open(note_file) as note_handle:
            lines = note_handle.readlines()
        note_parsed = Note.parse_note(lines, timezone)
        return note_parsed

    @varsnap
    def parse_time(self, timestamp, timezone):
        timestamp = int(timestamp)
        self.time = datetime.datetime.fromtimestamp(timestamp, timezone)
        return self.time

    @varsnap
    def parse_markdown(self, note):
        self.note = markdown2.markdown(
            "\n".join(note),
            extras=MARKDOWN_EXTRAS,
        )
        return self.note


@varsnap
def prune_note_files(note_files):
    def is_valid_note(note_file):
        if '~' in note_file:
            return False
        if note_file[0] == '.':
            return False
        return True
    files = [note_file for note_file in note_files if is_valid_note(note_file)]
    return files


def get_note_files():
    current_directory = os.path.dirname(os.path.realpath(__file__))
    notes_directory = os.path.join(current_directory, 'notes')
    files = os.listdir(notes_directory)
    files.sort(reverse=True)
    files = prune_note_files(files)
    files = [os.path.join(notes_directory, note_file) for note_file in files]
    return files


# @varsnap
@cached_function
def get_notes():
    note_files = get_note_files()
    timezone = pytz.timezone(os.environ['DISPLAY_TIMEZONE'])
    notes = []
    for note_file in note_files:
        note_parsed = Note.get_note_file_data(note_file, timezone)
        if note_parsed:
            notes.append(note_parsed)
    return notes


@varsnap
@cached_function
def get_note_from_slug(slug):
    """ Given the slug of a note, reurn the note contents """
    notes = get_notes()
    for note in notes:
        if note.slug == slug:
            return note
    return None

@@ -327,16 +327,44 @@
 (self):%0A
+        self.note_file = ''%0A
         
@@ -371,32 +371,32 @@
 self.title = ''%0A
-
         self.slu
@@ -506,36 +506,137 @@
 def 
-parse_note(lines, timezone):
+get_note_file_data(note_file, timezone):%0A        with open(note_file) as note_handle:%0A            lines = note_handle.readlines()
 %0A   
@@ -791,16 +791,58 @@
  Note()%0A
+        note_parsed.note_file = note_file%0A
         
@@ -995,32 +995,32 @@
 down(lines%5B6:%5D)%0A
+
         note_par
@@ -1087,247 +1087,8 @@
 ed%0A%0A
-    @staticmethod%0A    def get_note_file_data(note_file, timezone):%0A        with open(note_file) as note_handle:%0A            lines = note_handle.readlines()%0A        note_parsed = Note.parse_note(lines, timezone)%0A        return note_parsed%0A%0A
     

Add note_file into Note object