import json


class BGHelper:
    """
    :param id: unique id give for object
    :param action_type: Name/Label given for action
    :param bg_type: background type that will replace empty/None will not add any background
    :param bg_src: replacement value, can be color or image path to replace
    """
    def __init__(self, id, action_type, bg_type, bg_src):
        self.id = id
        self.action_type:str = action_type
        self.bg_type:str = bg_type
        self.bg_src:str = bg_src

    @classmethod
    def from_data(cls, id, action_type, data_str):
        return cls(id=id, action_type=action_type, bg_type=data_str["bg_type"], bg_src=data_str["bg_src"])


class FilterHelper:
    def __init__(self, id, action_type, filter_id, filter_name):
        self.id = id
        self.action_type = action_type
        self.filter_id = filter_id
        self.filter_name = filter_name

    @classmethod
    def from_data(cls, id, action_type, data_str):
        return cls(id=id, action_type=action_type, filter_id=data_str["filter_id"],
                   filter_name=data_str["filter_name"])


class BorderHelper:
    def __init__(self, id, action_type, border_width, border_color_value):
        self.id = id
        self.action_type = action_type
        self.border_width = border_width
        self.border_color_value = border_color_value

    @classmethod
    def from_data(cls, id, action_type, data_str):
        return cls(id=id, action_type=action_type, border_width=data_str["border_width"],
                   border_color_value=data_str["border_color_value"])


class InputHandler:
    def __init__(self, t_list=None , t_file_map= None):
        if t_list is None:
            self.magic_list = []
        else:
            self.magic_list = t_list

        if t_list is None:
            self.file_map_list = {}
        else:
            self.file_map_list = t_file_map


    @classmethod
    def from_data(cls, data_str):
        t_magic_list = []
        ip_data = json.loads(data_str)
        for each_action in ip_data:
            action_type = each_action.get("action", "")
            id = each_action.get("id", "")
            if action_type == "bg_remove":
                t_magic_list.append(
                    BGHelper.from_data(id=id, action_type=action_type, data_str=each_action.get(action_type, {})))
            elif action_type == "apply_filter":
                t_magic_list.append(
                    FilterHelper.from_data(id=id, action_type=action_type, data_str=each_action.get(action_type, {})))
            elif action_type == "apply_border":
                t_magic_list.append(
                    BorderHelper.from_data(id=id, action_type=action_type, data_str=each_action.get(action_type, {})))

        print("len", len(t_magic_list))
        print("-------")
        # for magic in t_magic_list:
        #     if isinstance(magic, BGHelper):
        #         print(magic.action_type, magic.bg_type, magic.bg_src)
        #     elif isinstance(magic, FilterHelper):
        #         print(magic.action_type, magic.filter_id, magic.filter_name)
        #     elif isinstance(magic, BorderHelper):
        #         print(magic.action_type, magic.border_width, magic.border_color_value)
        print("-------")
        return cls(t_list=t_magic_list)

    def toJson(self):
        return json.dumps(self, default=lambda o: o.__dict__)
