from decimal import Decimal

from django.db import models
from django.utils.formats import localize
from django.utils.translation import ugettext_lazy as _
from django_countries.fields import CountryField
from i18nfield.fields import I18nCharField

from pretix.base.decimal import round_decimal
from pretix.base.models.base import LoggedModel


class TaxedPrice:
    def __init__(self, *, gross: Decimal, net: Decimal, tax: Decimal, rate: Decimal, name: str):
        if net + tax != gross:
            raise ValueError('Net value and tax value need to add to the gross value')
        self.gross = gross
        self.net = net
        self.tax = tax
        self.rate = rate
        self.name = name

    def __repr__(self):
        return '{} + {}% = {}'.format(localize(self.net), localize(self.rate), localize(self.gross))


TAXED_ZERO = TaxedPrice(
    gross=Decimal('0.00'),
    net=Decimal('0.00'),
    tax=Decimal('0.00'),
    rate=Decimal('0.00'),
    name=''
)

EU_COUNTRIES = {
    'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT',
    'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'GB'
}
EU_CURRENCIES = {
    'BG': 'BGN',
    'GB': 'GBP',
    'HR': 'HRK',
    'CZ': 'CZK',
    'DK': 'DKK',
    'HU': 'HUF',
    'PL': 'PLN',
    'RO': 'RON',
    'SE': 'SEK'
}


class TaxRule(LoggedModel):
    event = models.ForeignKey('Event', related_name='tax_rules')
    name = I18nCharField(
        verbose_name=_('Name'),
        help_text=_('Should be short, e.g. "VAT"'),
        max_length=190,
    )
    rate = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        verbose_name=_("Tax rate")
    )
    price_includes_tax = models.BooleanField(
        verbose_name=_("The configured product prices include the tax amount"),
        default=True,
    )
    eu_reverse_charge = models.BooleanField(
        verbose_name=_("Use EU reverse charge taxation rules"),
        default=False,
        help_text=_("Not recommended. Most events will NOT be qualified for reverse charge since the place of "
                    "taxation is the location of the event. This option disables charging VAT for all customers "
                    "outside the EU and for business customers in different EU countries who entered a valid EU VAT "
                    "ID. Only enable this option after consulting a tax counsel. No warranty given for correct tax "
                    "calculation. USE AT YOUR OWN RISK.")
    )
    home_country = CountryField(
        verbose_name=_('Merchant country'),
        blank=True,
        help_text=_('Your country of residence. This is the country the EU reverse charge rule will not apply in, '
                    'if configured above.'),
    )

    def allow_delete(self):
        from pretix.base.models.orders import OrderFee, OrderPosition

        return (
            not OrderFee.objects.filter(tax_rule=self, order__event=self.event).exists()
            and not OrderPosition.objects.filter(tax_rule=self, order__event=self.event).exists()
            and not self.event.items.filter(tax_rule=self).exists()
            and self.event.settings.tax_rate_default != self
        )

    @classmethod
    def zero(cls):
        return cls(
            event=None,
            name='',
            rate=Decimal('0.00'),
            price_includes_tax=True,
            eu_reverse_charge=False
        )

    def clean(self):
        if self.eu_reverse_charge and not self.home_country:
            raise ValueError(_('You need to set your home country to use the reverse charge feature.'))

    def __str__(self):
        if self.price_includes_tax:
            s = _('incl. {rate}% {name}').format(rate=self.rate, name=self.name)
        else:
            s = _('plus {rate}% {name}').format(rate=self.rate, name=self.name)
        if self.eu_reverse_charge:
            s += ' ({})'.format(_('reverse charge enabled'))
        return str(s)

    def tax(self, base_price, base_price_is='auto'):
        if self.rate == Decimal('0.00'):
            return TaxedPrice(
                net=base_price, gross=base_price, tax=Decimal('0.00'),
                rate=self.rate, name=self.name
            )

        if base_price_is == 'auto':
            if self.price_includes_tax:
                base_price_is = 'gross'
            else:
                base_price_is = 'net'

        if base_price_is == 'gross':
            gross = base_price
            net = gross - round_decimal(base_price * (1 - 100 / (100 + self.rate)))
        elif base_price_is == 'net':
            net = base_price
            gross = round_decimal(net * (1 + self.rate / 100))
        else:
            raise ValueError('Unknown base price type: {}'.format(base_price_is))

        return TaxedPrice(
            net=net, gross=gross, tax=gross - net,
            rate=self.rate, name=self.name
        )

    def is_reverse_charge(self, invoice_address):
        if not self.eu_reverse_charge:
            return False

        if not invoice_address or not invoice_address.country:
            return False

        if str(invoice_address.country) not in EU_COUNTRIES:
            return False

        if invoice_address.country == self.home_country:
            return False

        if invoice_address.is_business and invoice_address.vat_id and invoice_address.vat_id_validated:
            return True

        return False

    def tax_applicable(self, invoice_address):
        if not self.eu_reverse_charge:
            # No reverse charge rules? Always apply VAT!
            return True

        if not invoice_address or not invoice_address.country:
            # No country specified? Always apply VAT!
            return True

        if str(invoice_address.country) not in EU_COUNTRIES:
            # Non-EU country? Never apply VAT!
            return False

        if invoice_address.country == self.home_country:
            # Within same EU country? Always apply VAT!
            return True

        if invoice_address.is_business and invoice_address.vat_id and invoice_address.vat_id_validated:
            # Reverse charge case
            return False

        # Consumer in different EU country / invalid VAT
        return True

    def delete(self, *args, **kwargs):
        super().delete(*args, **kwargs)
        if self.event:
            self.event.cache.clear()

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        if self.event:
            self.event.cache.clear()
