#include "w5500.h"

#include <W5500/w5500.h>
#include <string.h>

#include "ch32v003fun.h"
#include "config.h"
#include "debug.h"
#include "spi_dma.h"
#include "systick.h"

void configure_network(void) {
  DEBUG_PRINT("===\n");
  DEBUG_PRINT("Starting network configuration...\n");
  DEBUG_PRINT("===\n");

  // Setup chip select and SPI callbacks
  reg_wizchip_cs_cbfunc(spi_select, spi_unselect);
  // reg_wizchip_spi_cbfunc(spi_read_byte, spi_write_byte);
  reg_wizchip_spiburst_cbfunc(spidma_read_buffer, spidma_write_buffer);

  uint8_t rx_tx_buff_sizes[] = {2, 2, 2, 2, 2, 2, 2, 2};
  wizchip_init(rx_tx_buff_sizes, rx_tx_buff_sizes);
}

// static uint8_t dns_buffer[512];

// todo: rm !!!
// void resolve_domain_name(const char* domain_name) {
//   DEBUG_PRINT("Resolving domain name \"%s\"...\n", domain_name);

//   DNS_init(DNS_SOCKET, dns_buffer);
//   // cloudflare dns
//   uint8_t dns[] = {1, 1, 1, 1};
//   uint8_t addr[4];
//   int8_t res;
//   uint8_t retries = 0;

//   while (retries < 3) {
//     Delay_Ms(250);

//     res = DNS_run(dns, (uint8_t*)domain_name, addr);
//     if (res == 1) {
//       DEBUG_PRINT("Result: %d.%d.%d.%d\n", addr[0], addr[1], addr[2],
//       addr[3]); break;
//     } else {
//       DEBUG_PRINT("DNS_run() failed, res = %d. Retries: %u\n", res, retries);
//     }
//     retries++;
//   }
// }
