Game slotmachine = new Game(500, 400);

void setup() {
  size(500, 400);
  background(0,0,0);
 text("Loading...", 20, 20);
 slotmachine.init();
}

void draw() {
 background(0,0,0);
 slotmachine.update();
 slotmachine.draw();
}

void mousePressed() {
  slotmachine.mousePressed(mouseX, mouseY, mouseButton);
}

void mouseReleased() {
   slotmachine.mouseReleased(mouseX, mouseY);
}

void mouseMoved() {
   slotmachine.mouseMoved(mouseX, mouseY);
}

void mouseDragged() {
  slotmachine.mouseDragged(mouseX, mouseY);
}
class Button {
  private float[] coords;
  private Event event;
  private float[] bounds;
  private Event hoverEvent;
  private Event clickEvent;
  private int[] fillColor;
  private int[] borderColor;
  private boolean previousPress = false;
  
 Button(Event event) {
   this.coords = new float[2];
    this.bounds = new float[2];
    this.fillColor = new int[3];
    this.borderColor = new int[3];
     this.hoverEvent = new Event(new String[]{"hover"});
     this.clickEvent = new Event(new String[]{"left", "right"});
     
    this.event = event;
 }


 void setPosition(float[] coords) {
  this.coords = coords;
 }
 
 void setBounds(float[] bounds) {
  this.bounds = bounds;
 }
 
 void setFillColor(int[] fillColor) {
  this.fillColor = fillColor;
 }
 
 void setBorderColor(int[] borderColor) {
   this.borderColor = borderColor;
 }
 
 void update() {
   if(this.hoverEvent.isRaised("hover")) {
     if(this.clickEvent.isRaised("left")) {
       this.previousPress = true;
       this.fillColor = new int[]{255, 0, 0};
     } else {
       this.fillColor = new int[]{64, 64, 255};
     }
   } else {
    this.fillColor = new int[]{0, 0, 255};
   }
 }
 
 void draw() {
   fill(this.fillColor[0], this.fillColor[1], this.fillColor[2]);
   stroke(this.borderColor[0], this.borderColor[1], this.borderColor[2]);
   rect(this.coords[0], this.coords[1], this.bounds[0], this.bounds[1]);
   fill(this.borderColor[0], this.borderColor[1], this.borderColor[2]);
   text("SPIN", this.coords[0] + (this.bounds[0] / 2) - 15, this.coords[1] + (this.bounds[1] / 2));
 }
 
 void mouseMoved(int mouseX, int mouseY) {
   if (mouseX > this.coords[0] && mouseX < this.coords[0] + this.bounds[0] &&
     mouseY > this.coords[1] && mouseY < this.coords[1] + this.bounds[1]) {
       this.hoverEvent.raiseEvent("hover");
     } else {
      this.hoverEvent.clear();
     }
 }
 
 void mousePressed(int mouseX, int mouseY, int mouseButton) {
   if(this.hoverEvent.isRaised("hover")) {
     if(mouseButton == LEFT) {
      this.clickEvent.raiseEvent("left"); 
     } else if (mouseButton == RIGHT) {
       this.clickEvent.raiseEvent("right"); 
     }
   }
 }
 
 void mouseReleased(int mouseX, int mouseY) {
      this.clickEvent.clear();
      if(this.previousPress && this.hoverEvent.isRaised("hover")) {
      this.event.raiseEvent("spinButtonClicked");
      this.previousPress = false;
      }
 }
 
 void mouseDragged(int mouseX, int mouseY) {
   this.mouseMoved(mouseX, mouseY);
 }
}

class Event {
  String[] eventKeys;
  boolean[] raised;
  Event(String[] eventKeys) {
    this.eventKeys = eventKeys;
    this.raised = new boolean[eventKeys.length];
  }
  
  void raiseEvent(String event) {
    int eventIndex = this.getIndexOfValue(event);
    if(eventIndex >= 0) {
     this.raised[eventIndex] = true; 
    }
  }
  
  boolean isRaised(String event) {
    int eventIndex = this.getIndexOfValue(event);
    if(eventIndex >= 0) {
     return this.raised[eventIndex]; 
    }
    return false;
  }
  
