# -*- coding: utf-8 -*-
# Generated by Django 1.11.23 on 2019-12-03 17:51
from __future__ import unicode_literals

from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations
from django.db import models


def migrate_allow_guest_access(apps, schema_editor):
    FacilityDataset = apps.get_model("kolibriauth", "FacilityDataset")
    DeviceSettings = apps.get_model("device", "DeviceSettings")

    try:
        default_facility = DeviceSettings.objects.get().default_facility
        allow_guest_access = default_facility.dataset.allow_guest_access
    except ObjectDoesNotExist:
        allow_guest_access = (
            FacilityDataset.objects.filter(allow_guest_access=False).count() <= 0
        )

    DeviceSettings.objects.update(allow_guest_access=allow_guest_access)


def revert_allow_guest_access(apps, schema_editor):
    FacilityDataset = apps.get_model("kolibriauth", "FacilityDataset")
    DeviceSettings = apps.get_model("device", "DeviceSettings")

    allow_guest_access = (
        DeviceSettings.objects.filter(allow_guest_access=False).count() <= 0
    )

    try:
        default_facility = DeviceSettings.objects.get().default_facility
        default_facility.dataset.allow_guest_access = allow_guest_access
        default_facility.dataset.save()
    except ObjectDoesNotExist:
        FacilityDataset.objects.update(allow_guest_access=allow_guest_access)


class Migration(migrations.Migration):

    dependencies = [
        ("device", "0004_auto_20190306_0553"),
        ("kolibriauth", "0012_facilitydataset_allow_guest_access"),
    ]

    operations = [
        migrations.AddField(
            model_name="devicesettings",
            name="allow_guest_access",
            field=models.BooleanField(default=True),
        ),
        migrations.RunPython(migrate_allow_guest_access, revert_allow_guest_access),
        migrations.AddField(
            model_name="devicesettings",
            name="allow_learner_unassigned_resource_access",
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name="devicesettings",
            name="allow_peer_unlisted_channel_import",
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name="devicesettings",
            name="landing_page",
            field=models.CharField(
                choices=[("sign-in", "Sign-in page"), ("learn", "Learn page")],
                default="sign-in",
                max_length=7,
            ),
        ),
    ]
