'''
    Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from celery.schedules import crontab
from pathlib import Path
import os
import yaml


def load_from_file(config_file):
    config_from_file = yaml.safe_load(open(config_file))
    config = Config()

    for entry in config_from_file:
        setattr(config, entry, config_from_file[entry])

    return config


class Config(object):
    DEBUG = False
    TESTING = False
    CREATE_TEST_DATABASE = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    MAIL_SERVER = 'localhost'
    MAIL_PORT = 25
    MAIL_DEFAULT_SENDER = 'GNUBoat Mapservice <gnuboat@engsas.de>'
    MAPSERVICE_WEBCLIENT = '/'
    CELERY_BROKER_URL = 'pyamqp://guest@localhost//'
    CELERY_IMPORTS = ("gnuboat_mapservice.server_impl.tasks")
    CELERY_RESULT_BACKEND = "cache"
    CELERY_CACHE_BACKEND = 'memcached://127.0.0.1:11211'
    # pixel size of tiles to be generated
    TILES_SIZES = [256, 512]
    # Min required data points to get no nodata value in a raster pixel
    TILES_DENSITIES = [10, 100]
    TILES_DENSITY_BASEE = 100
    # How many years are data points allowed to be old, before they are excluded from calculations
    TILES_POINT_AGE_MAX = 10
    # log_process generates tiles for this zoom range. all lower zoomlevels (0 - x) are generated by a periodic task (s. TILES_LITTLE_ZOOM_SCHEDULE option).
    # Those zoomlevels are not rendered by log_process(), because they contain few details, but large areas
    TILES_ZOOM_RANGE = {'minimum': 10, 'maximum': 19}
    # schedule, when to run the job to generate tiles for low zoomlevels (0 - TILES_ZOOM_RANGE['minimum']),
    # As format see https://docs.celeryproject.org/en/stable/userguide/periodic-tasks.html#crontab-schedules
    # If TILES_ZOOM_RANGE['minimum'] = 10 it might need up to 4097 slice jobs per tile_size and density
    TILES_LITTLE_ZOOM_SCHEDULE = crontab(
        0, 0, day_of_month='2'
    )  # execute every second day of each month
    TILES_SKIP_EMPTY = True
    # Maximum size of the raster files generated during tile process in bits
    TILES_RASTER_FILE_SIZE_MAX = 512 * 1024 * 1024 * 8


class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'mysql+mysqldb://gnuboat:gnuboat@localhost/gnuboat_mapservice'
    # TEMPLATE_FOLDER could be used to use own templates residing in TEMPLATE_FOLDER
    TEMPLATE_FOLDER = '/usr/share/gnuboat/templates'


class DevelopmentConfig(Config):
    DEBUG = True
    CREATE_TEST_DATABASE = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db'
    MAPSERVICE_WEBCLIENT = 'http://localhost:8081'
    #SERVER_NAME = 'gnuboat-mapservice:8080'
    HOST = 'localhost'
    SERVER_NAME = 'localhost:8080'
    TILES_CACHE_PATH = os.path.join(Path.home(), '.cache/gnuboat/tiles')
    TILES_SIZES = [256]


class TestingConfig(DevelopmentConfig):
    """
		Configuration for testing purposes only (like tox/nosetests)
	"""
    TESTING = True
    DEBUG = False
    CELERY_BROKER_URL = ''
    SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/gnuboat-ci-tests.db'
