# Generated by Django 2.0.1 on 2018-01-18 03:28

from django.db import migrations


def load_sample_data(apps, schema_editor):
    db_alias = schema_editor.connection.alias

    Thing = apps.get_model('campaign', 'Thing')
    ThingType = apps.get_model('campaign', 'ThingType')
    Attribute = apps.get_model('campaign', 'Attribute')
    AttributeValue = apps.get_model('campaign', 'AttributeValue')

    location = ThingType.objects.get(name='Location')
    faction = ThingType.objects.get(name='Faction')
    npc = ThingType.objects.get(name='NPC')

    Thing.objects.using(db_alias).bulk_create([
        Thing(
            name='Material Plane',
            description='The mundane plane, where mortal life unfolds at the centre of the cosmic wheel.',
            thing_type=location
        ),
        Thing(
            name='Drakesbone',
            description='A country fresh from civil war, full of feuding barons and a corrupt navy.',
            thing_type=location
        ),
        Thing(
            name='Port Imar',
            description='A walled port city overflowing with trade. Notoriously hostile to spellcasters, its Watchers hunt and kill any supernatural threat.',
            thing_type=location
        ),
        Thing(
            name='Watchers',
            description='The Watchers guard ruthlessly against supernatural threats, using their silvered weapons to slay fiends and lycanthropes and their special training to stop spellcasters.',
            thing_type=faction
        ),
        Thing(
            name='Baroness Alonsa Vannastye',
            description='The wealthy leader of Port Imar, where she rules with a strict but fair hand.',
            thing_type=npc
        ),
        Thing(
            name='Sir Katya Fangtaker',
            description='The battle-scarred leader of the Watchers, notorious for her bloody victory against a coven of vampires in Port Imar\'s sewers.',
            thing_type=npc
        )
    ])

    material_plane = Thing.objects.using(db_alias).get(name='Material Plane')
    drakesbone = Thing.objects.using(db_alias).get(name='Drakesbone')
    port_imar = Thing.objects.using(db_alias).get(name='Port Imar')
    watchers = Thing.objects.using(db_alias).get(name='Watchers')
    baroness_alonsa_vannastye = Thing.objects.using(db_alias).get(name='Baroness Alonsa Vannastye')
    sir_katya_fangtaker = Thing.objects.using(db_alias).get(name='Sir Katya Fangtaker')

    material_plane.children.add(drakesbone)
    material_plane.save()

    drakesbone.children.add(port_imar)
    drakesbone.save()

    port_imar.children.add(watchers)
    port_imar.children.add(baroness_alonsa_vannastye)
    port_imar.save()

    watchers.children.add(sir_katya_fangtaker)
    watchers.save()

    faction_leader = Attribute.objects.using(db_alias).get(thing_type=faction, name='Leader')
    faction_attitude = Attribute.objects.using(db_alias).get(thing_type=faction, name='Attitude')
    faction_power = Attribute.objects.using(db_alias).get(thing_type=faction, name='Power')
    faction_reach = Attribute.objects.using(db_alias).get(thing_type=faction, name='Reach')
    npc_race = Attribute.objects.using(db_alias).get(thing_type=npc, name='Race')
    npc_attitude = Attribute.objects.using(db_alias).get(thing_type=npc, name='Attitude')
    npc_occupation = Attribute.objects.using(db_alias).get(thing_type=npc, name='Occupation')
    npc_link = Attribute.objects.using(db_alias).get(thing_type=npc, name='Link')

    AttributeValue.objects.using(db_alias).bulk_create([
        AttributeValue(thing=watchers, attribute=faction_leader, value='Sir Katya Fangtaker'),
        AttributeValue(thing=watchers, attribute=faction_attitude, value='Neutral'),
        AttributeValue(thing=watchers, attribute=faction_power, value='High'),
        AttributeValue(thing=watchers, attribute=faction_reach, value='Local'),
        AttributeValue(thing=baroness_alonsa_vannastye, attribute=npc_race, value='Human'),
        AttributeValue(thing=baroness_alonsa_vannastye, attribute=npc_attitude, value='Neutral'),
        AttributeValue(thing=baroness_alonsa_vannastye, attribute=npc_occupation, value='Ruler of Port Imar'),
        AttributeValue(thing=baroness_alonsa_vannastye, attribute=npc_link, value='https://www.dndbeyond.com/monsters/noble'),
        AttributeValue(thing=sir_katya_fangtaker, attribute=npc_race, value='Tiefling'),
        AttributeValue(thing=sir_katya_fangtaker, attribute=npc_attitude, value='Neutral'),
        AttributeValue(thing=sir_katya_fangtaker, attribute=npc_occupation, value='Leader of the Watchers'),
        AttributeValue(thing=sir_katya_fangtaker, attribute=npc_link, value='https://www.dndbeyond.com/monsters/59485-watcher')
    ])


def delete_sample_data(apps, schema_editor):
    db_alias = schema_editor.connection.alias

    Thing = apps.get_model('campaign', 'Thing')
    Thing.objects.using(db_alias).filter(name='Material Plane').delete()
    Thing.objects.using(db_alias).filter(name='Drakesbone').delete()
    Thing.objects.using(db_alias).filter(name='Port Imar').delete()
    Thing.objects.using(db_alias).filter(name='Watchers').delete()
    Thing.objects.using(db_alias).filter(name='Baroness Alonsa Vannastye').delete()
    Thing.objects.using(db_alias).filter(name='Sir Katya Fangtaker').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('campaign', '0009_initial_data'),
    ]

    operations = [
        migrations.RunPython(load_sample_data, delete_sample_data)
    ]
