'''

@author: olivier.massot, 2018
'''
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from martor.fields import MartorFormField

from main.models import Story, Epic, Comment, Sprint
from django.forms.fields import DateField

class RegisterForm(UserCreationForm):
                            
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email')
        
    def save(self, commit=True):
        user = super(RegisterForm, self).save(commit=False)
        user.first_name = self.cleaned_data["first_name"]
        user.last_name = self.cleaned_data["last_name"]
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()

class ProfileForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email')

class EpicForm(forms.ModelForm):
    
    description = MartorFormField(label="Description")
 
    class Meta:
        model = Epic
        fields = ('project', 'name', 'size', 'value', 'description')
        
    def __init__(self, *args, **kwargs):
        super(EpicForm, self).__init__(*args, **kwargs)
        self.fields['description'].required = False
        
class StoryForm(forms.ModelForm):
    
    description = MartorFormField(label="Description")
 
    class Meta:
        model = Story
        widgets = {'author': forms.HiddenInput()}
        fields = ('epic', 'author', 'name', 'weight', 'description', 'assignees', 'sprints')
        
    def __init__(self, *args, **kwargs):
        super(StoryForm, self).__init__(*args, **kwargs)
        self.fields['description'].required = False
        self.fields['assignees'].required = False
        self.fields['sprints'].required = False
        self.fields['weight'].required = False
    
    def save(self, *args, **kwargs):
        if self.cleaned_data['weight'] == '':
            self.cleaned_data['weight'] = None
        return super(StoryForm, self).save(*args, **kwargs)
        
        
class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        widgets = {'content_type': forms.HiddenInput(), 'object_id': forms.HiddenInput()}
        fields = ('content','content_type', 'object_id')
    content = MartorFormField(label="Commentaire")

    def __init__(self, *args, **kwargs):
        super(CommentForm, self).__init__(*args, **kwargs)
        if not self.prefix:
            self.prefix = str(self.instance.id) if self.instance and self.instance.id else "new"
 
class NewSprintForm(forms.ModelForm):
    class Meta:
        model = Sprint
        widgets = {'number': forms.HiddenInput()}
        fields = ('date_start','date_end','number')
        
    date_start = DateField(label='Date de début', widget=forms.TextInput(attrs={'class':'datepicker'}))
    date_end = DateField(label='Date de fin', widget=forms.TextInput(attrs={'class':'datepicker'}))

    def clean(self):
        cleaned_data = super().clean()
        
        if not cleaned_data.get("date_end") > cleaned_data.get("date_start"):
            raise forms.ValidationError("La date de fin doit être postérieure à la date de début.")

        if not cleaned_data.get("date_start") >= Sprint.current().date_end:
            raise forms.ValidationError("La date de début doit être postérieure à la date de fin du sprint en cours.")

class SprintForm(forms.ModelForm):
    class Meta:
        model = Sprint
        fields = ('retro',)
    retro = MartorFormField(label="Rétrospective")
    
    
    
    
