class WTODocumentForm(DocumentMixin, forms.Form):
    """
    Form used to add documents via ajax. Only one field will be populated at a time.
    """

    committee_notification_document = RestrictedFileField(
        label="Attach committee notification document",
        content_types=settings.ALLOWED_FILE_TYPES,
        max_upload_size=settings.FILE_MAX_SIZE,
        required=False,
        multi_document=False,
    )
    meeting_minutes = RestrictedFileField(
        label="Committee meeting minutes",
        content_types=settings.ALLOWED_FILE_TYPES,
        max_upload_size=settings.FILE_MAX_SIZE,
        required=False,
        multi_document=False,
    )

    def save(self):
        if self.cleaned_data.get("committee_notification_document"):
            return self.upload_document("committee_notification_document")
        if self.cleaned_data.get("meeting_minutes"):
            return self.upload_document("meeting_minutes")
class AddNoteForm(DocumentMixin, forms.Form):
    note = forms.CharField(
        label=("Add notes on an interaction or event"),
        widget=forms.Textarea,
        error_messages={"required": "Add text for the note."},
    )
    document_ids = MultipleValueField(required=False)
    document = RestrictedFileField(
        label="Attach a document",
        content_types=settings.ALLOWED_FILE_TYPES,
        max_upload_size=settings.FILE_MAX_SIZE,
        required=False,
    )

    def __init__(self, barrier_id, *args, **kwargs):
        self.barrier_id = barrier_id
        super().__init__(*args, **kwargs)

    def clean_document(self):
        return self.validate_document()

    def save(self):
        client = MarketAccessAPIClient(self.token)
        client.notes.create(
            barrier_id=self.barrier_id,
            text=self.cleaned_data["note"],
            documents=self.cleaned_data.get("document_ids"),
        )
class EconomicAssessmentDocumentForm(DocumentMixin, forms.Form):
    document = RestrictedFileField(
        content_types=settings.ALLOWED_FILE_TYPES,
        max_upload_size=settings.FILE_MAX_SIZE,
    )

    def save(self):
        return self.upload_document()
class EconomicAssessmentRatingForm(DocumentMixin, forms.Form):
    rating = forms.ChoiceField(
        label="What is the initial economic assessment of this barrier?",
        choices=[],
        error_messages={
            "required":
            "Select the initial economic assessment of this barrier"
        },
    )
    explanation = forms.CharField(
        label="Explain the assessment",
        widget=forms.Textarea,
        required=True,
        error_messages={"required": "Enter an explanation"},
    )
    document_ids = MultipleValueField(required=False)
    document = RestrictedFileField(
        content_types=settings.ALLOWED_FILE_TYPES,
        max_upload_size=settings.FILE_MAX_SIZE,
        required=False,
    )
    ready_for_approval = forms.NullBooleanField()
    approved = forms.NullBooleanField()

    def __init__(self,
                 ratings,
                 barrier=None,
                 economic_assessment=None,
                 *args,
                 **kwargs):
        self.barrier = barrier
        self.economic_assessment = economic_assessment
        super().__init__(*args, **kwargs)
        self.fields["rating"].choices = [(key, value)
                                         for key, value in ratings.items()]

    def clean(self):
        cleaned_data = super().clean()
        if self.cleaned_data["ready_for_approval"] is None:
            del cleaned_data["ready_for_approval"]
        if self.cleaned_data["approved"] is None:
            del cleaned_data["approved"]
        cleaned_data["documents"] = self.cleaned_data.pop("document_ids", [])
        del cleaned_data["document"]

    def clean_document(self):
        return self.validate_document()

    def save(self):
        client = MarketAccessAPIClient(self.token)
        if self.economic_assessment:
            client.economic_assessments.patch(id=self.economic_assessment.id,
                                              **self.cleaned_data)
        elif self.barrier:
            client.economic_assessments.create(
                barrier_id=self.barrier.id,
                **self.cleaned_data,
            )
