# Generated by Django 4.2.7 on 2024-01-01 18:44
import django
from django.db import migrations, models
from django_scopes import scopes_disabled

TANDOOR = 'TANDOOR'
TANDOOR_DARK = 'TANDOOR_DARK'
BOOTSTRAP = 'BOOTSTRAP'
DARKLY = 'DARKLY'
FLATLY = 'FLATLY'
SUPERHERO = 'SUPERHERO'

PRIMARY = 'PRIMARY'
SECONDARY = 'SECONDARY'
SUCCESS = 'SUCCESS'
INFO = 'INFO'
WARNING = 'WARNING'
DANGER = 'DANGER'
LIGHT = 'LIGHT'
DARK = 'DARK'


# ['light', 'warning', 'info', 'success'] --> light (theming_tags L45)
def get_nav_bg_color(theme, nav_color):
    if theme == TANDOOR:  # primary not actually primary color but override existed before update, same for dark
        return {PRIMARY: '#ddbf86', SECONDARY: '#b55e4f', SUCCESS: '#82aa8b', INFO: '#385f84', WARNING: '#eaaa21', DANGER: '#a7240e', LIGHT: '#cfd5cd', DARK: '#221e1e'}[nav_color]
    if theme == TANDOOR_DARK:
        return {PRIMARY: '#ddbf86', SECONDARY: '#b55e4f', SUCCESS: '#82aa8b', INFO: '#385f84', WARNING: '#eaaa21', DANGER: '#a7240e', LIGHT: '#cfd5cd', DARK: '#221e1e'}[nav_color]
    if theme == BOOTSTRAP:
        return {PRIMARY: '#007bff', SECONDARY: '#6c757d', SUCCESS: '#28a745', INFO: '#17a2b8', WARNING: '#ffc107', DANGER: '#dc3545', LIGHT: '#f8f9fa', DARK: '#343a40'}[nav_color]
    if theme == DARKLY:
        return {PRIMARY: '#375a7f', SECONDARY: '#444', SUCCESS: '#00bc8c', INFO: '#3498DB', WARNING: '#F39C12', DANGER: '#E74C3C', LIGHT: '#999', DARK: '#303030'}[nav_color]
    if theme == FLATLY:
        return {PRIMARY: '#2C3E50', SECONDARY: '#95a5a6', SUCCESS: '#18BC9C', INFO: '#3498DB', WARNING: '#F39C12', DANGER: '#E74C3C', LIGHT: '#ecf0f1', DARK: '#7b8a8b'}[nav_color]
    if theme == SUPERHERO:
        return {PRIMARY: '#DF691A', SECONDARY: '#4E5D6C', SUCCESS: '#5cb85c', INFO: '#5bc0de', WARNING: '#f0ad4e', DANGER: '#d9534f', LIGHT: '#abb6c2', DARK: '#4E5D6C'}[nav_color]


def get_nav_text_color(theme, nav_color):
    if theme == TANDOOR:
        return {PRIMARY: DARK, SECONDARY: DARK, SUCCESS: DARK, INFO: DARK, WARNING: DARK, DANGER: DARK, LIGHT: DARK, DARK: DARK}[nav_color]
    if theme == TANDOOR_DARK:
        return {PRIMARY: DARK, SECONDARY: DARK, SUCCESS: DARK, INFO: DARK, WARNING: DARK, DANGER: DARK, LIGHT: DARK, DARK: DARK}[nav_color]
    if theme == BOOTSTRAP:
        return {PRIMARY: DARK, SECONDARY: DARK, SUCCESS: DARK, INFO: DARK, WARNING: DARK, DANGER: DARK, LIGHT: DARK, DARK: DARK}[nav_color]
    if theme == DARKLY:
        return {PRIMARY: DARK, SECONDARY: DARK, SUCCESS: DARK, INFO: DARK, WARNING: DARK, DANGER: DARK, LIGHT: DARK, DARK: DARK}[nav_color]
    if theme == FLATLY:
        return {PRIMARY: DARK, SECONDARY: DARK, SUCCESS: LIGHT, INFO: LIGHT, WARNING: LIGHT, DANGER: DARK, LIGHT: LIGHT, DARK: DARK}[nav_color]
    if theme == SUPERHERO:
        return {PRIMARY: DARK, SECONDARY: DARK, SUCCESS: LIGHT, INFO: LIGHT, WARNING: LIGHT, DANGER: DARK, LIGHT: LIGHT, DARK: DARK}[nav_color]


def get_current_colors(apps, schema_editor):
    with scopes_disabled():
        # in case any food had a non digit fdc ID before this migration, remove it
        UserPreference = apps.get_model('cookbook', 'UserPreference')

        update_ups = []
        for up in UserPreference.objects.all():
            if up.theme != TANDOOR or up.nav_color != PRIMARY:
                up.nav_bg_color = get_nav_bg_color(up.theme, up.nav_color)
                up.nav_text_color = get_nav_text_color(up.theme, up.nav_color)
                up.nav_show_logo = (up.theme == TANDOOR or up.theme == TANDOOR_DARK)
                update_ups.append(up)

        UserPreference.objects.bulk_update(update_ups, ['nav_bg_color', 'nav_text_color', 'nav_show_logo'])


class Migration(migrations.Migration):
    dependencies = [
        ('cookbook', '0205_alter_food_fdc_id_alter_propertytype_fdc_id'),
    ]

    operations = [
        migrations.RenameField(
            model_name='userpreference',
            old_name='sticky_navbar',
            new_name='nav_sticky',
        ),
        migrations.AddField(
            model_name='userpreference',
            name='nav_bg_color',
            field=models.CharField(default='#ddbf86', max_length=8),
        ),
        migrations.AddField(
            model_name='userpreference',
            name='nav_text_color',
            field=models.CharField(choices=[('LIGHT', 'Light'), ('DARK', 'Dark')], default='DARK', max_length=16),
        ),
        migrations.AddField(
            model_name='userpreference',
            name='nav_show_logo',
            field=models.BooleanField(default=True),
        ),
        migrations.RunPython(get_current_colors),
        migrations.RemoveField(
            model_name='userpreference',
            name='nav_color',
        ),
        migrations.AddField(
            model_name='space',
            name='nav_bg_color',
            field=models.CharField(blank=True, default='', max_length=8),
        ),
        migrations.AddField(
            model_name='space',
            name='nav_logo',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='space_nav_logo', to='cookbook.userfile'),
        ),
        migrations.AddField(
            model_name='space',
            name='nav_text_color',
            field=models.CharField(choices=[('BLANK', '-------'), ('LIGHT', 'Light'), ('DARK', 'Dark')], default='BLANK', max_length=16),
        ),
        migrations.AddField(
            model_name='space',
            name='space_theme',
            field=models.CharField(choices=[('BLANK', '-------'), ('TANDOOR', 'Tandoor'), ('BOOTSTRAP', 'Bootstrap'), ('DARKLY', 'Darkly'), ('FLATLY', 'Flatly'), ('SUPERHERO', 'Superhero'), ('TANDOOR_DARK', 'Tandoor Dark (INCOMPLETE)')],
                                   default='BLANK',
                                   max_length=128),
        ),
        migrations.AddField(
            model_name='space',
            name='custom_space_theme',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='space_theme', to='cookbook.userfile'),
        ),
    ]
