import os
import uuid
import requests

from .utils import default_config_path, read_from_config, save_in_config, \
                   is_uuid, get_timestamp, silent_fail
from .. import __version__

EVENT_VERSION = '1'
EVENT_SOURCE = 'cartoframes'

UUID_KEY = 'uuid'
ENABLED_KEY = 'enabled'
METRICS_FILENAME = 'metrics.json'

_metrics_config = None


def setup_metrics(enabled):
    '''Update the metrics configuration.

    Args:
        enabled (bool): flag to enable/disable metrics.

    '''
    global _metrics_config

    _metrics_config[ENABLED_KEY] = enabled

    save_in_config(_metrics_config, filename=METRICS_FILENAME)


@silent_fail
def init_metrics_config():
    global _metrics_config

    filepath = default_config_path(METRICS_FILENAME)

    if _metrics_config is None:
        if os.path.exists(filepath):
            _metrics_config = read_from_config(filepath=filepath)

        if not check_valid_metrics_uuid(_metrics_config):
            _metrics_config = create_metrics_config()
            save_in_config(_metrics_config, filename=METRICS_FILENAME)


def create_metrics_config():
    return {
        UUID_KEY: str(uuid.uuid4()),
        ENABLED_KEY: True
    }


def get_metrics_uuid():
    if _metrics_config is not None:
        return _metrics_config.get(UUID_KEY)


def get_metrics_enabled():
    if _metrics_config is not None:
        return _metrics_config.get(ENABLED_KEY)


def check_valid_metrics_uuid(metrics_config):
    return metrics_config is not None and is_uuid(metrics_config.get(UUID_KEY))


def build_metrics_data(event_name):
    return {
        'event_version': EVENT_VERSION,
        'event_timestamp': get_timestamp(),
        'event_source': EVENT_SOURCE,
        'event_name': event_name,
        'source_version': __version__,
        'installation_id': get_metrics_uuid()
    }


# @silent_fail
def post_metrics():
    json_data = build_metrics_data('data_uploaded')
    result = requests.post('http://carto.com/api/metrics', json=json_data)

    print(json_data, result)
    print('Metrics sent!')


def send_metrics(method):
    def fn(*args, **kw):
        result = method(*args, **kw)
        post_metrics()
        return result
    return fn


# Run this once
init_metrics_config()

@@ -1859,16 +1859,26 @@
 metrics(
+event_name
 ):%0A    j
@@ -1911,23 +1911,18 @@
 ata(
-'data_uploaded'
+event_name
 )%0A  
@@ -2074,27 +2074,75 @@
 ics(
-method):%0A    def fn
+event_name):%0A    def decorator_func(func):%0A        def wrapper_func
 (*ar
@@ -2149,16 +2149,20 @@
 gs, **kw
+args
 ):%0A     
@@ -2160,24 +2160,28 @@
 s):%0A        
+  
+  
 result = met
@@ -2181,14 +2181,12 @@
 t = 
-method
+func
 (*ar
@@ -2193,16 +2193,20 @@
 gs, **kw
+args
 )%0A      
@@ -2207,16 +2207,20 @@
         
+ 
+   
 post_met
@@ -2224,18 +2224,32 @@
 metrics(
-)%0A
+event_name)%0A    
         
@@ -2262,16 +2262,20 @@
  result%0A
+    
     retu
@@ -2281,10 +2281,46 @@
 urn 
-fn
+wrapper_func%0A    return decorator_func
 %0A%0A%0A#

Improve send_metrics decorator to allow params