# -*- Coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
from django import forms
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone

import datetime

# Create your models here.


class BoatCategory(models.Model):
    name = models.CharField(_("Boat Category"),max_length=30)
    club = models.CharField(_("Club"),max_length=30)

    class Meta:
        verbose_name = _("boat category")
        verbose_name_plural = _("boat categories")
        ordering = ['name']

    def __str__(self):
        str = self.name

        return str

    def __unicode__(self):
        str = self.name

        return str

class Member(models.Model):
    statuses = (
        ("coach", _("coach")),
        ("member", _("member")),
        ("administrator", _("administrator")),
        )

    class Meta:
        verbose_name = _("member")
        verbose_name_plural = _("members")
        ordering = ['user']
    
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    contributions = models.BooleanField(default=True)
    status = models.CharField(default="member",
                              choices=statuses,
                              max_length=30)
    categories = models.ManyToManyField(BoatCategory,
                                        verbose_name=_("Boat Categories"))
    club = models.CharField(_("Club"),max_length=30)
    hoursworked = models.FloatField(_("Hours Worked"))

    def __str__(self):
        str =  self.user.first_name+' '+self.user.last_name
        return str

    def __unicode__(self):
        str =  self.user.first_name+' '+self.user.last_name
        return str

class Boat(models.Model):
    class Meta:
        verbose_name = _("boat")
        verbose_name_plural = _("boats")
        ordering = ['boatcode']
    
    boatname = models.CharField(_("Boat Name"),max_length=30)
    boatcode = models.CharField(_("Boat Code"),max_length=10)
    categories = models.ManyToManyField(BoatCategory)
    nrseats = models.IntegerField(_("Nr of Seats"))
    statuses = (
        ("water",_("water")),
        ("hangar",_("hangar")),
        ("damaged",_("damaged")),
        ("races",_("races")),
        )
    status = models.CharField(default="hangar",
                              choices=statuses,
                              max_length=30)

    comment = models.CharField(_("Comment"),blank=True,max_length=100)

    def __str__(self):
        str = self.boatcode+' '+self.boatname

        return str

    def __unicode__(self):
        str = self.boatcode+' '+self.boatname

        return str

class MemberWork(models.Model):
    class Meta:
        verbose_name = _("member work")
        verbose_name_plural = _("member work")
    
    date = models.DateField(_("Date"))
    hours = models.FloatField(_("Hours"))
    name = models.CharField(_("Work Event Name"),max_length=30)
    comment = models.CharField(_("Comment"),blank=True,max_length=100)
    worker = models.ManyToManyField(Member,_("Worker"))
    statuses = (
        ("planned",_("planned")),
        ("executed",_("executed"))
        )
    status = models.CharField(default="planned",
                              choices=statuses,
                              max_length=30)
class Races(models.Model):
    class Meta:
        verbose_name = _("race")
        verbose_name_plural = _("races")


    name = models.CharField(max_length = 30)
    startdatetime = models.DateTimeField(verbose_name = _("Start Time"),default=timezone.now)
    enddatetime = models.DateTimeField(verbose_name = _("End Time"),default=timezone.now)

    transportboats = models.ManyToManyField(Boat)

    def __str__(self):
        str = self.name
        str = str+' '+self.startdatetime.strftime('%Y-%m-%d %H:%M:%S')
        return str

    def __unicode__(self):
        str = self.name
        str = str+' '+self.startdatetime.strftime('%Y-%m-%d %H:%M:%S')
        return str


class Outing(models.Model):
    class Meta:
        verbose_name = _("outing")
        verbose_name_plural = _("outings")
    

    starttime = models.DateTimeField(verbose_name = _("Start Time"),default=timezone.now)
    endtime = models.DateTimeField(verbose_name = _("End Time"),blank=True,null=True)
    distance = models.FloatField(_("Distance (km)"),default=12)
    comment = models.CharField(_("Comment"),blank=True,max_length=100)
    statuses = (
        ("reservation",_("reservation")),
        ("active", _("active")),
        ("completed", _("completed")),
        ("race", _("race")),
        )
    boat = models.ForeignKey(Boat,on_delete=models.CASCADE)
    rower = models.ForeignKey(Member,on_delete=models.CASCADE)
    race = models.ManyToManyField(Races,blank=True)
    otherrowers = models.ManyToManyField(Member,related_name="otherrowers")
    status = models.CharField(_("Status"),default="active",
                              choices=statuses,
                              max_length=30)


    def __str__(self):
        str = self.boat.boatcode
        str = str+' '+self.starttime.strftime('%Y-%m-%d %H:%M:%S')
        return str

    def __unicode__(self):
        str = self.boat.boatcode
        str = str+' '+self.starttime.strftime('%Y-%m-%d %H:%M:%S')
        return str

