import os
import datetime
import xml
from xml.parsers.expat import *
from xml.dom.minidom import parse, parseString
from dynastie.generators.generator import DynastieGenerator
from django.db import models

class Index(DynastieGenerator):

    cur_page = 0
    nb_pages = 0
    cur_post = 0
    posts_per_page = 0
    filename = 'index'
    dirname = ''

    def createNavigation(self, posts, dom, root, node):
        if self.nb_pages == 0 or self.nb_pages == 1:
            return ''
        
        if self.dirname != '':
            if self.dirname.startswith('/'):
                href = '<a href="' + self.dirname + '/' + self.filename
            else:
                href = '<a href="/' + self.dirname + '/' + self.filename
        else:
            href = '<a href="/' + self.filename

        nav = ''
        if self.cur_page != 0:
            nav = nav + href + '.html">&lt;&lt; First</a>  '
            if self.cur_page == 1:
                nav = nav + href + '.html">&lt; Prev</a> '
            else:
                nav = nav + href + str(self.cur_page-1) + '.html">&lt; Prev</a> '

        start = self.cur_page-5
        if start < 0:
            start = 0
        end = start + 10
        
        if end > self.nb_pages:
            end = self.nb_pages

        for i in range(start, end):
            if i == self.cur_page:
                nav = nav + str(i+1) + ' '
            else:
                if i == 0:
                    nav = nav + href + '.html">1</a> '
                else:
                    nav = nav + href + str(i) + '.html">' + str(i+1) + '</a> '

        if self.cur_page < self.nb_pages-1:
            nav = nav + href + str(self.cur_page+1) + '.html">Next &gt;</a>  '
            nav = nav + href + str(self.nb_pages-1) + '.html">Last &gt;&gt;</a>'

        new_dom = parseString('<div class="navigation">' + nav + '</div>')
        new_node = new_dom.getElementsByTagName('div')[0]
        root.replaceChild(new_node.cloneNode(True), node)

    def createPost(self, post, dom, post_elem, root):
        values = {}
        values['title'] = self.createLinkElem(dom, post.getPath(), post.title)
        values['author'] = post.author.first_name + ' ' + post.author.last_name
        values['date'] = post.creation_date.strftime("%A, %d %B %Y %H:%m")
        values['post_content'] = ''

        blog = post.blog
        blog.create_paths()

        filename = blog.src_path + '/_post/' + str(post.id)

        if not os.path.exists(filename):
            self.addError('File does not exists ' + filename)
            return

        f = open(filename, 'rb')
        post_content = f.read()
        f.close()

        self.simpleTransform(values, dom, post_elem, root)

        content_nodes = post_elem.getElementsByTagName("div")
        post_transform = ('post_content')
        for content_node in content_nodes:
            the_class = content_node.getAttribute('class')
            if not the_class in post_transform:
                continue
            if the_class == 'post_content':
                new_node = dom.createTextNode(post_content)
                content_node.appendChild(new_node)

    def createPosts(self, posts, dom, root, node):
        posts_elem = self.createElement(dom, 'posts')
        for i in range(0, self.posts_per_page):
            post_elem = self.createElement(dom, 'post')
            if len(posts) > self.cur_post:
                self.createPost(posts[self.cur_post], dom, post_elem, node)
            else:
                post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
            posts_elem.appendChild(post_elem)

            self.cur_post = self.cur_post + 1
            if self.cur_post == len(posts):
                break
        root.replaceChild(posts_elem, node)

    def createRecents(self, posts, dom, root, node):
        if self.cur_post == len(posts):
            root.removeChild(node)
            return

        if node.hasAttribute("limit"):
            nb_recents = int(node.getAttribute("limit"))
        else:
            nb_recents = 5
        recents_elem = self.createElement(dom, 'recents')
        for child in node.childNodes:
            recents_elem.appendChild(child.cloneNode(True))
        list_elem = dom.createElement('ul')
        for i in range(0, nb_recents):
            post_elem = dom.createElement('li')
            if self.cur_post+i < len(posts):
                post = posts[self.cur_post+i]
                link_elem = self.createLinkElem(dom, post.getPath(), post.title)
                post_elem.appendChild(link_elem)
            else:
                break
            list_elem.appendChild(post_elem)

        recents_elem.appendChild(list_elem)
        root.replaceChild(recents_elem, node)

    def generate(self, blog, src, output):
        from dynastie.models import Post, Blog

        hooks = {'posts' : self.createPosts,
                 'navigation' : self.createNavigation,
                 'recents' : self.createRecents}

        if not os.path.exists(src + '/_index.html'):
            self.addError('No _index.html found, exiting')
            return self.report

        try:
            dom = parse(src + '/_index.html')
        except xml.dom.DOMException as e:
            self.addError('Error parsing _index.html : ' + e)
            return self.report
            
        post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")

        if not post_nodes is None:
            if post_nodes[0].hasAttribute("limit"):
                self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
            else:
                self.posts_per_page = 5
        else:
            self.addError('No tag dyn:posts found')

        now = datetime.datetime.now()
        cur_year = now.year
        posts = Post.objects.filter(creation_date__year=cur_year, published=True, front_page=True).order_by('-creation_date')

        if posts.count() < self.posts_per_page:
            posts = Post.objects.all()[:self.posts_per_page]

        if posts.count() > self.posts_per_page:
            self.nb_pages = self.computeNbPages(posts.count(), self.posts_per_page)

        filename = 'index.html'
        while self.cur_page <= self.nb_pages:
            #print 'Generate ' + filename
            nodes = dom.getElementsByTagName("*")
            nodes[0] = self.parse(src, hooks, posts, dom, nodes[0])
            self.writeIfNotTheSame(output + '/' + filename, nodes[0])
            self.cur_page = self.cur_page + 1
            filename = 'index' + str(self.cur_page) + '.html'
            dom = parse(src + '/_index.html')

        filename = output + '/' + filename

        while os.path.exists(filename):
            self.addReport('Removing unused ' + filename)
            os.unlink(filename)
            filename = filename + '.gz'
            if os.path.exists(filename):
                self.addReport('Removing unused ' + filename)
                os.unlink(filename)
            self.cur_page = self.cur_page + 1
            filename = output + '/index' + str(self.cur_page) + '.html'

        if not self.somethingWrote:
            self.addReport('Nothing changed')

        return self.report
