# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-01-08 12:46
from __future__ import unicode_literals

from django.db import migrations, models

def set_holiday_types(apps, schema_editor):
    Holiday = apps.get_model('holidays', 'Holiday')
    StaticHoliday = apps.get_model('holidays', 'StaticHoliday')
    NthXDayHoliday = apps.get_model('holidays', 'NthXDayHoliday')
    NthXDayAfterHoliday = apps.get_model('holidays', 'NthXDayAfterHoliday')
    CustomHoliday = apps.get_model('holidays', 'CustomHoliday')

    print('\nConverting old holidays to new format...')
    from holidays.models import Holiday as HolidayBase

    for s_holiday in StaticHoliday.objects.all():
        holiday = Holiday.objects.get(id=s_holiday.holiday_ptr_id)
        holiday.day = s_holiday.day_old
        holiday.type = HolidayBase.STATIC
        holiday.save()

    for nx_holiday in NthXDayHoliday.objects.all():
        holiday = Holiday.objects.get(id=nx_holiday.holiday_ptr_id)
        holiday.nth = nx_holiday.nth_old
        holiday.day_of_week = nx_holiday.day_of_week_old
        holiday.type = HolidayBase.NXDAY
        holiday.save()

    for nxa_holiday in NthXDayAfterHoliday.objects.all():
        holiday = Holiday.objects.get(id=nxa_holiday.holiday_ptr_id)
        holiday.nth = nxa_holiday.nth_old
        holiday.day_of_week = nxa_holiday.day_of_week_old
        holiday.after_nth = nxa_holiday.after_nth_old
        holiday.after_day_of_week = nxa_holiday.after_day_of_week_old
        holiday.type = HolidayBase.NXADAY
        holiday.save()

    for c_holiday in CustomHoliday.objects.all():
        holiday = Holiday.objects.get(id=c_holiday.holiday_ptr_id)
        holiday.day = c_holiday.day_old
        holiday.year = c_holiday.year_old
        holiday.type = HolidayBase.CUSTOM
        holiday.save()

    for holiday in Holiday.objects.filter(type=None):
        print('WARNING: Removing orphaned holiday', holiday.name)
        holiday.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('holidays', '0001_initial'),
    ]

    operations = [
        migrations.RenameField(
            model_name='staticholiday',
            old_name='day',
            new_name='day_old',
        ),
        migrations.RenameField(
            model_name='nthxdayholiday',
            old_name='nth',
            new_name='nth_old',
        ),
        migrations.RenameField(
            model_name='nthxdayholiday',
            old_name='day_of_week',
            new_name='day_of_week_old',
        ),
        migrations.RenameField(
            model_name='nthxdayafterholiday',
            old_name='nth',
            new_name='nth_old',
        ),
        migrations.RenameField(
            model_name='nthxdayafterholiday',
            old_name='day_of_week',
            new_name='day_of_week_old',
        ),
        migrations.RenameField(
            model_name='nthxdayafterholiday',
            old_name='after_nth',
            new_name='after_nth_old',
        ),
        migrations.RenameField(
            model_name='nthxdayafterholiday',
            old_name='after_day_of_week',
            new_name='after_day_of_week_old',
        ),
        migrations.RenameField(
            model_name='customholiday',
            old_name='day',
            new_name='day_old',
        ),
        migrations.RenameField(
            model_name='customholiday',
            old_name='year',
            new_name='year_old',
        ),
        migrations.AddField(
            model_name='holiday',
            name='after_day_of_week',
            field=models.PositiveSmallIntegerField(blank=True, choices=[(0, 'Monday'), (1, 'Tuesday'), (2, 'Wednesday'), (3, 'Thursday'), (4, 'Friday'), (5, 'Saturday'), (6, 'Sunday')], null=True),
        ),
        migrations.AddField(
            model_name='holiday',
            name='after_nth',
            field=models.PositiveSmallIntegerField(blank=True, choices=[(1, 'First'), (2, 'Second'), (3, 'Third'), (4, 'Fourth'), (5, 'Fifth')], null=True),
        ),
        migrations.AddField(
            model_name='holiday',
            name='day',
            field=models.PositiveSmallIntegerField(blank=True, choices=[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 16), (17, 17), (18, 18), (19, 19), (20, 20), (21, 21), (22, 22), (23, 23), (24, 24), (25, 25), (26, 26), (27, 27), (28, 28), (29, 29), (30, 30), (31, 31)], null=True),
        ),
        migrations.AddField(
            model_name='holiday',
            name='day_of_week',
            field=models.PositiveSmallIntegerField(blank=True, choices=[(0, 'Monday'), (1, 'Tuesday'), (2, 'Wednesday'), (3, 'Thursday'), (4, 'Friday'), (5, 'Saturday'), (6, 'Sunday')], null=True),
        ),
        migrations.AddField(
            model_name='holiday',
            name='nth',
            field=models.PositiveSmallIntegerField(blank=True, choices=[(1, 'First'), (2, 'Second'), (3, 'Third'), (4, 'Fourth'), (5, 'Fifth')], null=True),
        ),
        migrations.AddField(
            model_name='holiday',
            name='year',
            field=models.PositiveSmallIntegerField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name='holiday',
            name='type',
            field=models.CharField(choices=[('STATIC', 'Static Holiday'), ('NXDAY', 'Nth X Day Holiday'), ('NXADAY', 'Nth X Day After Holiday'), ('CUSTOM', 'Custom Holiday')], null=True, max_length=6),
        ),
        migrations.AddField(
            model_name='holiday',
            name='offset_weekend',
            field=models.BooleanField(default=False, help_text='Offset the date if it falls on a weekend to a weekday?'),
        ),
        migrations.RunPython(set_holiday_types),
        migrations.AlterField(
            model_name='holiday',
            name='type',
            field=models.CharField(choices=[('STATIC', 'Static Holiday'), ('NXDAY', 'Nth X Day Holiday'), ('NXADAY', 'Nth X Day After Holiday'), ('CUSTOM', 'Custom Holiday')], db_index=True, max_length=6),
        ),
        migrations.DeleteModel(
            name='CustomHoliday',
        ),
        migrations.DeleteModel(
            name='NthXDayAfterHoliday',
        ),
        migrations.DeleteModel(
            name='NthXDayHoliday',
        ),
        migrations.DeleteModel(
            name='StaticHoliday',
        ),
        migrations.CreateModel(
            name='CustomHoliday',
            fields=[
            ],
            options={
                'proxy': True,
                'indexes': [],
            },
            bases=('holidays.holiday',),
        ),
        migrations.CreateModel(
            name='NthXDayAfterHoliday',
            fields=[
            ],
            options={
                'proxy': True,
                'indexes': [],
            },
            bases=('holidays.holiday',),
        ),
        migrations.CreateModel(
            name='NthXDayHoliday',
            fields=[
            ],
            options={
                'proxy': True,
                'indexes': [],
            },
            bases=('holidays.holiday',),
        ),
        migrations.CreateModel(
            name='StaticHoliday',
            fields=[
            ],
            options={
                'proxy': True,
                'indexes': [],
            },
            bases=('holidays.holiday',),
        ),
    ]
