"""Definition of an ElkM1 Area"""
import time

from .const import Max, TextDescriptions
from .elements import Element, Elements
from .message import add_message_handler, as_encode, al_encode


class Area(Element):
    """Class representing an Area"""
    def __init__(self, index, elk):
        super().__init__(index, elk)
        self.armed_status = None
        self.arm_up_state = None
        self.alarm_state = None
        self.is_exit = False
        self.timer1 = 0
        self.timer2 = 0
        self.timer_timerstamp = 0

    def arm(self, level, code):
        """(Helper) Arm system at specified level (away, vacation, etc)"""
        self._elk.send(al_encode(level, self._index, code))

    def disarm(self, code):
        """(Helper) Disarm system."""
        self.arm(0, code)

class Areas(Elements):
    """Handling for multiple areas"""
    def __init__(self, elk):
        super().__init__(elk, Area, Max.AREAS.value)
        add_message_handler('AS', self._as_handler)
        add_message_handler('EE', self._ee_handler)

    def sync(self):
        """Retrieve areas from ElkM1"""
        self.elk.send(as_encode())
        self.get_descriptions(TextDescriptions.AREA.value)

    def _as_handler(self, armed_statuses, arm_up_states, alarm_states):
        for area in self.elements:
            area.setattr('armed_status', armed_statuses[area.index])
            area.setattr('arm_up_state', arm_up_states[area.index])
            area.setattr('alarm_state', alarm_states[area.index])

    def _ee_handler(self, area, is_exit, timer1, timer2, armed_status):
        area = self.elements[area]
        area.setattr('armed_status', armed_status)
        area.setattr('timer1', timer1)
        area.setattr('timer2', timer2)
        area.setattr('is_exit', is_exit)
        area.setattr('timer_timestamp', time.time())