  private int getIndexOfValue(String key) {
   for(int i = 0; i < this.eventKeys.length; i++) {
    if(key == this.eventKeys[i]) {
       return i; 
    }
   }
  return -1; 
  }
  
  void clear() {
    this.raised = new boolean[eventKeys.length];
  }
  
  void clearEvent(String event) {
    println("clearing " + event);
    int eventIndex = this.getIndexOfValue(event);
    if(eventIndex >= 0) {
     this.raised[eventIndex] = false; 
    }
  }
}

class Game {
  private float width;
  private float height;
  private Button spinButton;
  private Reel[] reels;
  private Event[] gameEvents;
  private Event[] reelEvents;
  private boolean startedSpin;
  private int[] lastSpinResults;
  private int lastWinAmount;
  
 Game(float width, float height) {
   this.width = width;
   this.height = height;
   this.reelEvents = new Event[]{
     new Event(new String[]{"reelDone"})
     , new Event(new String[]{"reelDone"})
     , new Event(new String[]{"reelDone"})
     , new Event(new String[]{"reelDone"})
     , new Event(new String[]{"reelDone"})
   };
   this.gameEvents = new Event[]{
     new Event(new String[]{"spinButtonClicked"})
   };
   this.spinButton = new Button(this.gameEvents[0]);
   this.reels = new Reel[]{
     new Reel(null, this.reelEvents[0])
     ,new Reel(this.reelEvents[0], this.reelEvents[1])
     ,new Reel(this.reelEvents[1], this.reelEvents[2])
     ,new Reel(this.reelEvents[2], this.reelEvents[3])
     ,new Reel(this.reelEvents[3], this.reelEvents[4])
   };
 }
 
 void init() {
   this.lastWinAmount = 0;
   this.startedSpin = false;
   this.spinButton.setPosition(new float[]{this.width - 90, this.height - 90});
   this.spinButton.setBounds(new float[]{80, 80});
   
   for(int i = 0; i < this.reels.length; i++) {
     this.reels[i].init();
      this.reels[i].setPosition(new float[]{((i + 1) * 15) + (i * 80), 20});
   this.reels[i].setFillColor(new int[]{0, 0, 0});
   this.reels[i].setBorderColor(new int[]{255, 0, 0});
   }
   
   this.lastSpinResults = new int[this.reels.length];
  
 }
 
 void draw() {
   this.spinButton.draw();
   
    for(int i = 0; i < this.reels.length; i++) {
     this.reels[i].draw();
    }
    
    text("Last win: " + this.lastWinAmount, this.width / 2, this.height - 50);
 }
 
 void update() {
   this.spinButton.update();
   
   if(this.gameEvents[0].isRaised("spinButtonClicked")) {
     this.startedSpin = true;
     this.lastSpinResults = new int[this.reels.length];
     for(int i = 0; i < this.reels.length; i++) {
       this.lastSpinResults[i] = this.reels[i].spin(i);
     }
     
   }
   
   if(this.startedSpin && this.reelEvents[this.reelEvents.length - 1].isRaised("reelDone")) {
     for(int i = 0; i < this.reelEvents.length; i++) {
     this.reelEvents[i].clear();
     this.startedSpin = false;
   }
   this.rewardCalculation(this.lastSpinResults);
   }

   for(int i = 0; i < this.reels.length; i++) {
     this.reels[i].update();
    }
    
   for(int i = 0; i < this.gameEvents.length; i++) {
     this.gameEvents[i].clear();
   }
 }
 
 private void rewardCalculation(int[] spinResults) {
   int prize = 0;
   int dupeCount = 0;
   for(int i = 0; i < spinResults.length; i++) {
   if (i < spinResults.length - 1) {
    if(spinResults[i] == spinResults[i + 1]) {
     dupeCount += 1;
    prize += spinResults.length;
    prize *= dupeCount;
   } else {
    dupeCount = 0;
   } 
   }
   }
   
   this.lastWinAmount = prize; 
 }
 
