#include "Button.h"
#include "LCDModule.h"
#include "Prepare.h"
#include <WiFi.h>
#include <stdlib.h>
#include <string.h>


void setup(){
  Serial.begin(115200);

  String url = "http://10.2.1.1:/abc.png";

  String hostname = url.substring(7);
  uint16_t port = 80;
  String path = "/";

  int idx = hostname.indexOf("/");
  if(idx != -1) {
    path = hostname.substring(idx);
    hostname = hostname.substring(0, idx);
  }

  idx = hostname.indexOf(":");
  if(idx != -1) {
    if(!hostname.endsWith(":"))
      port = atoi(hostname.substring(idx + 1).c_str());
    hostname = hostname.substring(0, idx);
  }

  Serial.println(hostname);
  Serial.println(port);
  Serial.println(path);
  // Serial.println(WiFi.macAddress().substring(12, 14));

  // Setting up LCD
  initLCDModule();

  // Setting up joystick buttons
  initButtons();
}

void loop(){
  lcd ("   -- Idle --");
  lcd2("T:200c B:50c");
  switch(awaitButtonPress()) {
    case MENU:
      for(int menuIndex = 0; menuIndex >= 0;) {
        char *menuItem[] = {
          "Print from SD", 
          "Prepare", 
          "Printer Settings", 
          "Wi-fi Settings"
        };
        menuIndex = buildMenu(menuItem, sizeof(menuItem)/sizeof(*menuItem), 0);
        switch(menuIndex) {
          case 0: break;
          case 1: prepare(); break;
          case 2: break;
          case 3: break;
          default: return;
        }
      }
    break;
  }
}