# coding=utf-8
#########################################################################
# File Name: forms.py
# Original Author: 段凯强
# Mail: duankq@ios.ac.cn
# Created Time: 2013-12-26
# Update: 
#########################################################################

#########################################################################
# Copyright (c) 2013~2014 by 段凯强
# Reand the file "license" distributed with these sources, or XXXX 
# XXXXXXXXXXXXXXXXXX switch for additional information, such as how 
# to use, copy, modify, sell and/or distribute this software and its
# documentation any purpose anyway.
#########################################################################


import datetime, time
import re
from django import forms

class BasicTaskForm(forms.Form):
    taskType = forms.CharField()
    date = forms.DateField()
    time = forms.IntegerField()
    title = forms.CharField()
    content = forms.CharField()

    def clean_taskType(self):
        taskType = self.cleaned_data['taskType']
        if taskType == u'Basic':
            return taskType
        raise forms.ValidationError('taskType_err')

    def clean_date(self):
        date = self.cleaned_data['date']
        today = datetime.date.today()
        if date < today + datetime.timedelta(days = 8) and date > today:
            return date
        raise forms.ValidationError('date_err')

    def clean_time(self):
        time = self.cleaned_data['time']
        if time >= 1 and time <= 3:
            return time
        raise forms.ValidationError('time_err')

    def clean_title(self):
        pattern = re.compile(u'^[a-zA-Z0-9\u4e00-\u9fa5]+$')
        title = self.cleaned_data['title']
        l = len(title)
        if l >= 1 and l <= 10 and pattern.match(title):
            return title
        raise forms.ValidationError('title_err')

    def clean_content(self):
        pattern = re.compile(u'^[（）！￥？。，《》{}【】“”·、：；‘’……\s\x21-\x7e\u4e00-\u9fa5]+$')
        pattern_blank = re.compile(u'^\s+$')
        content = self.cleaned_data['content']
        l = len(content)
        if l >= 1 and l <= 100 and pattern.match(content) and not pattern_blank.match(content):
            return content
        raise forms.ValidationError('content_err')

class BookTaskForm(forms.Form):
    taskType = forms.CharField()
    date = forms.DateField()
    time = forms.IntegerField()
    bookName = forms.CharField()
    readFrom = forms.CharField()
    readTo = forms.CharField()

    def clean_taskType(self):
        taskType = self.cleaned_data['taskType']
        if taskType == u'Book':
            return taskType
        raise forms.ValidationError('taskType_err')

    def clean_date(self):
        date = self.cleaned_data['date']
        today = datetime.date.today()
        if date < today + datetime.timedelta(days = 8) and date > today:
            return date
        raise forms.ValidationError('date_err')

    def clean_time(self):
        time = self.cleaned_data['time']
        if time >= 1 and time <= 3:
            return time
        raise forms.ValidationError('time_err')

    def clean_bookName(self):
        pattern = re.compile(u'^[（）！￥？。，《》{}【】“”·、：；‘’……\s\x21-\x7e\u4e00-\u9fa5]+$')
        pattern_blank = re.compile(u'^\s+$')
        bookName = self.cleaned_data['bookName']
        l = len(bookName)
        if l >= 1 and l <= 50 and pattern.match(bookName) and not pattern_blank.match(bookName):
            return bookName
        raise forms.ValidationError('bookName_err')

    def clean_readFrom(self):
        pattern = re.compile(u'^[（）！￥？。，《》{}【】“”·、：；‘’……\s\x21-\x7e\u4e00-\u9fa5]+$')
        pattern_blank = re.compile(u'^\s+$')
        readFrom = self.cleaned_data['readFrom']
        l = len(readFrom)
        if l >= 1 and l <= 50 and pattern.match(readFrom) and not pattern_blank.match(readFrom):
            return readFrom
        raise forms.ValidationError('readFrom_err')

    def clean_readTo(self):
        pattern = re.compile(u'^[（）！￥？。，《》{}【】“”·、：；‘’……\s\x21-\x7e\u4e00-\u9fa5]+$')
        pattern_blank = re.compile(u'^\s+$')
        readTo = self.cleaned_data['readTo']
        l = len(readTo)
        if l >= 1 and l <= 50 and pattern.match(readTo) and not pattern_blank.match(readTo):
            return readTo
        raise forms.ValidationError('readTo_err')

