'''
    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/>.
'''

import os
import pytz
import flask

from ..server_impl.database.Model import db, User, ApiKey, Water, OsmType, Gauge, Device, Log, Queue, ProcessingStatus, db_setup
from ..server_impl.demo import DbSetup as demoDbSetup
from ..server_impl.tasks.tasks import log_process


class DatabaseContent:
    def __init__(self):
        test_user = None
        test_water = None
        test_gauge = None
        test_device = None
        test_log = None

    def setup(self):
        db_setup()

        print("CREATING DATABASE TEST CONTENT...")
        with db.session.no_autoflush:
            self.test_user = User.factory(
                'autotests', 'autotests', 'auto@example.org'
            )
            self.test_user.activated = True
            self.test_user.first_name = 'Autotests'
            self.test_user.license_accepted = True
            db.session.add(self.test_user)
            db.session.commit()

            user_activation = User.factory(
                'test_user_activation', 'test_user_activation',
                'test@example.org'
            )
            user_activation.activated = False
            user_activation.activation_code = 'dac9630aec642a428cd73f4be0a03569'
            user_activation.first_name = 'Autotests'
            user_activation.license_accepted = True
            db.session.add(user_activation)
            db.session.commit()

            db.session.add(ApiKey(user=self.test_user, key='test_key'))
            db.session.add(ApiKey(user=self.test_user, key='test_key_delete'))
            db.session.commit()

            self.test_water = Water(
                name='Test Water',
                manager=self.test_user,
                osm_id=1756854,
                osm_type=OsmType.Relation
            )
            db.session.add(self.test_water)
            db.session.commit()

            self.test_gauge = Gauge(
                name='test_gauge',
                manager=self.test_user,
                water=self.test_water,
                location='SRID=4326;POINT(-90.136111 29.934722)',
                reference_water_level_gauge_height=3
            )
            db.session.add(self.test_gauge)
            db.session.commit()

            self.test_device = Device(
                name='Test Device', owner=self.test_user, sensor_depth=0.2
            )
            db.session.add(self.test_device)
            db.session.commit()

            log_file = os.path.join(
                os.path.dirname(__file__), 'data/nmea0183-2017-12-09.log'
            )
            self.test_log = demoDbSetup._sample_log_add(
                self.test_water, self.test_gauge, 0, self.test_device,
                self.test_user, log_file
            )

            print("...Finished")
