from enum import Enum
from karura.env import get_lang
from karura.core.description import Description


class Insight():
    LANG = get_lang()

    def __init__(self):
        self.lang = self.LANG
        self.description = {}
        self.index = InsightIndex()
        self.automatic = False
    
    def init_description(self):
        for k in self.description:
            if isinstance(self.description[k], Description):
                self.description[k].delete()

        self.description.clear()

    def describe(self):
        d = None
        if self.lang in self.description:
            d = self.description[self.lang]
        elif len(self.description) > 0:
            d = list(self.description.values())[0]
        
        if isinstance(d, str) and len(d) > 0:
            return Description(d)
        else:
            return d
    
    def a2t(self, array):
        return ",".join(array)

    def is_applicable(self, dfe):
        self.init_description()
        its = self.get_insight_targets(dfe)
        if len(its) > 0:
            return True
        else:
            return False
    
    def adopt(self, dfe, interpreted=None):
        raise Exception("You have to implements adopt method")
    
    def get_insight_targets(self, dfe):
        return []
    
    def interpret(self, reply):
        if reply:
            return True
        else:
            return False
    
    def get_transformer(self, dfe):
        return None


class InsightIndex():
    COLUMN_CHECK_TAG = "column_check"
    ROW_CHECK_TAG = "row_check"
    PREPROCESSING = "preprocessing"
    FEATURE_AUGMENTATION = "feature_augmentation"
    LABEL_FORMAT = "label_format"
    FEATURE_SELECTION = "feature_selection"
    MODEL_SELECTION = "model_selection"

    def __init__(self, done=False, tags=()):
        self.done = done
        self.tags = [] if len(tags) == 0 else tags
    
    def as_column_check(self):
        self.set_tag(self.COLUMN_CHECK_TAG)
    
    def as_row_check(self):
        self.set_tag(self.ROW_CHECK_TAG)
    
    def as_preprocessing(self):
        self.set_tag(self.PREPROCESSING)
    
    def as_feature_augmentation(self):
        self.set_tag(self.FEATURE_AUGMENTATION)

    def as_label_format(self):
        self.set_tag(self.LABEL_FORMAT)

    def as_feature_selection(self):
        self.set_tag(self.FEATURE_SELECTION)

    def as_model_selection(self):
        self.set_tag(self.MODEL_SELECTION)

    def append_tag(self, tag_name):
        if tag_name not in self.tags:
            self.tags.append(tag_name)

    def set_tag(self, tag_name):
        self.tags = []
        self.tags.append(tag_name)

    @classmethod
    def query(cls, insights, is_done=None, tag=""):
        done_criteria = lambda x: True if is_done is None or x.index.done == is_done else False
        tag_criteria = lambda x: True if tag == "" or tag in x.index.tags else False
        items = [i for i in insights if done_criteria(i) and tag_criteria(i)]
        return items
