import os
import json

def singleton(cls):
    _instance = {}

    def inner():
        if cls not in _instance:
            _instance[cls] = cls()
        return _instance[cls]
    return inner

@singleton
class Store(object):
    def __init__(self):
        self.path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data.json")
        
        if not os.path.exists(self.path):
            with open(self.path, 'w') as f:
                json.dump({}, f)
                
    def read(self):
        with open(self.path, 'r') as f:
            data = json.load(f)
            
        return data
    
    def write(self, data):
        with open(self.path, 'w') as f:
            json.dump(data, f)


def groupUUID():
    sp = Store()
    read_data = sp.read()
    if "groupUUID" in read_data:
        return read_data["groupUUID"]
    else:
        return ""
    
def isCreateWidget():
    sp = Store()
    read_data = sp.read()
    if "isCreateWidget" in read_data:
        return read_data["isCreateWidget"]
    else:
        return False
    
def finishCreateWidget():
    sp = Store()
    read_data = sp.read()
    read_data["isCreateWidget"] = False
    sp.write(read_data)
    
def token():
    sp = Store()
    read_data = sp.read()
    if "token" in read_data:
        return read_data["token"]
    else:
        return ""
    
def widgetMap():
    sp = Store()
    read_data = sp.read()
    if "widgets" in read_data:
        return read_data["widgets"]
    else:
        return {}
    
def insertWidgets(widget_id, path):
    sp = Store()
    read_data = sp.read()
    if "widgets" not in read_data:
        read_data["widgets"] = {}
    widgetsMap = read_data["widgets"]
    widgetsMap[widget_id] = path
    for k in list(widgetsMap.keys()):
        if os.path.exists(widgetsMap[k]) == False:
            del widgetsMap[k]
    sp.write(read_data)

def writeDeviceInfo(data):
    sp = Store()
    read_data = sp.read()
    read_data["deviceInfo"] = data
    sp.write(read_data)
    
def readDeviceInfo():
    sp = Store()
    read_data = sp.read()
    if "deviceInfo" in read_data:
        return read_data["deviceInfo"]
    else:
        return {}
    
def clear():
    sp = Store()
    sp.write("{}")
    read_data = sp.read()