from pathlib import Path
from urllib.parse import urljoin

from django.db import models
from django.urls import reverse

import settings
from troggle.core.models.troggle import Expedition, TroggleModel

"""The model declarations  LogBookEntry, PersonLogEntry, QM
"""

todo = """
- Can we rewrite things to eliminate the CaveSlug and EntranceSlug Classes and objects? Surely
  foreign keys work fine ?!
"""


class CaveSlug(models.Model):
    """Moved here to avoid nasty cyclic import error"""

    cave = models.ForeignKey("Cave", on_delete=models.CASCADE)
    slug = models.SlugField(max_length=50, unique=True)
    primary = models.BooleanField(default=False)


class LogbookEntry(TroggleModel):
    """Single parsed entry from Logbook"""

    date = (
        models.DateField()
    )  # MJG wants to turn this into a datetime such that multiple Logbook entries on the same day can be ordered.ld()
    expedition = models.ForeignKey(Expedition, blank=True, null=True, on_delete=models.SET_NULL)  # yes this is double-
    title = models.CharField(max_length=200)
    cave_slug = models.SlugField(max_length=50, blank=True, null=True)
    place = models.CharField(
        max_length=100, blank=True, null=True, help_text="Only use this if you haven't chosen a cave"
    )
    text = models.TextField()
    slug = models.SlugField(max_length=50)
    time_underground = models.FloatField(null=True, help_text="In decimal hours")

    class Meta:
        verbose_name_plural = "Logbook Entries"
        # several PersonLogEntrys point in to this object
        ordering = ("-date",)

    def cave(self):  # Why didn't he just make this a foreign key to Cave ?
        c = CaveSlug.objects.get(slug=self.cave_slug, primary=True).cave
        return c

    def isLogbookEntry(self):  # Function used in templates
        return True

    def get_absolute_url(self):
        return urljoin(settings.URL_ROOT, reverse("logbookentry", kwargs={"date": self.date, "slug": self.slug}))

    def __str__(self):
        return f"{self.date}: {self.title}"

    def get_next_by_id(self):
        LogbookEntry.objects.get(id=self.id + 1)

    def get_previous_by_id(self):
        LogbookEntry.objects.get(id=self.id - 1)

    def DayIndex(self):
        """This is used to set different colours for the different trips on
        the calendar view of the expedition"""
        mx = 10
        todays = list(LogbookEntry.objects.filter(date=self.date))
        if self in todays:
            index = todays.index(self)
        else:
            print(f"DayIndex: Synchronization error. Restart server.  {self}")
            index = 0

        if index not in range(0, mx):
            print(f"DayIndex: More than {mx-1} LogbookEntry items on one day  '{index}' {self}")
            index = 0
        return index


class PersonLogEntry(TroggleModel):
    """Single Person going on a trip, which may or may not be written up.
    It could account for different T/U for people in same logbook entry.
    """

    personexpedition = models.ForeignKey("PersonExpedition", null=True, on_delete=models.CASCADE)
    time_underground = models.FloatField(help_text="In decimal hours")
    logbook_entry = models.ForeignKey(LogbookEntry, on_delete=models.CASCADE)
    is_logbook_entry_author = models.BooleanField(default=False)

    class Meta:
        ordering = ("-personexpedition",)
        # order_with_respect_to = 'personexpedition'

    def next_personlog(self):
        futurePTs = (
            PersonLogEntry.objects.filter(
                personexpedition=self.personexpedition, logbook_entry__date__gt=self.logbook_entry.date
            )
            .order_by("logbook_entry__date")
            .all()
        )
        if len(futurePTs) > 0:
            return futurePTs[0]
        else:
            return None

    def prev_personlog(self):
        pastPTs = (
            PersonLogEntry.objects.filter(
                personexpedition=self.personexpedition, logbook_entry__date__lt=self.logbook_entry.date
            )
            .order_by("-logbook_entry__date")
            .all()
        )
        if len(pastPTs) > 0:
            return pastPTs[0]
        else:
            return None

    def place(self):
        return self.logbook_entry.cave and self.logbook_entry.cave or self.logbook_entry.place

    def __str__(self):
        return f"{self.personexpedition} ({self.logbook_entry.date})"


