from django.db import models

from wagtail.admin.edit_handlers import FieldPanel
from wagtail.snippets.models import register_snippet
from django.utils.translation import ugettext_lazy as _


@register_snippet
class Address(models.Model):
    name = models.CharField(max_length=64, verbose_name=_('Name'))
    line_1 = models.CharField(max_length=128, verbose_name=_('Address Line 1'))
    line_2 = models.CharField(max_length=128, blank=True, verbose_name=_('Address Line 2'))
    city = models.CharField(max_length=64, verbose_name=_('City'))
    postcode = models.CharField(max_length=10, verbose_name=_('Postcode'))
    country = models.ForeignKey('shipping.Country', blank=True, null=True, on_delete=models.PROTECT, verbose_name=_('Country'))

    panels = [
        FieldPanel('name'),
        FieldPanel('line_1'),
        FieldPanel('line_2'),
        FieldPanel('city'),
        FieldPanel('postcode'),
        FieldPanel('country')
    ]

    def __str__(self):
        return "{}, {}, {}".format(self.name, self.city, self.country)


class Country(models.Model):
    """
    International Organization for Standardization (ISO) 3166-1 Country list
    Instance Variables:
    iso -- ISO 3166-1 alpha-2
    name -- Official country names (in all caps) used by the ISO 3166
    display_name -- Country names in title format
    sort_priority -- field that allows for customizing the default ordering
    0 is the default value, and the higher the value the closer to the
    beginning of the list it will be.  An example use case would be you will
    primarily have addresses for one country, so you want that particular
    country to be the first option in an html dropdown box.  To do this, you
    would simply change the value in the json file or alter
    country_grabber.py's priority dictionary and run it to regenerate
    the json
    """
    iso = models.CharField(max_length=2, primary_key=True)
    name_official = models.CharField(max_length=128)
    name = models.CharField(max_length=128)
    sort_priority = models.PositiveIntegerField(default=0)

    class Meta:
        verbose_name_plural = _('Countries')
        ordering = ('-sort_priority', 'name',)

    def __str__(self):
        """ Return the display form of the country name"""
        return self.name
