import pytest

import basket.product as product
import basket.offer as offer

@pytest.fixture
def prod_offer():
    offer_def = {'id': 1,
                 'title': 'Apples 10% off',
                 'qualifying_product': 'apples',
                 'qualifying_qty': 1,
                 'discounted_product': 'apples',
                 'discount_percent': 10}
    return offer.Offer(offer_def)


@pytest.mark.parametrize('name', ['apples', 'Apples', 'appLeS'])
@pytest.mark.parametrize('price', [100, 100.0, '100'])
@pytest.mark.parametrize('unit', ['bag', 'Bag', 'bAg'])
def test_prod(name, price, unit):
    prod = product.Product(name, price, unit)
    assert prod.name == 'apples'
    assert prod.price == 100
    assert prod.unit == 'bag'


def test_apply_offer(prod_offer):
    prod = product.Product('apples', 100, 'bag')
    prod.apply_offer(prod_offer)
    assert prod._offer is prod_offer
    assert prod.has_offer is True
    assert prod.discounted_price == 90
    assert prod.discount_amount == 10
    assert 'Apples 10% off: -10p' in prod.discount_message


def test_apply_offer_pounds(prod_offer):
    prod = product.Product('apples', 100, 'bag')
    prod_offer.discount_percent = 100
    prod.apply_offer(prod_offer)
    assert prod.discounted_price == 0
    assert prod.discount_amount == 100
    assert 'Apples 10% off: -£1.00' in prod.discount_message


def test_clear_offer(prod_offer):
    prod = product.Product('apples', 100, 'bag')
    prod.apply_offer(prod_offer)
    assert prod._offer is prod_offer
    prod.clear_offer()
    assert not prod.has_offer
    assert prod.discounted_price == 100
    assert prod.discount_amount == 0
    assert prod.discount_message is None
