import fullscreen.*;
import guicomponents.*;
//import processing.opengl.*;
SoftFullScreen fs; 

GPanel pnlControls;
GTextField txfSomeText;
GButton Pro, Ard, CrossBtn, StopProg;
GLabel Closed, Shutdown;

int P = 0, A = 0;
PImage cross_image, processing_logo;

int cx, cy;
float secondsRadius;
float minutesRadius;
float hoursRadius;
float clockDiameter;

RectButton rect1, rect2, rect3, rect4, rect5, rect6, rect7, rect8, rect9, rect10, rect11, rect12;
boolean locked = false;
color currentcolor;

PFont fontA, closeFont; 

void setup() {
  smooth();
  size(1335, 715);
  fs = new SoftFullScreen(this); 
  fs.enter(); 

  int radius = min(149, 149) / 3;
  secondsRadius = radius * 0.72;
  minutesRadius = radius * 0.60;
  hoursRadius = radius * 0.50;
  clockDiameter = radius * 1.8;

  cx = 560;
  cy = 560;

  G4P.setMouseOverEnabled(true);
  LogoButtons();

  color baseColor = color(102);
  currentcolor = baseColor;
  color buttoncolor = color(#1AA1E1);
  color highlight = color(#9AE31A); 
  rect1 = new RectButton(33, 33, 149, 149, buttoncolor, highlight);
  rect2 = new RectButton(193, 33, 149, 149, buttoncolor, highlight);
  rect3 = new RectButton(354, 33, 149, 149, buttoncolor, highlight);
  rect4 = new RectButton(33, 193, 149, 149, buttoncolor, highlight);
  rect5 = new RectButton(354, 193, 149, 307, buttoncolor, highlight);
  rect6 = new RectButton(33, 350, 310, 149, buttoncolor, highlight);
  rect7 = new RectButton(194, 514, 149, 149, buttoncolor, highlight);
  rect8 = new RectButton(354, 514, 149, 149, buttoncolor, highlight);
  rect9= new RectButton(836, 514, 149, 149, buttoncolor, highlight);
  rect10= new RectButton(33, 193, 149, 149, buttoncolor, highlight);
  buttoncolor = color(#9AE31A);
  highlight = color(#0BFC00); 
  rect11 = new RectButton(33, 514, 149, 149, buttoncolor, highlight);//corner rect
  rect12 = new RectButton(676, 514, 149, 149, buttoncolor, highlight);//right rect
}

void draw() {
  background(0);
  //tint(255,255);
  //image(loadImage("cross.jpg"), 525, 35, 25, 25);
  tint(255, P);
  image(loadImage("Processing.jpg"), 700, 100, 475, 300);
  tint(255, A);
  image(loadImage("Arduino.jpg"), 700, 100, 475, 300);
  fill(#1AA1E1);
  rect(515, 514, 149, 149); //clock rect, DO NOT REMOVE
  analogClock();
  digitalClock();
  fill(#9AE31A);
  rect(194, 193, 149, 149); //date rect, DO NOT REMOVE
  date();

  update(mouseX, mouseY);
  rect1.display();
  rect2.display();
  rect3.display();
  rect4.display();
  rect5.display();
  rect6.display();
  rect7.display();
  rect8.display();
  rect9.display();
  rect10.display();
  rect11.display();
  rect12.display();
}

void update(int x, int y)
{
  if (locked == false) {
    rect1.update();
    rect2.update();
    rect3.update();
    rect4.update();
    rect5.update();
    rect6.update();
    rect7.update();
    rect8.update();
    rect9.update();
    rect10.update();
    rect11.update();
    rect12.update();
  } 
  else {
    locked = false;
  }

  if (mousePressed) {
    if (rect1.pressed()) {
      currentcolor = rect1.basecolor;
    } 
    else if (rect2.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect3.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect4.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect5.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect6.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect7.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect8.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect9.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect10.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect11.pressed()) {
      currentcolor = rect2.basecolor;
    }
    else if (rect12.pressed()) {
      currentcolor = rect2.basecolor;
    }
  }
}

void LogoButtons() {
  CrossBtn = new GButton(this, "Button.png", 1, 525, 35, 25, 25);
  StopProg = new GButton(this, "power.jpg", 1, 35, 675, 25, 25);
  Pro = new GButton(this, "ProcessingLogo.png", 1, 996, 514, 154, 154);
  Ard = new GButton(this, "ArduinoLogo.png", 1, 1156, 513, 155, 155);
  CrossBtn.setAlpha(0);
  Pro.setAlpha(0);
  Ard.setAlpha(0);
}

void handleButtonEvents(GButton button) 
{
  if (Pro == button && button.eventType == GButton.CLICKED && P == 0) {
    P = 255;
    A = 0;
  }
  else {
    P = 0;
  }
  if (Ard == button && button.eventType == GButton.CLICKED && A == 0) {
    A = 255;
    P = 0;
  }
  else {
    A = 0;
  }
  if (StopProg == button && button.eventType == GButton.CLICKED) {
    System.exit(0);
  }
}
void analogClock() {
  noStroke();
  ellipse(cx, cy, clockDiameter, clockDiameter);

  // Angles for sin() and cos() start at 3 o'clock;
  // subtract HALF_PI to make them start at the top
  float s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
  float m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI; 
  float h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI;

  // Draw the hands of the clock
  stroke(255);
  strokeWeight(1);
  line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);
  strokeWeight(2);
  line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius);
  strokeWeight(4);
  line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius);

  // Draw the minute ticks
  strokeWeight(2);
  beginShape(POINTS);
  for (int a = 0; a < 360; a+=30) {
    float x = cx + cos(radians(a)) * secondsRadius;
    float y = cy + sin(radians(a)) * secondsRadius;
    vertex(x, y);
  }
  endShape();
}
void digitalClock() {
  PFont font = loadFont("Pro-20.vlw");
  textFont(font, 28);
  fill(255);
  fontA = loadFont("Batang-48.vlw");
  textFont(fontA, 22);
  text("Singapore", 550, 620);
  int s = second();
  int m = minute();
  int h = hour();
  // The nf() function spaces the numbers nicely
  String t = nf(h, 2) + ":" + nf(m, 2) + ":" + nf(s, 2);
  text(t, 520, 650);
}
void date() {
  PFont metaBold = loadFont("Meta-Bold.vlw"); 

  fill(255);
  int d = day();    // Values from 1 - 31
  int m = month();  // Values from 1 - 12
  int y = year();   // 2003, 2004, 2005, etc.
  String M = "";
  int x = 0;
  textFont(metaBold, 60);
  String s = String.valueOf(d);
  text(s, 250, 290);
  //s = String.valueOf(m);
  textFont(metaBold, 40);
  s = String.valueOf(y);
  text(s, 245, 335);

  switch(m) {
  case 1: 
    M = "January";
    break;
  case 2: 
    M = "February";
    break;
  case 3: 
    M = "March";
    break;
  case 4: 
    M = "April";
    break;
  case 5: 
    M = "May";
    break;
  case 6: 
    M = "June";
    break;
  case 7: 
    M = "July";
    break;
  case 8: 
    M = "August";
    break;
  case 9: 
    M = "September";
    break;
  case 10: 
    M = "October";
    x = 220;
    break;
  case 11: 
    M = "November";
    break;
  case 12: 
    M = "December";
    break;
  default:
    break;
  }
  text(M, x, 230);
}