import json
from datetime import timedelta

import pytest
from django.utils.timezone import now

from pretix.base.models import (
    Event, EventPermission, Item, Order, OrderPosition, Organizer, Quota, User,
)
from pretix.plugins.banktransfer.models import BankImportJob, BankTransaction


@pytest.fixture
def env():
    o = Organizer.objects.create(name='Dummy', slug='dummy')
    event = Event.objects.create(
        organizer=o, name='Dummy', slug='dummy',
        date_from=now(), plugins='pretix.plugins.banktransfer'
    )
    user = User.objects.create_user('dummy@dummy.dummy', 'dummy')
    EventPermission.objects.create(user=user, event=event)
    o1 = Order.objects.create(
        code='1Z3AS', event=event,
        status=Order.STATUS_PENDING,
        datetime=now(), expires=now() + timedelta(days=10),
        total=23, payment_provider='banktransfer'
    )
    o2 = Order.objects.create(
        code='6789Z', event=event,
        status=Order.STATUS_CANCELED,
        datetime=now(), expires=now() + timedelta(days=10),
        total=23, payment_provider='banktransfer'
    )
    quota = Quota.objects.create(name="Test", size=2, event=event)
    item1 = Item.objects.create(event=event, name="Ticket", default_price=23)
    quota.items.add(item1)
    OrderPosition.objects.create(order=o1, item=item1, variation=None, price=23)
    return event, user, o1, o2


@pytest.mark.django_db
def test_discard(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_ERROR,
                                           amount=0, date='unknown')
    client.login(email='dummy@dummy.dummy', password='dummy')
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'discard',
    }).content.decode('utf-8'))
    assert r['status'] == 'ok'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_DISCARDED
    assert trans.payer == ''


@pytest.mark.django_db
def test_accept_wrong_amount(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_INVALID,
                                           amount=12, date='unknown', order=env[2])
    client.login(email='dummy@dummy.dummy', password='dummy')
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'accept',
    }).content.decode('utf-8'))
    assert r['status'] == 'ok'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_VALID
    env[2].refresh_from_db()
    assert env[2].status == Order.STATUS_PAID


@pytest.mark.django_db
def test_assign_order(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_NOMATCH,
                                           amount=23, date='unknown')
    client.login(email='dummy@dummy.dummy', password='dummy')
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'assign:{}'.format(env[2].code),
    }).content.decode('utf-8'))
    assert r['status'] == 'ok'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_VALID
    env[2].refresh_from_db()
    assert env[2].status == Order.STATUS_PAID


@pytest.mark.django_db
def test_assign_order_unknown(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_NOMATCH,
                                           amount=23, date='unknown')
    client.login(email='dummy@dummy.dummy', password='dummy')
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'assign:FOO'
    }).content.decode('utf-8'))
    assert r['status'] == 'error'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_NOMATCH


@pytest.mark.django_db
def test_assign_order_amount_incorrect(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_NOMATCH,
                                           amount=12, date='unknown')
    client.login(email='dummy@dummy.dummy', password='dummy')
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'assign:{}'.format(env[2].code)
    }).content.decode('utf-8'))
    assert r['status'] == 'error'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_NOMATCH


@pytest.mark.django_db
def test_comment(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_NOMATCH,
                                           amount=12, date='unknown')
    client.login(email='dummy@dummy.dummy', password='dummy')
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'comment:This is my comment'.format(env[2].code)
    }).content.decode('utf-8'))
    assert r['status'] == 'ok'
    trans.refresh_from_db()
    assert trans.comment == 'This is my comment'
    assert trans.state == BankTransaction.STATE_NOMATCH


@pytest.mark.django_db
def test_retry_success(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_DUPLICATE,
                                           amount=23, date='unknown', order=env[3])
    client.login(email='dummy@dummy.dummy', password='dummy')
    env[3].status = Order.STATUS_PENDING
    env[3].save()
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'retry',
    }).content.decode('utf-8'))
    assert r['status'] == 'ok'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_VALID
    env[3].refresh_from_db()
    assert env[3].status == Order.STATUS_PAID


@pytest.mark.django_db
def test_retry_canceled(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_ERROR,
                                           amount=23, date='unknown', order=env[3])
    client.login(email='dummy@dummy.dummy', password='dummy')
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'retry',
    }).content.decode('utf-8'))
    assert r['status'] == 'error'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_ERROR
    env[3].refresh_from_db()
    assert env[3].status == Order.STATUS_CANCELED


@pytest.mark.django_db
def test_retry_refunded(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_ERROR,
                                           amount=23, date='unknown', order=env[3])
    client.login(email='dummy@dummy.dummy', password='dummy')
    env[3].status = Order.STATUS_REFUNDED
    env[3].save()
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'retry',
    }).content.decode('utf-8'))
    assert r['status'] == 'error'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_ERROR
    env[3].refresh_from_db()
    assert env[3].status == Order.STATUS_REFUNDED


@pytest.mark.django_db
def test_retry_paid(env, client):
    job = BankImportJob.objects.create(event=env[0])
    trans = BankTransaction.objects.create(event=env[0], import_job=job, payer='Foo',
                                           state=BankTransaction.STATE_ERROR,
                                           amount=23, date='unknown', order=env[3])
    client.login(email='dummy@dummy.dummy', password='dummy')
    env[3].status = Order.STATUS_PAID
    env[3].save()
    r = json.loads(client.post('/control/event/{}/{}/banktransfer/action/'.format(env[0].organizer.slug, env[0].slug), {
        'action_{}'.format(trans.pk): 'retry',
    }).content.decode('utf-8'))
    assert r['status'] == 'error'
    trans.refresh_from_db()
    assert trans.state == BankTransaction.STATE_ERROR
    env[3].refresh_from_db()
    assert env[3].status == Order.STATUS_PAID
