#include "connect.h"
#include <linux/version.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <avm/hui/events.h>
#if defined(CONFIG_AVM_EVENT) || defined(CONFIG_AVM_EVENT_MODULE)
#define AVM_LED_INTERNAL
#include <avm/event/event.h>
#undef AVM_LED_INTERNAL
#endif

void methode6_init(enum connect_event new_state __maybe_unused){
    if (wlan_on == 0) {
		wlan_on = 1;
		state_machine_queue_trigger_event(connect_event_turn_wlan_on);
    }
    else if(access == access_type_plc_bridge){
        state_machine_queue_trigger_event(connect_event_access_plc_bridge);
    }
    else if(access == access_type_wlan_bridge){
        state_machine_queue_trigger_event(connect_event_access_wlan_bridge);
    }
    else{
        state_machine_queue_trigger_event(connect_event_access_lan_bridge);
    }
}

/* Connect Methode 6 */
struct connect_state state_machine_methode6[] = {
    [connect_state_wait] = default_connect_state_wait,
    [connect_state_turn_wlan_on] = default_connect_state_turn_wlan_on,
    [connect_state_init] = {
        .description = "methode6_connect_state_init",
        .pre_func = methode6_init,
        .transitions = {
            {.event = connect_event_access_wlan_bridge, .next_state = connect_state_pairing_wlan_wpsenrollee},
            {.event = connect_event_access_plc_bridge, .next_state = connect_state_pairing_wlan_wpsenrollee_no_connect},
            {.event = connect_event_access_lan_bridge, .next_state = connect_state_done},
            {.event = connect_event_turn_wlan_on, .next_state = connect_state_turn_wlan_on},
        },
    },
    [connect_state_pairing_wlan_wpsenrollee] = {
        .description = "methode6_connect_state_wlan_wpsenrollee",
        .timeout_auto_trigger_ms = MAIN_PAIRING_TIMEOUT,
        .auto_trigger_event = connect_event_timeout,
        .pre_button_events = {
            avm_event_push_button_wlan_wps_station,
        },
        .transitions = {
            {.event = connect_event_timeout, .next_state = connect_state_timeout},
            {.event = connect_event_wlan_sta_wps_timeout, .next_state = connect_state_timeout},
            {.event = connect_event_wlan_sta_wps_error, .next_state = connect_state_error},
            {.event = connect_event_wlan_sta_wps_success, .next_state = connect_state_post_nexus},
        },
    },
    [connect_state_error]= {
        .description = "methode6_connect_state_error",
        .pre_led_event = event_connect_pairing_error,
        .pre_button_events = {
            avm_event_push_button_wlan_wps_station_off,
            avm_event_push_button_wlan_sta_wps_no_connect_off,
            avm_event_push_button_connect_error,
        },
    },
    [connect_state_timeout]  = {
        .description = "methode6_connect_state_timeout",
        .pre_led_event = event_connect_pairing_timeout,
        .pre_button_events = {
            avm_event_push_button_wlan_wps_station_off,
            avm_event_push_button_wlan_sta_wps_no_connect_off,
            avm_event_push_button_connect_timeout,
        },
    },
    [connect_state_pairing_wlan_wpsenrollee_no_connect] = {
        .description = "methode6_connect_state_wlan_wpsenrollee_no_connect",
        .timeout_auto_trigger_ms = MAIN_PAIRING_TIMEOUT,
        .auto_trigger_event = connect_event_timeout,
        .pre_button_events = {
            avm_event_push_button_wlan_sta_wps_no_connect,
        },
        .transitions = {
            {.event = connect_event_timeout, .next_state = connect_state_timeout},
            {.event = connect_event_wlan_sta_wps_no_connect_timeout, .next_state = connect_state_timeout},
            {.event = connect_event_wlan_sta_wps_no_connect_error, .next_state = connect_state_error},
            {.event = connect_event_wlan_sta_wps_no_connect_success, .next_state = connect_state_wps_enrollee_success},
        },
    },
    [connect_state_wps_enrollee_success] = {
        .description = "methode6_connect_state_wps_enrollee_no_connect_success",
        .pre_led_event = event_connect_pairing_success,
        .pre_button_events = {
            avm_event_push_button_connect_success,
        },
    },
    [connect_state_post_nexus] = {
        .description = "methode6_connect_state_post_nexus",
        .timeout_auto_trigger_ms = POST_NEXUS_TIMEOUT,
        .auto_trigger_event = connect_event_nexus_pairing_timeout,
        .pre_button_events = {
            avm_event_push_button_nexus_pairing,
        },
        .transitions = {
            {.event = connect_event_nexus_pairing_success, .next_state = connect_state_post_nexus_success},
            {.event = connect_event_nexus_pairing_timeout, .next_state = connect_state_post_nexus_timeout},
            {.event = connect_event_nexus_pairing_error, .next_state = connect_state_post_nexus_error}
        },
    },
    [connect_state_post_nexus_success] = {
        .description = "methode6_connect_state_post_nexus_success",
        .pre_led_event = event_connect_pairing_success,
        .pre_button_events = {
            avm_event_push_button_connect_success,
        },
    },
    [connect_state_post_nexus_timeout] = {
        .description = "methode6_connect_state_post_nexus_timeout",
        .pre_led_event = event_connect_pairing_done,
        .pre_button_events = {
            avm_event_push_button_nexus_pairing_off,
        },
    },
    [connect_state_post_nexus_error]= {
        .description = "methode6_connect_state_post_nexus_error",
        .pre_led_event = event_connect_pairing_done,
        .pre_button_events = {
            avm_event_push_button_connect_error,
        },
    },
    [connect_state_done] = {
        .description = "methode6_connect_state_done",
        .pre_led_event = event_connect_pairing_done,
        .pre_button_events = {
            avm_event_push_button_connect_timeout,
        },
    },



};