import json

import redis
from django.apps import AppConfig
from django.conf import settings

from dashboard.utils import RedisInstances

TEST_MODE = getattr(settings, "TEST_MODE", False)


class DashboardConfig(AppConfig):
    name = 'dashboard'
    verbose_name = "Dashboard application"

    def ready(self):
        if not TEST_MODE:
            redis_instance = redis.StrictRedis(host=getattr(settings, 'REDIS_HOST', 'redis'),
                                               port=getattr(settings, 'REDIS_PORT', 6379), db=0)
            # Incidents by time widget REDIS data setup
            if not redis_instance.exists(RedisInstances.WIDGET_INCS_BY_TIME_DAY):
                redis_instance.set(RedisInstances.WIDGET_INCS_BY_TIME_DAY, json.dumps([0] * 24))
            if not redis_instance.exists(RedisInstances.WIDGET_INCS_BY_TIME_WEEK):
                redis_instance.set(RedisInstances.WIDGET_INCS_BY_TIME_WEEK, json.dumps([0] * 7))
            if not redis_instance.exists(RedisInstances.WIDGET_INCS_BY_TIME_MONTH):
                redis_instance.set(RedisInstances.WIDGET_INCS_BY_TIME_MONTH, json.dumps([0] * 30))
            if not redis_instance.exists(RedisInstances.WIDGET_INCS_BY_TIME_YEAR):
                redis_instance.set(RedisInstances.WIDGET_INCS_BY_TIME_YEAR, json.dumps([0] * 12))
            # Events by time widget REDIS data setup
            if not redis_instance.exists(RedisInstances.WIDGET_EVENTS_BY_TIME_DAY):
                redis_instance.set(RedisInstances.WIDGET_EVENTS_BY_TIME_DAY, json.dumps([0] * 24))
            if not redis_instance.exists(RedisInstances.WIDGET_EVENTS_BY_TIME_WEEK):
                redis_instance.set(RedisInstances.WIDGET_EVENTS_BY_TIME_WEEK, json.dumps([0] * 7))
            if not redis_instance.exists(RedisInstances.WIDGET_EVENTS_BY_TIME_MONTH):
                redis_instance.set(RedisInstances.WIDGET_EVENTS_BY_TIME_MONTH, json.dumps([0] * 30))
            if not redis_instance.exists(RedisInstances.WIDGET_EVENTS_BY_TIME_YEAR):
                redis_instance.set(RedisInstances.WIDGET_EVENTS_BY_TIME_YEAR, json.dumps([0] * 12))
