import time
import json


class Zone:
    def __init__(self, number=0, name="", actualHumidity="100", desiredHumidity="0", autoMode=False, state=False, setState=0, endTimeSetState=0, planedDuration=0):
        self.number = number
        self.name = name
        self.actualHumidity = actualHumidity
        self.desiredHumidity = desiredHumidity
        self.autoMode = autoMode #False = manual, True = automatic
        self.state = state #True = on, False = off
        self.setState = setState #0=default, 1=turned off for time, 2=turned on for time, 3=command in pipeline
        self.endTimeSetState = endTimeSetState
        self.planedDuration = planedDuration

    def toJSON(self, translater):
        return {
            "number": self.number,
            "name": self.name,
            "actualHumidity": self.actualHumidity,
            "desiredHumidity": self.desiredHumidity,
            "autoMode": self.autoMode,
            "operationMode_text": translater.getTranslation("automatic mode" if self.autoMode else "manual mode"),
            "state": self.state,
            "state_text": translater.getTranslation("switched on" if self.state else "switched off"),
            "setState": self.setState,
            "endTimeSetState" : self.endTimeSetState,
            "planedDuration": self.planedDuration,
        }

    def setZoneManager(self, zoneManager):
        self.zoneManager = zoneManager

    def timeOver(self):
        return time.time() > self.endTimeSetState

    def refreshStateAutomode(self):
        match self.setState:
            case 0:
                self.state = False
                if(self.planedDuration > 0):
                    self.setState = 3
                elif(self.desiredHumidity > self.actualHumidity):
                    self.zoneManager.switchZoneState(zone=self, state=True, duration=self.zoneManager.systemSettings.defaultAutoIrrigationDuration, instant=False)
            case 1:
                if(self.timeOver()):
                    self.setState = 0
                    self.refreshStateAutomode()
                else:
                    self.state = False
            case 2:
                if (self.timeOver()):
                    self.setState = 0
                    self.refreshStateAutomode()
                else:
                    self.state = True
            case 3:
                self.state = False
                if (not self.planedDuration > 0):
                    self.setState = 0


    def refreshStateManualmode(self):
        if(self.setState > 0):
            match self.setState:
                case 0:
                    self.state = False
                    if (self.planedDuration > 0):
                        self.setState = 3
                case 1:
                    if(self.timeOver()):
                        self.setState = 0
                    self.state = False
                case 2:
                    if (self.timeOver()):
                        self.setState = 0
                        self.state = False
                    else:
                        self.state = True
                case 3:
                    self.state = False
                    if (not self.planedDuration > 0):
                        self.setState = 0


    def refreshState(self):
        self.planedDuration = self.zoneManager.getPlanedDurationForZone(zone=self)
        if(self.autoMode):
            self.refreshStateAutomode()
        else:
            self.refreshStateManualmode()

    def switchMode(self, autoMode):
        if(self.autoMode != autoMode):
            self.autoMode = autoMode
            self.state = False
            self.refreshState()

    def switchState(self, state, duration, instant):
        if(instant):
            if(not state):
                self.setState = 1
            else:
                self.setState = 2
            self.endTimeSetState = time.time() + duration
        else:
            if(state):
                self.setState = 3
        self.refreshState()


