# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-06-16 12:23
#
# This one merges the whodid from daaaaavvvvvve to dave
#
#
################################################################################


from __future__ import unicode_literals

from django.db import migrations
from time import sleep
from ..models import UserDid
from django.db.models import Count

def do_stuff(apps, schemd_editor) -> None:
    userds = UserDid.objects.annotate(Count('chars_changed'))
    Calligraphy = apps.get_model('calligraphy', 'UserDid').objects
    Characters = apps.get_model('calligraphy', 'Character').objects
    Pages = apps.get_model('calligraphy', 'Page').objects
    old_dave = Calligraphy.filter(user_supplied=9)[0]
    new_dave = Calligraphy.filter(user_supplied=1)[0]
    print()
    print()
    for userd in userds:
        print(userd.user_supplied.username + " : " + str(userd.chars_changed__count))
    print()
    pages = []
    for page in old_dave.pages_changed.all():
        pages.append(Pages.get(id=page.id))
    
    chars = []
    for char in old_dave.chars_changed.all():
        myChar = Characters.get(id=char.id)
        if myChar.supplied_by.id == 9:
            myChar.supplied_by.id = 1
            myChar.save()
        chars.append(myChar)
    
    for page in pages:
        new_dave.pages_changed.add(page)
        old_dave.pages_changed.remove(page)
    for char in chars:
        new_dave.chars_changed.add(char)
        old_dave.chars_changed.remove(char)

        
    userds2 = UserDid.objects.annotate(Count('chars_changed'))
    print()
    print()
    for userd in userds2:
        print(userd.user_supplied.username + " : " + str(userd.chars_changed__count))
    print()

    print()


class Migration(migrations.Migration):

    dependencies = [
        ('calligraphy', '0104_reasonchardeleted'),
    ]

    operations = [ migrations.RunPython(do_stuff)
    ]
