/**
 * Grid class - Cleaner version
 * 
 * Creates a minimal cyberpunk grid with pulse effects
 * Dramatically reduced horizontal lines for better visibility
 */
class Grid {
  // Grid properties
  int gridStyle = 0; // 0: minimal, 1: polar, 2: hexagonal
  int numStyles = 3;
  
  // Timing properties
  float cps = 0.5;
  float pulseIntensity = 0;
  
  // Colors
  color gridColor;
  color accentColor;
  
  // Minimal line counts
  int verticalLines = 6;     // dramatically reduced
  int horizontalLines = 2;   // dramatically reduced
  int polarCircles = 3;      // reduced
  int polarRadials = 6;      // reduced
  
  // Alpha values for subtle grid
  int defaultAlpha = 15;    // reduced for subtlety
  
  Grid() {
    gridColor = color(0, 100, 150, defaultAlpha);
    accentColor = color(0, 200, 255, 60);
  }
  
  void update() {
    // Decay pulse intensity
    pulseIntensity *= 0.95;
  }
  
  void display() {
    switch(gridStyle) {
      case 0:
        drawMinimalGrid();
        break;
      case 1:
        drawPolarGrid();
        break;
      case 2:
        drawHexGrid();
        break;
    }
  }
  
  void drawMinimalGrid() {
    strokeWeight(1);
    
    // Draw only a few vertical lines
    float verticalSpacing = width / (float)verticalLines;
    for (int i = 1; i < verticalLines; i++) {
      float x = i * verticalSpacing;
      float intensity = pulseIntensity * (1 - abs((x / width) - 0.5) * 2);
      
      stroke(lerpColor(gridColor, accentColor, intensity));
      line(x, 0, x, height);
    }
    
    // Draw only 1-2 horizontal lines (dramatically reduced)
    // Draw midline
    stroke(lerpColor(gridColor, accentColor, pulseIntensity * 0.3));
    strokeWeight(1 + pulseIntensity * 2);
    line(0, height * 0.5, width, height * 0.5);
    
    // Optional bottom line for grounding
    stroke(lerpColor(gridColor, accentColor, pulseIntensity * 0.1));
    strokeWeight(1);
    line(0, height * 0.9, width, height * 0.9);
  }
  
  void drawPolarGrid() {
    pushMatrix();
    translate(width / 2, height / 2);
    
    // Draw minimal circular grid
    noFill();
    for (int i = 1; i <= polarCircles; i++) {
      float radius = i * (min(width, height) / (polarCircles * 2.0));
      float intensity = pulseIntensity * (1 - (i / (float)polarCircles) * 0.8);
      
      stroke(lerpColor(gridColor, accentColor, intensity));
      strokeWeight(1 + intensity * 2);
      ellipse(0, 0, radius * 2, radius * 2);
    }
    
    // Draw minimal radial lines
    for (int i = 0; i < polarRadials; i++) {
      float angle = i * TWO_PI / polarRadials;
      float intensity = pulseIntensity * 0.6;
      
      stroke(lerpColor(gridColor, accentColor, intensity));
      strokeWeight(1 + intensity * 2);
      
      float radius = min(width, height) / 2;
      line(0, 0, cos(angle) * radius, sin(angle) * radius);
    }
    
    popMatrix();
  }
  
  void drawHexGrid() {
    float hexSize = 120;  // Increased size for fewer hexagons
    float horizontalSpacing = hexSize * 1.5;
    float verticalSpacing = hexSize * sqrt(3);
    
    stroke(lerpColor(gridColor, accentColor, pulseIntensity * 0.3));
    strokeWeight(1 + pulseIntensity * 2);
    noFill();
    
    // Draw just a few hexagons for subtle effect
    for (int row = 0; row < 3; row++) {  // Only 3 rows
      for (int col = 0; col < 3; col++) {  // Only 3 columns
        float xCenter = col * horizontalSpacing + ((row % 2 == 0) ? 0 : horizontalSpacing / 2);
        xCenter += width/2 - horizontalSpacing; // Center on screen
        
        float yCenter = row * verticalSpacing;
        yCenter += height/2 - verticalSpacing; // Center on screen
        
        // Intensity based on distance from center
        float distFromCenter = dist(xCenter, yCenter, width/2, height/2) / (width/2);
        float intensity = pulseIntensity * (1 - distFromCenter * 0.7);
        
        if (intensity > 0.05) {
          stroke(lerpColor(gridColor, accentColor, intensity));
          drawHexagon(xCenter, yCenter, hexSize);
        }
      }
    }
  }
  
  void drawHexagon(float xCenter, float yCenter, float size) {
    beginShape();
    for (int i = 0; i < 6; i++) {
      float angle = i * TWO_PI / 6;
      vertex(xCenter + cos(angle) * size, yCenter + sin(angle) * size);
    }
    endShape(CLOSE);
  }
  
  void trigger(float intensity) {
    // Trigger a pulse effect
    pulseIntensity = min(pulseIntensity + intensity, 1.0);
  }
  
  void setCPS(float newCps) {
    this.cps = newCps;
  }
  
  void toggleStyle() {
    gridStyle = (gridStyle + 1) % numStyles;
  }
}
