import java.lang.*;
import java.util.*;
import java.io.*;

import javax.swing.event.*; 
import javax.swing.*;   
import javax.swing.table.*;
      
import java.awt.*;
import java.awt.event.*;                       
import java.awt.datatransfer.*;

import javax.imageio.*;
import java.awt.image.*;
import java.util.zip.*;

public class prime_table_windowc extends JFrame implements ActionListener, ChangeListener {
  JPanel panel;
  JScrollPane panel2;
  JLabel label;
  Vector label_vector;
  JSpinner spinner;
  Vector spinner_vector;
  JLabel labelc;
  Vector labelc_vector;
  JButton add_button;
  JButton update_button;
  JPanel button_panel;
  prime_table_windowc() {
    setBounds(20,20,200,600);
    button_panel = new JPanel();
    this.getContentPane().add(button_panel,BorderLayout.SOUTH);    
    
    button_panel.setLayout(new GridLayout(1,1));
    create_new_panel();
    add_button = create_button("add","add");
    update_button = create_button("update","update");
    //this.getContentPane().add(update_button,BorderLayout.SOUTH);
    this.setTitle("prime harmonics");
  }
  JButton create_button(String text,String action) {  
    JButton button = new JButton(text);
    button.setActionCommand(action);    
    button.addActionListener(this);
    button_panel.add(button);
    //this.getContentPane().add(button);
    return button;
  }
  public void actionPerformed(ActionEvent e) {        
    String action = e.getActionCommand();
    //System.out.println(action);
    if (action.equals("add")) {
      int num_primes = main_app.prime_list.num_primes;
      if (num_primes < 50) {
        main_app.prime_list.add_prime();
      //this.hide();
        create_new_panel();
        this.show();
      }
      //this.repaint();
      //add_row(num_primes-1);
      //panel.setLayout(new GridLayout(num_primes,3));    
    }
    if (action.equals("update")) {
      int num_primes = main_app.prime_list.num_primes;
      for (int i = 0;i < num_primes;i++) {
        spinner = (JSpinner) spinner_vector.get(i);
        label = (JLabel) label_vector.get(i);      
        labelc = (JLabel) labelc_vector.get(i);      
        Number n = (Number) spinner.getValue();
        float f = n.floatValue();
        labelc.setText(get_cents(f) + " cents");
        main_app.prime_list.prime_factor[i] = f;
      }
      main_app.pattern_player.ins.alloc_string_tables();
      main_app.song_player.update_players();
      main_panelc.update_low_harmonics();
      main_panelc.update_harmonic_offsets();
    }
  }
  
  void create_new_panel() {
    //System.out.println("create_new_panel");
    if (panel2 != null) {
      this.getContentPane().remove(panel2);
    }
    int num_primes = main_app.prime_list.num_primes;
    panel = new JPanel();
    panel2 = new JScrollPane(panel);    
    panel.setLayout(new GridLayout(num_primes,3));    
    
    label_vector = new Vector();
    spinner_vector = new Vector();
    labelc_vector = new Vector();
    for (int i = 0;i < num_primes;i++) {
      add_row(i);
    }
    this.getContentPane().add(panel2);
    //panel2.show();
    //panel2.repaint();
    //this.getContentPane().repaint();
  }
  String strf2dr(int i) {
    int i1 = i % 100;
    int i2 = i / 100;
    
    String str = i2 + "." + i1;
    if (i1 < 10) {
      str = i2 + ".0" + i1;
    }
    return str;
  }
  void add_row(int i) {      
      float f = main_app.prime_list.prime_factor[i];
      spinner = new JSpinner(new SpinnerNumberModel(f, 1.2, 256.0, 0.01));
      //int p = prime_listc.get_prime_integer_factor(i);

      double oc = main_app.octave_cents;
      double notes_oct = main_app.notes_per_octave;
      int nsteps = (int) ((get_centsf(f) / oc) * notes_oct * 100);
      //int nsteps2 = nsteps % 100;
      //nsteps = nsteps / 100;
      spinner.addChangeListener(this);
      label = new JLabel(strf2dr(nsteps) + " steps");
      labelc = new JLabel(get_cents(f) + " cents");
      //labelc = new JLabel("factor " + (i+1) + ":");
      spinner_vector.add(spinner);
      label_vector.add(label);
      labelc_vector.add(labelc);      
      panel.add(spinner);        
      panel.add(label);
      panel.add(labelc);
  }
  double get_centsf(float f) {
    return (((Math.log(f) / Math.log(2)) * 1200)+0.5);
  }
  int get_cents(float f) {
    int c = (int) (((Math.log(f) / Math.log(2)) * 1200)+0.5);
    c = c % main_app.octave_cents;
    return c;
  }
  public void stateChanged(ChangeEvent e) {
    int num_primes = main_app.prime_list.num_primes;
    for (int i = 0;i < num_primes;i++) {
      spinner = (JSpinner) spinner_vector.get(i);
      labelc = (JLabel) labelc_vector.get(i);
      label = (JLabel) label_vector.get(i);      
      if (e.getSource() == spinner) {
        Number n = (Number) spinner.getValue();
        float f = n.floatValue();
        double oc = main_app.octave_cents;
        double notes_oct = main_app.notes_per_octave;
        int nsteps = (int) ((get_centsf(f) / oc) * notes_oct*100);
        //double fsteps = (nsteps * 0.01);
        label.setText(strf2dr(nsteps) + " steps");
        labelc.setText(get_cents(f) + " cents");
        main_app.prime_list.prime_factor[i] = f;
      }
    }
  }
}

