from models.model import Model

class Group(Model):
    def __init__(self):
        super().__init__()

    def create(self, name=""):
        next_id = self.get_next_id()
        self[next_id] = {
            "name": name,
            "devices": [],
            "pattern": "on",
            "colors": ["000000", "FF0000"],
            "brightness": 100,
            "delay": 100,
            "step_offset": 0,
            "step_increment": 1,
            "n1": 0,
            "n2": 0,
            "n3": 0,
            "n4": 0,
            "n5": 0,
            "n6": 0,
            "n7": 0,
            "n8": 0
        }
        self.save()
        return next_id

    def read(self, id):
        id_str = str(id)
        return self.get(id_str, None)
    
    def update(self, id, data):
        id_str = str(id)
        if id_str not in self:
            return False
        self[id_str].update(data)
        self.save()
        return True
    
    def delete(self, id):
        id_str = str(id)
        if id_str not in self:
            return False
        self.pop(id_str)
        self.save()
        return True
    
    def list(self):
        return list(self.keys())
