/*
 * Decompiled with CFR 0_132.
 */
package DFT;

import DFT.DftGen;
import DFT.Graph;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.text.Format;
import java.text.NumberFormat;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import main.ChronoPanel;
import signals.Signals;

public class Tools
extends JPanel
implements ActionListener,
MouseListener,
MouseMotionListener {
    private static final long serialVersionUID = 1L;
    private float[] samples;
    private float[] dft;
    private int Nsample = 150;
    private Signals signal;
    private Graph graph;
    private int width;
    private float offset = 0.0f;
    private boolean onSig = false;
    private int widthTmp;
    private boolean removeContinous = false;
    private JLabel samplingLabel = new JLabel("Sampling Rate: ");
    private JFormattedTextField samplingField = new JFormattedTextField(NumberFormat.getIntegerInstance());
    private JButton okSampleButton = new JButton("Ok");
    private JCheckBox continousPart = new JCheckBox("Remove continous part");

    public Tools(Signals s, int width, Graph graph) {
        this.signal = s;
        this.graph = graph;
        this.width = width;
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.samplingField.setPreferredSize(new Dimension(60, 20));
        this.samplingField.setValue(this.Nsample);
        this.okSampleButton.setPreferredSize(new Dimension(50, 20));
        this.okSampleButton.addActionListener(this);
        this.okSampleButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent evt) {
                String tmp = Tools.this.samplingField.getText();
                tmp = tmp.replaceAll(",", "");
                Tools.access$1(Tools.this, Integer.parseInt(tmp));
                Tools.this.genDFT();
                Tools.this.repaint();
            }
        });
        this.continousPart.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent evt) {
                Tools.access$3(Tools.this, Tools.this.continousPart.getModel().isSelected());
                Tools.this.genDFT();
                Tools.this.repaint();
            }
        });
        JSeparator sep = new JSeparator(1);
        sep.setPreferredSize(new Dimension(5, 20));
        this.add(this.samplingLabel);
        this.add(this.samplingField);
        this.add(this.okSampleButton);
        this.add(sep);
        this.add(this.continousPart);
        this.genDFT();
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(new Color(220, 220, 220));
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.translate(0, 30);
        int sigHeight = Signals.heightUnit + Signals.Yspace + 10;
        g.setColor(Color.WHITE);
        g.fillRect(3, 5, this.getWidth() - 6, sigHeight - 5);
        g.setColor(Color.BLACK);
        g.drawRect(3, 5, this.getWidth() - 6, sigHeight - 5);
        if (this.onSig) {
            g.setColor(new Color(210, 210, 230));
            g.fillRect(Signals.leftSpace, Signals.Yspace - 8, this.widthTmp - Signals.leftSpace, Signals.heightUnit + 16);
        }
        this.signal.draw(g, null, 0, this.getWidth());
        g.setColor(Color.RED);
        int i = 0;
        while (i < this.Nsample) {
            int x = Signals.leftSpace + i * (this.width - Signals.leftSpace) / this.Nsample;
            g.fillRect(x - 1, (int)(1.0f - this.samples[i]) * Signals.heightUnit + Signals.Yspace - 1, 3, 3);
            ++i;
        }
        g.translate(0, -30);
        this.graph.update(this.dft);
    }

    public void genDFT() {
        this.samples = new float[this.Nsample];
        float[] dftTmp = new float[this.Nsample];
        this.dft = new float[this.Nsample / 2];
        int i = 0;
        while (i < this.Nsample) {
            int x = Signals.leftSpace + i * (this.width - Signals.leftSpace) / this.Nsample;
            this.samples[i] = (float)this.signal.wave[this.signal.getIndex(x)] + this.offset;
            ++i;
        }
        dftTmp = DftGen.from(this.samples);
        i = 0;
        while (i < this.Nsample / 2) {
            this.dft[i] = dftTmp[i];
            ++i;
        }
        if (this.removeContinous) {
            this.dft[0] = 0.0f;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        this.onSig = this.widthTmp > Signals.leftSpace;
        if (this.onSig) {
            this.width = this.widthTmp;
            this.genDFT();
            this.repaint();
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        this.onSig = false;
        this.repaint();
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        this.onSig = e.getY() > 30;
        this.widthTmp = (e.getX() - Signals.leftSpace + this.signal.widthUnit / 2) / this.signal.widthUnit * this.signal.widthUnit + Signals.leftSpace;
        this.repaint();
    }

    static /* synthetic */ void access$1(Tools tools, int n) {
        tools.Nsample = n;
    }

    static /* synthetic */ void access$3(Tools tools, boolean bl) {
        tools.removeContinous = bl;
    }

}