class WTOProfileForm(DocumentMixin, forms.Form):
    committee_notified = forms.ChoiceField(
        label="Committee notified of the barrier",
        choices=(),
        required=False,
    )
    committee_notification_link = forms.URLField(
        label="Committee notification",
        help_text=
        ("Enter a web link to the notification or attach a committee notification "
         "document"),
        max_length=2048,
        required=False,
        error_messages={
            "max_length":
            ("Notification link should be %(limit_value)d characters or fewer",
             )
        },
    )
    committee_notification_document_id = forms.CharField(required=False)
    committee_notification_document = RestrictedFileField(
        label="Attach committee notification document",
        content_types=settings.ALLOWED_FILE_TYPES,
        max_upload_size=settings.FILE_MAX_SIZE,
        required=False,
    )
    member_states = forms.MultipleChoiceField(
        label="Member state(s) that raised the barrier in a WTO committee",
        required=False,
    )
    committee_raised_in = forms.ChoiceField(
        label="WTO committee the barrier was raised in",
        choices=(),
        required=False,
    )
    meeting_minutes_id = forms.CharField(required=False)
    meeting_minutes = RestrictedFileField(
        label="Committee meeting minutes",
        content_types=settings.ALLOWED_FILE_TYPES,
        max_upload_size=settings.FILE_MAX_SIZE,
        required=False,
    )
    raised_date = DayMonthYearField(
        label="Date the barrier was raised in a bilateral meeting in Geneva",
        help_text="For example 30 11 2020",
        required=False,
    )
    case_number = forms.CharField(
        label="WTO dispute settlement case number for the barrier",
        max_length=255,
        required=False,
        error_messages={
            "max_length":
            "Case number should be %(limit_value)d characters or fewer",
        },
    )

    def __init__(self, id, metadata, wto_profile, *args, **kwargs):
        self.id = id
        self.metadata = metadata
        super().__init__(*args, **kwargs)
        self.set_committee_notified_choices()
        self.set_member_states_choices()
        self.set_committee_raised_in_choices()

        if wto_profile.wto_has_been_notified is False:
            self.fields[
                "committee_notified"].label = "Which committee should be notified of the barrier?"
            del self.fields["committee_notification_link"]
            del self.fields["committee_notification_document"]
            del self.fields["committee_notification_document_id"]
            del self.fields["meeting_minutes"]
        if wto_profile.wto_should_be_notified is False:
            del self.fields["committee_notified"]

    def get_committee_choices(self):
        """
        Grouped committee choices by committee group
        """
        return (("", "Select a committee"), ) + tuple((
            group["name"],
            tuple((committee["id"], committee["name"])
                  for committee in group["wto_committees"]),
        ) for group in self.metadata.get_wto_committee_groups())

    def set_member_states_choices(self):
        self.fields["member_states"].choices = [
            (country["id"], country["name"])
            for country in self.metadata.get_country_list()
        ]

    def set_committee_notified_choices(self):
        self.fields["committee_notified"].choices = self.get_committee_choices(
        )

    def set_committee_raised_in_choices(self):
        self.fields[
            "committee_raised_in"].choices = self.get_committee_choices()

    def clean_committee_notification_document(self):
        return self.validate_document("committee_notification_document")

    def clean_meeting_minutes(self):
        return self.validate_document("meeting_minutes")

    def clean_raised_date(self):
        if self.cleaned_data.get("raised_date"):
            return self.cleaned_data["raised_date"].isoformat()

    def save(self):
        client = MarketAccessAPIClient(self.token)
        wto_profile = {
            "committee_notified": self.cleaned_data.get("committee_notified"),
            "member_states": self.cleaned_data.get("member_states"),
            "committee_raised_in":
            self.cleaned_data.get("committee_raised_in"),
            "raised_date": self.cleaned_data.get("raised_date"),
            "case_number": self.cleaned_data.get("case_number", ""),
        }
        if "committee_notification_link" in self.fields:
            wto_profile["committee_notification_link"] = self.cleaned_data.get(
                "committee_notification_link", "")
        if "committee_notification_document" in self.fields:
            wto_profile[
                "committee_notification_document"] = self.cleaned_data.get(
                    "committee_notification_document_id")
        if "meeting_minutes" in self.fields:
            wto_profile["meeting_minutes"] = self.cleaned_data.get(
                "meeting_minutes_id")
        client.barriers.patch(id=self.id, wto_profile=wto_profile)