<reponame>ablil/meistertask-cli
from typing import Dict
from datetime import datetime

from src.utils import Colors


class Task:
    task_status = {1: "open", 2: "completed", 3: "completed_archived", 4: "trashed"}

    def __init__(self, name="", description="", project_id=None, section_id=None):
        self.id = None
        self.name = name
        self.description = description
        self.status = self.task_status[1]
        self.created = None
        self.updated = None

        self.section_id = section_id
        self._section_name = None
        self._project_id = project_id

    @staticmethod
    def from_json(json: Dict):
        task = Task()
        task.id = json["id"]
        task.name = json["name"]
        task.description = json["notes"] if json["notes"] else ""
        if json["status"] in Task.task_status.keys():
            task.status = Task.task_status[json["status"]]
        task.created = datetime.fromisoformat(
            json["created_at"].replace("T", " ").replace("Z", "")
        )
        task.updated = datetime.fromisoformat(
            json["updated_at"].replace("T", " ").replace("Z", "")
        )

        task.section_id = json["section_id"]
        task._section_name = json["section_name"]
        task._project_id = json["project_id"]

        return task

    def __repr__(self) -> str:
        return "\n".join(
            (
                Colors.wrap(Colors.UNDERLINE, "Name")
                + ": {} / {}".format(self.name, self.id),
                Colors.wrap(Colors.UNDERLINE, "Status") + ": {}".format(self.status),
                Colors.wrap(Colors.UNDERLINE, "Description")
                + ": {}".format(self.description),
                Colors.wrap(Colors.UNDERLINE, "Section")
                + ": {}".format(self._section_name),
                Colors.wrap(Colors.UNDERLINE, "Created")
                + ": {} / ".format(self.created)
                + Colors.wrap(Colors.UNDERLINE, "Updated")
                + ": {}".format(self.updated),
                "\n",
            )
        )

    def show_summary(self):
        if self.description and len(self.description) > 10:
            print(
                Colors.wrap(Colors.UNDERLINE, "Task")
                + ": {} ({}) / {} / {} ".format(
                    self.name, self.id, self.status, self.description[:10] + " ..."
                )
            )
        else:
            print(
                Colors.wrap(Colors.UNDERLINE, "Task")
                + ": {} ({}) / {} / {} ".format(
                    self.name, self.id, self.status, self.description[:10] + " ..."
                )
            )
