"""
Web Interface

Regelmäßiges Abfragen von Daten im Internet
 - NAVTEX
    - DWD
 - Pegelstände
    - pegelonline
    - WSV
 - Wetterinformationen 

"""
import os
import http.client
import ssl
import re
import sqlite3
import datetime
from gi.repository import GLib

class WebInterface():

    def __init__(self, logger, cfg):
        self.log = logger
        dbpath = os.path.join(cfg['histpath'], "tidedata.db")
        try:
            self.conn = sqlite3.connect(dbpath)
            self.cur = self.conn.cursor()
        except:
            self.log.error(f"Failed to open tide database: {dbpath}")
            return
        # Datenbank erstellen wenn nicht vorhanden
        sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='station'"
        self.cur.execute(sql)
        if self.cur.fetchone() == None:
            sql = ("CREATE TABLE IF NOT EXISTS station ("
                   "uuid TEXT PRIMARY KEY NOT NULL,"
                   "name TEXT"
                  )
            self.cur.execute(sql)
            self.log.info(f"Created tide database: {dbpath}")

        # In der Konfiguration werden Minuten angegeben
        #GLib.timeout_add_seconds(cfg['tide_refresh'] * 60, self.on_timer)
        GLib.timeout_add_seconds(60 * 60, self.on_timer)
        self.running = True

    def __del__(self):
        self.conn.close()

    def on_timer(self):
        """
        Data handling
        """
        self.refresh()
        self.housekeeping()
        return True

    def bsh_get_data(self, local=False):
        """
        Webseite auslesen
        """
        if local:
            # Für Tests um nicht permanent die Webseite abzufragen
            with open("DE__714P.json", "r") as fh:
                content = fh.read()
        else:
            ssl_context = ssl.create_default_context()
            conn = http.client.HTTPSConnection("wasserstand-nordsee.bsh.de", 443, context=ssl_context)
            url = "https://wasserstand-nordsee.bsh.de/data/DE__714P.json"

            try:
                conn.request("GET", url)
                response = conn.getresponse()
                if response.status == 200:
                    content = response.read().decode()
                else:
                    print(f"Error: {response.status}")
                    return []
            except http.client.HTTPException as e:
                self.log.warning(f"HTTP error occurred: {e}")
                return []
            except ssl.SSLError as ssl_error:
                self.log.warning(f"SSL error occurred: {ssl_error}")
                return []

        return content

    def refresh(self):
        self.log.info("Data refresh")
        data = self.bsh_get_data(True)
        """
        sql = "INSERT INTO message (msgid, station, content) VALUES (?, ?, ?)"
        for m in messages:
            msg = self.parse_message(m)
            self.cur.execute("SELECT COUNT(*) FROM message WHERE msgid=?", (msg['id'],))
            result = self.cur.fetchone()
            if result[0] == 0:
                self.log.debug(f"NAVTEX: insert new message '{msg['id']}'")
                self.cur.execute(sql, (msg['id'], msg['station'], m))
                self.conn.commit()
        """

    def housekeeping(self):
        self.log.info("Housekeeping")
