from shadho.backend.base.domain import BaseDomain
from shadho.backend.json.value import Value
from shadho.backend.utils import InvalidObjectError

import uuid


class Domain(BaseDomain):
    def __init__(self, id=None, domain=None, path=None, strategy=None,
                 scaling=None, model=None, values=None, exhaustive=False,
                 exhaustive_idx=None):
        self.id = id if id is not None else str(uuid.uuid4())
        self.exhaustive_idx = exhaustive_idx
        if isinstance(domain, dict):
            distribution = getattr(scipy.stats, domain['distribution'])
            self.domain = distribution(*domain['args'],
                                       **domain['kwargs'])
            rng = np.random.RandomState()
            if 'rng' in domain:
                state = domain['rng']
                rng.set_state(tuple([state[0], np.array(state[1]), state[2],
                                     state[3], state[4]]))
            self.domain.random_state = rng
        elif exhaustive and isinstance(domain, list) and exhaustive_idx is None:
            self.domain = domain
            self.exhaustive_idx = 0
        else:
            self.domain = domain

        self.exhaustive = exhaustive

        self.path = path

        self.strategy = strategy if strategy is not None else 'random'
        self.scaling = scaling if scaling is not None else 'linear'

        self.model = model.id if hasattr(model, 'id') else model

        self.values = []
        values = values if values is not None else []
        for value in values:
            self.add_value(value)

    def add_value(self, value):
        if isinstance(value, Value):
            self.values.append(value.id)
        elif isinstance(value, (int, str)):
            self.values.append(value)
        else:
            raise InvalidObjectError(value)

    def to_json(self):
        if hasattr(self.domain, dist):
            domain = {
                'domain': self.domain.dist.name,
                'args': self.domain.args,
                'kwargs': self.domain.kwds,
            }
        else:
            domain = self.domain

        return {
            'domain': domain,
            'path': self.path,
            'strategy': self.strategy,
            'scaling': self.scaling,
            'model': self.model,
            'values': self.values,
            'exhaustive': self.exhaustive,
            'exhaustive_idx': self.exhaustive_idx
        }

@@ -153,16 +153,55 @@
 t uuid%0A%0A
+import numpy as np%0Aimport scipy.stats%0A%0A
 %0Aclass D
@@ -1539,44 +1539,60 @@
 s = 
+%5B
 values
+%5D
  if 
-values is not None else %5B%5D
+not isinstance(values, list) else values
 %0A   
@@ -1680,24 +1680,58 @@
 lf, value):%0A
+        if value is not None:%0A    
         if i
@@ -1751,24 +1751,28 @@
 ue, Value):%0A
+    
             
@@ -1804,24 +1804,28 @@
 id)%0A        
+   
+ 
 elif isinsta
@@ -1852,32 +1852,36 @@
 )):%0A            
+    
 self.values.appe
@@ -1890,38 +1890,46 @@
 (value)%0A        
+  
+  
 else:%0A
+    
             rais

update add_value to match pattern in Model