class WorkTaskForm(forms.Form):
    taskType = forms.CharField()
    date = forms.DateField()
    time = forms.IntegerField()
    summary = forms.CharField()
    goal = forms.CharField()

    def clean_taskType(self):
        taskType = self.cleaned_data['taskType']
        if taskType == u'Work':
            return taskType
        raise forms.ValidationError('taskType_err')

    def clean_date(self):
        date = self.cleaned_data['date']
        today = datetime.date.today()
        if date < today + datetime.timedelta(days = 8) and date > today:
            return date
        raise forms.ValidationError('date_err')

    def clean_time(self):
        time = self.cleaned_data['time']
        if time >= 1 and time <= 3:
            return time
        raise forms.ValidationError('time_err')

    def clean_summary(self):
        pattern = re.compile(u'^[（）！￥？。，《》{}【】“”·、：；‘’……\s\x21-\x7e\u4e00-\u9fa5]+$')
        pattern_blank = re.compile(u'^\s+$')
        summary = self.cleaned_data['summary']
        l = len(summary)
        if l >= 1 and l <= 50 and pattern.match(summary) and not pattern_blank.match(summary):
            return summary
        raise forms.ValidationError('summary')

    def clean_goal(self):
        pattern = re.compile(u'^[（）！￥？。，《》{}【】“”·、：；‘’……\s\x21-\x7e\u4e00-\u9fa5]+$')
        pattern_blank = re.compile(u'^\s+$')
        goal = self.cleaned_data['goal']
        l = len(goal)
        if l >= 1 and l <= 50 and pattern.match(goal) and not pattern_blank.match(goal):
            return goal
        raise forms.ValidationError('goal_err')

class HomeworkTaskForm(forms.Form):
    taskType = forms.CharField()
    date = forms.DateField()
    time = forms.IntegerField()
    courseName = forms.CharField()
    introduction = forms.CharField()

    def clean_taskType(self):
        taskType = self.cleaned_data['taskType']
        if taskType == u'Homework':
            return taskType
        raise forms.ValidationError('taskType_err')

    def clean_date(self):
        date = self.cleaned_data['date']
        today = datetime.date.today()
        if date < today + datetime.timedelta(days = 8) and date > today:
            return date
        raise forms.ValidationError('date_err')

    def clean_time(self):
        time = self.cleaned_data['time']
        if time >= 1 and time <= 3:
            return time
        raise forms.ValidationError('time_err')

    def clean_courseName(self):
        pattern = re.compile(u'^[a-zA-Z0-9\u4e00-\u9fa5]+$')
        courseName = self.cleaned_data['courseName']
        l = len(courseName)
        if l >= 1 and l <= 10 and pattern.match(courseName):
            return courseName
        raise forms.ValidationError('courseName_err')

    def clean_introduction(self):
        pattern = re.compile(u'^[（）！￥？。，《》{}【】“”·、：；‘’……\s\x21-\x7e\u4e00-\u9fa5]+$')
        pattern_blank = re.compile(u'^\s+$')
        introduction = self.cleaned_data['introduction']
        l = len(introduction)
        if l >= 1 and l <= 100 and pattern.match(introduction) and not pattern_blank.match(introduction):
            return introduction
        raise forms.ValidationError('introduction_err')

class TaskIdForm(forms.Form):
    taskId = forms.IntegerField()

    def clean_taskId(self):
        taskId = self.cleaned_data['taskId']
        if taskId > 0:
            return taskId
        raise forms.ValidationError('taskId_err')



class ChangeDateForm(forms.Form):
    taskId = forms.IntegerField()
    date = forms.DateField()
    time = forms.IntegerField()

    def clean_taskId(self):
        taskId = self.cleaned_data['taskId']
        if taskId > 0:
            return taskId
        raise forms.ValidationError('taskId_err')

    def clean_date(self):
        date = self.cleaned_data['date']
        today = datetime.date.today()
        if date < today + datetime.timedelta(days = 8) and date > today:
            return date
        raise forms.ValidationError('date_err')

    def clean_time(self):
        time = self.cleaned_data['time']
        if time >= 1 and time <= 3:
            return time
        raise forms.ValidationError('time_err')



class ExchangeTaskForm(forms.Form):
    taskId1 = forms.IntegerField()
    taskId2 = forms.IntegerField()
    
    def clean_taskId1(self):
        taskId1 = self.cleaned_data['taskId1']
        if taskId1 > 0:
            return taskId1
        raise forms.ValidationError('taskId1_err')

    def clean_taskId2(self):
        taskId2 = self.cleaned_data['taskId2']
        if taskId2 > 0:
            return taskId2
        raise forms.ValidationError('taskId2_err')



class DelayShiftTaskForm(forms.Form):
    fromId = forms.IntegerField()
    toId = forms.IntegerField()

    def clean_fromId(self):
        fromId = self.cleaned_data['fromId']
        if fromId > 0:
            return fromId
        raise forms.ValidationError('fromId_err')

    def clean_toId(self):
        toId = self.cleaned_data['toId']
        if toId > 0:
            return toId
        raise forms.ValidationError('toId_err')
