import obspython as obs


text_sources = []
counter_sources = [None, None, None, None]
counter_start_val = [0, 0, 0, 0]
global_settings = None
hotkey_ids = []
hotkey_id = obs.OBS_INVALID_HOTKEY_ID


def script_load(settings):
    print("----- OBS Counter Loaded -----")
    global hotkey_ids
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_1_add", "Counter 1 - Add", hk_counter_1_add))
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_1_subtract", "Counter 1 - Subtract", hk_counter_1_subtract))
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_2_add", "Counter 2 - Add", hk_counter_2_add))
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_2_subtract", "Counter 2 - Subtract", hk_counter_2_subtract))
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_3_add", "Counter 3 - Add", hk_counter_3_add))
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_3_subtract", "Counter 3 - Subtract", hk_counter_3_subtract))
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_4_add", "Counter 4 - Add", hk_counter_4_add))
    hotkey_ids.append(obs.obs_hotkey_register_frontend("counter_4_subtract", "Counter 4 - Subtract", hk_counter_4_subtract))

    hotkey_save_array = obs.obs_data_get_array(settings, "counter.hotkey_array")
    print(hotkey_save_array)
    for idx, ID in enumerate(hotkey_ids):
        obs.obs_hotkey_load(ID, hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)

    '''
    for id in hotkey_ids:
        obs.obs_hotkey_load(id, hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)
    '''
    obs.obs_frontend_add_event_callback(on_frontend_load)


def script_properties():
    print("props")
    print(counter_sources)

    props = obs.obs_properties_create()
    drop_list = obs.obs_properties_add_list(props, "counter_selection_1", "Text Source Counter 1", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
    obs.obs_property_list_add_string(drop_list, "", "")
    for i, src in enumerate(text_sources):
        obs.obs_property_list_add_string(drop_list, obs.obs_source_get_name(src), str(i))
    obs.obs_properties_add_int(props, "counter_default_value_1", "Start Value Counter 1", -999999999, 999999999, 1)

    drop_list = obs.obs_properties_add_list(props, "counter_selection_2", "Text Source Counter 2", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
    obs.obs_property_list_add_string(drop_list, "", "")
    for i, src in enumerate(text_sources):
        obs.obs_property_list_add_string(drop_list, obs.obs_source_get_name(src), str(i))
    obs.obs_properties_add_int(props, "counter_default_value_2", "Start Value Counter 2", -999999999, 999999999, 1)

    drop_list = obs.obs_properties_add_list(props, "counter_selection_3", "Text Source Counter 3", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
    obs.obs_property_list_add_string(drop_list, "", "")
    for i, src in enumerate(text_sources):
        obs.obs_property_list_add_string(drop_list, obs.obs_source_get_name(src), str(i))
    obs.obs_properties_add_int(props, "counter_default_value_3", "Start Value Counter 3", -999999999, 999999999, 1)

    drop_list = obs.obs_properties_add_list(props, "counter_selection_4", "Text Source Counter 4", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
    obs.obs_property_list_add_string(drop_list, "", "")
    for i, src in enumerate(text_sources):
        obs.obs_property_list_add_string(drop_list, obs.obs_source_get_name(src), str(i))
    obs.obs_properties_add_int(props, "counter_default_value_4", "Start Value Counter 4", -999999999, 999999999, 1)

    return props


def script_update(settings):
    print("update script")

    # first time called (on obs startup) just save settings globally, everytime after that (settings where chagned) call update_counter_settings
    global global_settings
    if global_settings is None:
        global_settings = settings
    else:
        update_counter_settings(settings)


def on_frontend_load(event):
    if event is not obs.OBS_FRONTEND_EVENT_FINISHED_LOADING:
        return

    print("frontend load")
    srcs = obs.obs_enum_sources()
    for src in srcs:
        if obs.obs_source_get_id(src) == "text_gdiplus_v2":
            text_sources.append(src)
    
    update_counter_settings(global_settings)
    script_properties()


def update_counter_settings(settings):
    for i in range(0,4):
        counter_start_val[i] = obs.obs_data_get_int(settings, "counter_default_value_{}".format(i+1))

        list_str = obs.obs_data_get_string(settings, "counter_selection_{}".format(i+1))
        if list_str == "":
            counter_sources[i] = None
        else:
            counter_sources[i] = text_sources[int(list_str)]
            set_source_text(counter_sources[i], str(counter_start_val[i]))

        # print("Counter", i+1, ":", obs.obs_source_get_name(counter_sources[i]), "Start Value:", counter_start_val[i])


def script_save(settings):
    print("save")

    hotkey_save_array = obs.obs_data_array_create()

    for idx, ID in enumerate(hotkey_ids):
        obs.obs_data_array_push_back(hotkey_save_array, obs.obs_hotkey_save(ID))

    obs.obs_data_set_array(settings, "counter.hotkey_array", hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)

    '''
    for ID in hotkey_ids:
        hotkey_save_array = obs.obs_hotkey_save(ID)
        print(hotkey_save_array)

    obs.obs_data_set_array(settings, "counter.hotkey_array", hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)
    '''


def script_unload():
    print("Unload")
    '''
    obs.obs_hotkey_unregister(hk_counter_1_add)
    obs.obs_hotkey_unregister(hk_counter_1_subtract)
    obs.obs_hotkey_unregister(hk_counter_2_add)
    obs.obs_hotkey_unregister(hk_counter_2_subtract)
    obs.obs_hotkey_unregister(hk_counter_3_add)
    obs.obs_hotkey_unregister(hk_counter_3_subtract)
    obs.obs_hotkey_unregister(hk_counter_4_add)
    obs.obs_hotkey_unregister(hk_counter_4_subtract)
    '''


def hk_counter_1_add(pressed):
    if pressed:
        hk_counter_handler(1, True)


def hk_counter_1_subtract(pressed):
    if pressed:
        hk_counter_handler(1, False)


def hk_counter_2_add(pressed):
    if pressed:
        hk_counter_handler(2, True)


def hk_counter_2_subtract(pressed):
    if pressed:
        hk_counter_handler(2, False)


def hk_counter_3_add(pressed):
    if pressed:
        hk_counter_handler(3, True)


def hk_counter_3_subtract(pressed):
    if pressed:
        hk_counter_handler(3, False)


def hk_counter_4_add(pressed):
    if pressed:
        hk_counter_handler(4, True)


def hk_counter_4_subtract(pressed):
    if pressed:
        hk_counter_handler(4, False)


def hk_counter_handler(counter, do_add):
    src = counter_sources[counter-1]
    if src is not None:
        text = get_source_text(src)
        if is_int(text):
            if do_add:
                new_int = int(text) + 1
                set_source_text(src, str(new_int))
            else:
                new_int = int(text) - 1
                set_source_text(src, str(new_int))


def get_source_text(src):
    src_settings = obs.obs_source_get_settings(src)
    text = obs.obs_data_get_string(src_settings, "text")
    obs.obs_data_release(src_settings)
    return text


def set_source_text(src, string):
    src_settings = obs.obs_source_get_settings(src)
    obs.obs_data_set_string(src_settings, "text", string)
    obs.obs_source_update(src, src_settings)
    obs.obs_data_release(src_settings)


def is_int(s):
    try:
        int(s)
        return True
    except ValueError:
        return False


