# Ghiro - Copyright (C) 2013-2016 Ghiro Developers.
# This file is part of Ghiro.
# See the file 'docs/LICENSE.txt' for license terms.

import os

from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator

from analyses.models import Case, Analysis, Comment
from ghiro.common import check_allowed_content
from lib.utils import get_content_type_from_file

class CaseForm(forms.ModelForm):
    """Case form."""
    class Meta:
        model = Case
        fields = "__all__"

class CommentForm(forms.ModelForm):
    """Comment form."""
    class Meta:
        model = Comment
        fields = "__all__"

class UploadImageForm(forms.ModelForm):
    """Image upload form."""
    image = forms.FileField(required=True)

    class Meta:
        model = Analysis
        fields = ["image"]

    def clean_image(self):
        image = self.cleaned_data.get("image", False)
        if image:
            # File check.
            if image._size > settings.MAX_FILE_UPLOAD:
                raise ValidationError("Image file too large")
            # Type check.
            file_type = get_content_type_from_file(image.temporary_file_path())
            if not check_allowed_content(file_type):
                raise ValidationError("Image type not supported.")
        else:
            raise ValidationError("Image field is mandatory.")

class ImageFolderForm(forms.Form):
    """Folder upload form."""
    path = forms.CharField(required=True)

    def clean_image(self):
        path = self.cleaned_data.get("path", False)
        if path:
            # Checks if it exist.
            if not os.path.exists(path):
                raise ValidationError("Specified path not found.")
            # Checks if is a directory.
            if not os.path.isdir(path):
                raise ValidationError("Specified path is not a directory.")
        else:
            raise ValidationError("URL field is mandatory.")

class UrlForm(forms.Form):
    """Image url form."""
    url = forms.CharField(required=True)

    def clean_url(self):
        url = self.cleaned_data.get("url", False)
        if url:
            validate = URLValidator()
            try:
                validate(url)
            except ValidationError:
                raise ValidationError("Please enter a valid URL")
        else:
            raise ValidationError("Path field is mandatory.")
