#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"

#include "lwip/err.h"
#include "lwip/sys.h"

#define EXAMPLE_ESP_WIFI_SSID       "iot"
#define EXAMPLE_ESP_WIFI_PASS       "<REDACTED>"
#define EXAMPLE_ESP_MAXIMUM_RETRY   5

#define WIFI_SCAN_LIST_SIZE         16

static const char *TAG = "wifi";

static esp_event_handler_instance_t ip_event_handler;
static esp_event_handler_instance_t wifi_event_handler;

static void print_auth_mode(int authmode)
{
    static const char *authmode_names[] = {
        [WIFI_AUTH_OPEN] = "WIFI_AUTH_OPEN",
        [WIFI_AUTH_WEP] = "WIFI_AUTH_WEP",
        [WIFI_AUTH_WPA_PSK] = "WIFI_AUTH_WPA_PSK",
        [WIFI_AUTH_WPA2_PSK] = "WIFI_AUTH_WPA2_PSK",
        [WIFI_AUTH_WPA_WPA2_PSK] = "WIFI_AUTH_WPA_WPA2_PSK",
        [WIFI_AUTH_WPA2_ENTERPRISE] = "WIFI_AUTH_WPA2_ENTERPRISE",
        [WIFI_AUTH_WPA3_PSK] = "WIFI_AUTH_WPA3_PSK",
        [WIFI_AUTH_WPA2_WPA3_PSK] = "WIFI_AUTH_WPA2_WPA3_PSK",
        [WIFI_AUTH_WAPI_PSK] = "WIFI_AUTH_WAPI_PSK",
        [WIFI_AUTH_MAX] = "UNKNOWN",
    };

    // Check for silly things
    if ((authmode < WIFI_AUTH_OPEN) || (authmode > WIFI_AUTH_MAX))
        authmode = WIFI_AUTH_MAX;

    ESP_LOGI(TAG, "Authmode \t%s", authmode_names[authmode]);
}

static void print_cipher_type(int pairwise_cipher, int group_cipher)
{
    static const char *cipher_names[] = {
        [WIFI_CIPHER_TYPE_NONE] = "WIFI_CIPHER_TYPE_NONE",
        [WIFI_CIPHER_TYPE_WEP40] = "WIFI_CIPHER_TYPE_WEP40",
        [WIFI_CIPHER_TYPE_WEP104] = "WIFI_CIPHER_TYPE_WEP104",
        [WIFI_CIPHER_TYPE_TKIP] = "WIFI_CIPHER_TYPE_TKIP",
        [WIFI_CIPHER_TYPE_CCMP] = "WIFI_CIPHER_TYPE_CCMP",
        [WIFI_CIPHER_TYPE_TKIP_CCMP] = "WIFI_CIPHER_TYPE_TKIP_CCMP",
        [WIFI_CIPHER_TYPE_AES_CMAC128] = "WIFI_CIPHER_TYPE_AES_CMAC128",
        [WIFI_CIPHER_TYPE_SMS4] = "WIFI_CIPHER_TYPE_SMS4",
        [WIFI_CIPHER_TYPE_GCMP] = "WIFI_CIPHER_TYPE_GCMP",
        [WIFI_CIPHER_TYPE_GCMP256] = "WIFI_CIPHER_TYPE_GCMP256",
        [WIFI_CIPHER_TYPE_AES_GMAC128] = "WIFI_CIPHER_TYPE_AES_GMAC128",
        [WIFI_CIPHER_TYPE_AES_GMAC256] = "WIFI_CIPHER_TYPE_AES_GMAC256",
        [WIFI_CIPHER_TYPE_UNKNOWN] = "UNKNOWN",
    };

    // Check for silly things
    if ((pairwise_cipher < WIFI_CIPHER_TYPE_NONE) || (pairwise_cipher > WIFI_CIPHER_TYPE_UNKNOWN))
        pairwise_cipher = WIFI_CIPHER_TYPE_UNKNOWN;
    if ((group_cipher < WIFI_CIPHER_TYPE_NONE) || (group_cipher > WIFI_CIPHER_TYPE_UNKNOWN))
        group_cipher = WIFI_CIPHER_TYPE_UNKNOWN;

    ESP_LOGI(TAG, "Pairwise Cipher \t%s", cipher_names[pairwise_cipher]);
    ESP_LOGI(TAG, "Group Cipher \t%s", cipher_names[group_cipher]);
}

// NOTE: Make sure WiFi is disconnected and not trying to connect else scan will fail with 0 results
static void wifi_scan(void)
{
    static wifi_ap_record_t ap_info[WIFI_SCAN_LIST_SIZE];

    uint16_t number = WIFI_SCAN_LIST_SIZE;
    uint16_t ap_count = 0;

    // Initialization
    memset(ap_info, 0, sizeof(ap_info));

    // Start scan
    esp_wifi_scan_start(NULL, true);
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
    ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);

    // Dump results
    for (int i = 0; (i < WIFI_SCAN_LIST_SIZE) && (i < ap_count); i++) {
        ESP_LOGI(TAG, "SSID \t\t%s", ap_info[i].ssid);
        ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi);
        print_auth_mode(ap_info[i].authmode);
        if (ap_info[i].authmode != WIFI_AUTH_WEP) {
            print_cipher_type(ap_info[i].pairwise_cipher, ap_info[i].group_cipher);
        }
        ESP_LOGI(TAG, "Channel \t\t%d\n", ap_info[i].primary);
    }
}

static void wifi_station_event_handler(void* arg, esp_event_base_t event_base,
                                int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
        ESP_LOGI(TAG, "WiFi station started, begin scan");
        wifi_scan();
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
        ESP_LOGW(TAG, "WiFi STA disconnected, reconnecting");
        esp_wifi_connect();
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
        esp_wifi_connect();
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
    }
}

static void wifi_ip_event_handler(void *arg, esp_event_base_t event_base,
                                 int32_t event_id, void *event_data)
{
    ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
    const esp_netif_ip_info_t *ip_info = &event->ip_info;

    ESP_LOGI(TAG, "WiFi Got IP Address");
    ESP_LOGI(TAG, "~~~~~~~~~~~");
    ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
    ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
    ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
    ESP_LOGI(TAG, "~~~~~~~~~~~");
}

int32_t wifi_init(void)
{
    ESP_ERROR_CHECK(esp_netif_init());

    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_sta();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_station_event_handler, NULL, &wifi_event_handler));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_ip_event_handler, NULL, &ip_event_handler));

    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS,
	     .threshold.authmode = WIFI_AUTH_WPA2_WPA3_PSK,
	     .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
        },
    };
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());

    ESP_LOGI(TAG, "Initialized");

    return 0;
}

