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

# System modules
from utils.logger import log
import requests
import socket
import sys


def request_errors(func):
    def inner_function(*args, **kwargs):
        this = args[0]
        try:
            this.error = []
            res = func(*args, **kwargs)
            return res

        except requests.exceptions.HTTPError as e:
            res = e.response

            if res.status_code == 400:
                #'0x80001005\r\nThe Error Message\r\n'
                msg = "Device error"
                log.error(msg)
                this.error.append(res.text.replace("\r\n", " "))
            elif res.status_code == 500:
                log.error(e.reason)  # Device error
                log.error(e.text)
                this.error.append(e.reason)
                this.error.append(e.text)
            else:  # 404
                log.error(e.reason)  # Not found
                this.error.append(e.reason)

            return

        except requests.exceptions.ConnectTimeout as e:
            msg = "Connection timeout!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            return
        
        except requests.exceptions.Timeout as e:
            msg = "Timeout!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            return

        except requests.exceptions.TooManyRedirects as e:
            msg = "Bad request!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            return

        except ConnectionRefusedError as e:
            msg = "Connection refused!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            return

        except requests.exceptions.ConnectionError as e:
            msg = "Connection error!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            return []

        except AttributeError as e:
            msg = "No data!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            return

        except requests.exceptions.RequestException as e:
            msg = "Generic request exception!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            raise SystemExit(e)

        except Exception as e:
            msg = "Not handled error!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(str(e))
            raise SystemExit(e)

    return inner_function


def telnet_errors(func):
    def inner_function(*args, **kwargs):
        this = args[0]

        try:
            this.error = []
            return func(*args, **kwargs)

        except socket.gaierror as e:
            msg = "Server not found!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(e)
            return

        except socket.timeout as e:
            msg = "Server timeout!"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(e)
            return

        except AttributeError as e:
            msg = "No attribute with this name"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(e)
            return

        except Exception as e:
            msg = "Not handled error"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(e)
            raise SystemExit(e)

    return inner_function


def ascom_errors(func):
    def inner_function(*args, **kwargs):
        this = args[0]

        try:
            this.error = []
            return func(*args, **kwargs)

        except Exception as e:
            msg = "Not handled error"
            log.error(msg)
            log.error(e)
            this.error.append(msg)
            this.error.append(e)
            raise SystemExit(e)

    return inner_function
