#!/usr/bin/env python
# -*- coding: utf-8 -*-

# System modules
from flask import Response, render_template, make_response, request
from flask_restx import Namespace, Resource, fields
from astropy.time import Time

import ping3
import socket
import requests

# Custom modules
#from config.addresses import ADDRESSES



############################
# REST API
############################

api = Namespace('home', description='Home page API')

@api.route("/ping")
class Ping(Resource):
    """Ping the servers"""

    def get(self):
        """Servers status"""

        def ping(address, timeout=0.1):
            try:
                res = ping3.ping(address, timeout=timeout) 
                return True if res else False
            except Excepion as e:
                res = None
        
        res = {
            "response": {
                "Ascom server":      ping("ascom"),
                "Scientific camera": ping("ccd-sbig"),
                "Meteo station":     ping("meteo"),                
                "TCS server":        ping("grigioni"),
                "All sky camera":    ping("130.251.11.11"),
                "Domotics server":   ping("grafana10.com.diten.unige.it"),
                "Cabinet switch":    ping("fork"),
                "Webcam":            ping("ipcam"),
            },
            "error": []
        }

        return res

    
@api.route("/ports")
class Ports(Resource):
    """Check the ports"""

    def get(self):
        """Port status"""
        
        timeout = 0.1

        def nmap(address, port):
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                connection = socket.create_connection((address,port), timeout=timeout)
                res = True
            except TimeoutError as e:
                res = None
            except ConnectionRefusedError as e:
                res = False
            except socket.gaierror:
                res = False
                
            sock.close()
            return res
            
        res = {
            "response": {
                "Ascom server":      nmap('ascom', 533),
                "Scientific camera": nmap('ccd-sbig', 80),
                "Meteo station":     nmap('meteo', 5001),                
                "TCS server":        nmap('grigioni', 65432),
                "All sky camera":    nmap('130.251.11.11', 80),
                "Domotics server":   nmap('grafana10.com.diten.unige.it', 7070),
                "Cabinet switch":    nmap('fork', 9000),
                "Webcam":            nmap('ipcam', 554),
            },
            "error": []
        }

        return res

    
@api.route("/tests")
class Tests(Resource):
    """Test the servers"""

    def get(self):
        """Response from servers"""
        
        timeout = 0.1
        
        def req(address, timeout=timeout):

            try:
                res = requests.get(address, timeout=timeout)
                return True if res.status_code == 200 else False
            except requests.exceptions.ConnectionError as e:
                return None
            except requests.exceptions.ConnectTimeout as e:
                return  None
            except requests.exceptions.ReadTimeout as e:
                return  None
                
        res = {
            "response": {
                "Ascom server":      req("http://ascom:533/api/v1/switch/0/getswitch?id=1"),
                "Scientific camera": req("http://ccd-sbig/api/VersionNumbers.cgi"),
                "Meteo station":     req("http://fork:5533/api/environment/meteo/clock", timeout=0.1),
                "TCS server":        req("http://fork:5533/api/telescope/coordinates"),
                "All sky camera":    "Boooh",
                "Domotics server":   req("http://fork:5533/api/camera/power"),
                "Cabinet switch":    req("http://fork:5533/api/telescope/power"),
                "Webcam":            req("http://admin:BigBang2021@ipcam/image/jpeg.cgi"),
            },
            "error": []
        }
        
        return res


    
############################
# WEB VIEW
############################

web = Namespace('home', description='Home page')

@web.route("/")
class HomeWeb(Resource):
    """Show the home page"""

    def get(self):
        """Load the home page"""

        ping = Ping()
        res = ping.get()
        ping = res["response"]

        ports = Ports()
        res = ports.get()
        ports = res["response"]

        tests = Tests()
        res = tests.get()
        tests = res["response"]

        print(ports)
        data = [ping, ports, tests]
        
        return make_response(render_template("index.html", data=data))