 void mousePressed(int mouseX, int mouseY, int mouseButton) {
   this.spinButton.mousePressed(mouseX, mouseY, mouseButton);
 }
 void mouseReleased(int mouseX, int mouseY) {
   this.spinButton.mouseReleased(mouseX, mouseY);
 }
 void mouseMoved(int mouseX, int mouseY) {
   this.spinButton.mouseMoved(mouseX, mouseY);
 }
 
  void mouseDragged(int mouseX, int mouseY) {
   this.spinButton.mouseDragged(mouseX, mouseY);
 }
}
class Reel {
  private float[] coords;
  private int[] fillColor;
  private int[] borderColor;
  private String[] reelList;
  private int currentReelIndex;
  private int nextReelIndex;
  private Event previousReel;
  private Event currentReel;
  private boolean isSpinning;
  
  Reel(Event previousReel, Event currentReel) {
    this.previousReel = previousReel;
    this.currentReel = currentReel;
   this.coords = new float[2];
  this.reelList = new String[14]; 
  }
  
  void init() {
    this.currentReelIndex = this.reelList.length - 1;
    this.nextReelIndex = 1;
    this.isSpinning = true;
    
    this.reelList[0] = "iron"; //pull
    this.reelList[1] = "steel"; //push
    this.reelList[2] = "tin"; //senses
    this.reelList[3] = "pewter"; //strength
    
    
    this.reelList[4] = "zinc"; //rioter
    this.reelList[5] = "brass"; //soother
    this.reelList[6] = "copper"; //hide allomancy
    this.reelList[7] = "bronze"; //sense allomancy
    
    //this.reelList[0] = "cadmium"; //slow time bubble
    //this.reelList[0] = "bendalloy"; //fast time bubble
    this.reelList[8] = "gold"; //see past with different choices
    this.reelList[9] = "electrum"; //see future
    
    //this.reelList[0] = "chromium"; //destroy another's metals
    //this.reelList[0] = "nicrosil"; //flare another's metals
    this.reelList[10] = "aluminium"; //destroy self metals
    this.reelList[11] = "duralumin"; //flare self metals
    
    this.reelList[12] = "atium"; //see few seconds into the future
    this.reelList[13] = "malatium"; //see someone else's possible lives
    //this.reelList[0] = "lerasium"; //make mistborn
    
  }
  
  void setPosition(float[] coords) {
  this.coords = coords;
 }
 
  void setFillColor(int[] fillColor) {
  this.fillColor = fillColor;
 }
 
 void setBorderColor(int[] borderColor) {
   this.borderColor = borderColor;
 }
 
 int spin(int seed) {
   this.nextReelIndex = (int)random(0, this.reelList.length);
   this.isSpinning = true;
   return this.nextReelIndex;
   
 }
  
  void draw() {
    fill(this.fillColor[0], this.fillColor[1], this.fillColor[2]);
    stroke(this.borderColor[0], this.borderColor[1], this.borderColor[2]);
    rect(this.coords[0], this.coords[1], 80, 180);
   
    fill(255, 255, 255);
    int previousReelIndex = this.currentReelIndex - 1;
    int nextReelIndex = this.currentReelIndex + 1;
    if(previousReelIndex < 0) {
      previousReelIndex = this.reelList.length - 1;
    }
    if(nextReelIndex > this.reelList.length - 1) {
      nextReelIndex = 0;
    }
    
    text(this.reelList[previousReelIndex], this.coords[0] + 5, this.coords[1] + 30);
    text(this.reelList[this.currentReelIndex], this.coords[0] + 5, this.coords[1] + 90);
    text(this.reelList[nextReelIndex], this.coords[0] + 5, this.coords[1] + 150);
  }
  
  void update() {
    if(this.currentReelIndex == this.nextReelIndex && (this.previousReel == null || this.previousReel.isRaised("reelDone"))) {
     this.isSpinning = false;
     this.currentReel.raiseEvent("reelDone");
    }
    
     if(isSpinning) {
      this.currentReelIndex += 1;
    }
    
    if(this.currentReelIndex > this.reelList.length - 1) {
     this.currentReelIndex = 0;
    }
  }
}


