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

# System modules


# Custom modules
from utils.logger import log
from devices import cam, tel
from config.constants import camera_state, filter_state


class BaseTemplate:
    def __init__(self):
        #self.name = type(self).__name__
        self.name = __name__
        self.description = "Other templates inherit from this"
        self.error = []

    # Il decoratore delle eccezioni NON va qui

    def content(self):
        '''
        Here the inherited template will be developed
        '''
        return

    # Il decoratore delle eccezioni va qui
    def run(self, params, extra={}):
        log.info(f"Running template {self.name}")

        if not params:
            log.warning(f"Params are empty: {params}")
        else:
            log.debug(f"Params:")
            for key, value in params.items():
                log.debug(f"{key:>10}  {str(value):<6}")

        log.info(f"Running content {self.name}")
        self.content(params)

        log.info(f"Template {self.name} ended")

        return

    def abort(self):
        msg = f"Aborting template {self.name}"
        log.error(msg)
        self.error.append(msg)

        try:
            self.error = []
            if (cam.state != 0) or (filter_state != 0):
                log.warning(f"Camera is {camera_state[cam.state]}")
                log.warning(f"Filter is {filter_state[cam.is_moving]}")
                cam.abort()

            return

        except KeyboardInterrupt as e:
            msg = f"Interrupt triggered in {__name__}"
            log.error(msg)
            log.error(e)
            self.error.append(msg)
            self.error.append(e)
            return
