#!/usr/bin/env python3
#coding: utf-8

#!---------- squiNotes.py ----------
# My tasks-taking app
#-----------------------------!

#CLASS
class task:
    def __init__(self, createtime: int, modtime: int, title: str, text: str, done: bool, priority: int, personnal: bool = False):
        """
        createtime, modtime : epoch time of task writing / modfying
        title and text are str
        """
        self.modtime = modtime
        self.createtime = createtime
        self.title = title
        self.text = text
        self.priority = priority
        self.personnal = personnal
        
        if done:
            self.donemark = "✅"
        else: 
            if personnal:
                self.donemark = "🔒"
            else:
                self.donemark = "⚠️"

    def rendertime(self, pretimestamp: int):
        """
        Render the given timestamp as dd/mm/yyyy-hh:mm
        """
        import datetime
        timestamp = datetime.datetime.fromtimestamp(pretimestamp)
        timestamp = timestamp.strftime("%d/%m/%Y-%H:%M:%S")
        return timestamp

    def flaskrender(self):
        """
        Render the task whole content as html for flask, using flask.Markup
        """
        import markdown
        from flask import Markup, url_for

        rendered = f"""
        <div class="taskcontainer">
        <div class="tasktitle">{self.donemark} {Markup.escape(self.title)}</div>
        <form action="." method="GET" name="{self.createtime}"><button type="submit" name="toswitch" value="{self.createtime}" class "taskbutton">Done/Todo</button>|<button type="submit" name="delete" value="{self.createtime}" class="taskbutton" onclick="return confirm('Really ?')">Delete</button>|<button type="submit" name="edit" value="{self.createtime}" class="taskbutton">Edit</button>
        </form>
        <div class="tasktime">Priority: {self.priority}</div>
        <div class="tasktime">Created : {self.rendertime(self.createtime)}
        <br>Modified : {self.rendertime(self.modtime)}</div><br>
        <div class="tasktext">{markdown.markdown(self.text, extensions=['fenced_code', 'codehilite', 'nl2br', 'smarty'])}</div><br>
        </div>
        """
        return Markup(rendered)

    def __str__(self):
        return f"{self.title} - {self.donemark} - Priority {self.priority}"