class QM(TroggleModel):
    """This is based on qm.csv in trunk/expoweb/1623/204 which has the fields:
    "Number","Grade","Area","Description","Page reference","Nearest station","Completion description","Comment"
    """

    cave = models.ForeignKey("Cave", related_name="QMs", blank=True, null=True, on_delete=models.SET_NULL)
    block = models.ForeignKey("SurvexBlock", null=True, on_delete=models.SET_NULL)  # only for QMs from survex files
    blockname = models.TextField(blank=True, null=True)  # NB truncated copy of survexblock name with last char added
    expoyear = models.CharField(
        max_length=4, blank=True, null=True
    )  # could change to datetime if logbooks similarly chnaged
    found_by = models.ForeignKey(
        LogbookEntry, related_name="QMs_found", blank=True, null=True, on_delete=models.SET_NULL
    )
    ticked = models.BooleanField(
        default=False
    )  # for ticked QMs not attached to a logbook entry, should imply completion_description has text
    ticked_off_by = models.ForeignKey(
        LogbookEntry, related_name="QMs_ticked_off", blank=True, null=True, on_delete=models.SET_NULL
    )  # unused, ever?!
    number = models.IntegerField(
        help_text="this is the sequential number in the year, only unique for CSV imports",
    )
    GRADE_CHOICES = (
        ("A", "A: Large obvious lead"),
        ("B", "B: Average lead"),
        ("C", "C: Tight unpromising lead"),
        ("D", "D: Dig"),
        ("X", "X: Unclimbable aven"),
    )  # also seen "?" and "V" in imported data - see urls.py
    grade = models.CharField(max_length=1, choices=GRADE_CHOICES)
    location_description = models.TextField(blank=True)
    nearest_station_description = models.CharField(max_length=400, blank=True, null=True)
    nearest_station_name = models.CharField(max_length=200, blank=True, null=True)
    nearest_station = models.ForeignKey("SurvexStation", blank=True, null=True, on_delete=models.SET_NULL)
    area = models.CharField(max_length=100, blank=True, null=True)
    completion_description = models.TextField(blank=True, null=True)
    comment = models.TextField(blank=True, null=True)

    def __str__(self):
        return f"{self.code()}"

    def code(self):
        if self.cave:
            cavestr = str(self.cave.slug())[5:]
        else:
            cavestr = ""
        if self.expoyear:
            expoyearstr = str(self.expoyear)
        else:
            expoyearstr = str(self.cave.slug())[5:9]
        if self.blockname:
            blocknamestr = "-" + str(self.blockname)
        else:
            blocknamestr = ""
        return f"{cavestr}-{expoyearstr}-{self.number}{self.grade}{blocknamestr}"

    def get_completion_url(self):
        """assumes html file named is in same folder as cave description file"""
        cd = None
        if self.completion_description:
            try:
                dir = Path(self.cave.url).parent
                cd = dir / self.completion_description
            except:
                cd = None
        return cd

    def newslug(self):
        qmslug = f"{str(self.cave)}-{self.expoyear}-{self.blockname}{self.number}{self.grade}"
        return qmslug

    def get_absolute_url(self):
        # This reverse resolution stuff is pure magic. Just change the regex in urls.py and everything changes to suit. Whacky.
        return urljoin(
            settings.URL_ROOT,
            reverse(
                "qm",
                kwargs={
                    "cave_id": self.cave.slug(),
                    "year": self.expoyear,
                    "blockname": self.blockname,
                    "qm_id": self.number,
                    "grade": self.grade,
                },
            ),
        )

    def get_next_by_id(self):
        return QM.objects.get(id=self.id + 1)

    def get_previous_by_id(self):
        return QM.objects.get(id=self.id - 1)
