/*
 * jTimeRegion.java
 *
 * Created on 5 Ιούλιος 2005, 11:30 πμ
 *
 * This file is part of Jubler.
 *
 * Jubler is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.
 *
 *
 * Jubler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jubler; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

package com.panayotis.jubler.time.gui;
import com.panayotis.jubler.subs.SubEntry;
import com.panayotis.jubler.subs.Subtitles;
import com.panayotis.jubler.time.Time;
import com.panayotis.jubler.time.gui.JTimeSpinner;
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JRadioButton;

import static com.panayotis.jubler.i18n.I18N._;

/**
 *
 * @author  teras
 */
public class JTimeRegion extends JTimeArea {
    
    protected JTimeSpinner curSTime, givSTime;
    protected JTimeSpinner curFTime, givFTime;
    
    /** Creates new form jTimeRegion */
    public JTimeRegion() {
        super();
        initComponents();
        curSTime = new JTimeSpinner();
        givSTime = new JTimeSpinner();
        curFTime = new JTimeSpinner();
        givFTime = new JTimeSpinner();
        PSCurrent.add(curSTime, BorderLayout.CENTER);
        PSGiven.add(givSTime, BorderLayout.CENTER);
        PFCurrent.add(curFTime, BorderLayout.CENTER);
        PFGiven.add(givFTime, BorderLayout.CENTER);
        
        curSTime.setEnabled(false);
        givSTime.setEnabled(false);
        curFTime.setEnabled(false);
        givFTime.setEnabled(false);
        
        curSTime.setToolTipText(_("Use selected time as start of region"));
        curFTime.setToolTipText(_("Use selected time as end of region"));
        givSTime.setToolTipText(_("Use given time as start of region"));
        givFTime.setToolTipText(_("Use given time as end of region"));
    }
    
    Time first, last;
    
    
    public void updateData(Subtitles subs, int[] selected) {
        super.updateData(subs, selected);
        initTime(findFirstInList(subs, selected), findLastInList(subs, selected));
    }
    
    
    public Vector<SubEntry> getAffectedSubs() {
        /* Select affected subtitles by time region */
        double tstart, tfinish;
        double tcurrent;
        
        Vector<SubEntry> affected;
        SubEntry csub;
        int i;
        
        tstart = getStartTime();
        tfinish = getFinishTime();
        
        affected = new Vector<SubEntry>();
        for ( i = 0 ; i < subs.size() ; i++ ) {
            csub = subs.elementAt(i);
            tcurrent = csub.getStartTime().toSeconds();
            if (tcurrent >= tstart && tcurrent <= tfinish) {
                affected.add(csub);
            }
        }
        return affected;
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        FromGroup = new javax.swing.ButtonGroup();
        ToGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        TimeSBegin = new javax.swing.JRadioButton();
        TimeSCurrent = new javax.swing.JRadioButton();
        PSCurrent = new javax.swing.JPanel();
        TimeSGiven = new javax.swing.JRadioButton();
        PSGiven = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        TimeFBegin = new javax.swing.JRadioButton();
        TimeFCurrent = new javax.swing.JRadioButton();
        PFCurrent = new javax.swing.JPanel();
        TimeFGiven = new javax.swing.JRadioButton();
        PFGiven = new javax.swing.JPanel();

        setLayout(new java.awt.GridLayout(1, 0));

        jPanel1.setLayout(new java.awt.GridLayout(5, 0));

        jPanel1.setBorder(new javax.swing.border.TitledBorder(_("Start time")));
        FromGroup.add(TimeSBegin);
        TimeSBegin.setSelected(true);
        TimeSBegin.setText(_("From the beginning"));
        TimeSBegin.setToolTipText(_("The beginning of the region is the start of the file"));
        TimeSBegin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimeSBeginActionPerformed(evt);
            }
        });

        jPanel1.add(TimeSBegin);

        FromGroup.add(TimeSCurrent);
        TimeSCurrent.setText(_("From current time"));
        TimeSCurrent.setToolTipText(_("The beginning of the region is the selected time"));
        TimeSCurrent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimeSCurrentActionPerformed(evt);
            }
        });

        jPanel1.add(TimeSCurrent);

        PSCurrent.setLayout(new java.awt.BorderLayout());

        jPanel1.add(PSCurrent);

        FromGroup.add(TimeSGiven);
        TimeSGiven.setText(_("From the following time"));
        TimeSGiven.setToolTipText(_("The beginning of the region is the given time"));
        TimeSGiven.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimeSGivenActionPerformed(evt);
            }
        });

        jPanel1.add(TimeSGiven);

        PSGiven.setLayout(new java.awt.BorderLayout());

        jPanel1.add(PSGiven);

        add(jPanel1);

        jPanel2.setLayout(new java.awt.GridLayout(5, 0));

        jPanel2.setBorder(new javax.swing.border.TitledBorder(_("End time")));
        ToGroup.add(TimeFBegin);
        TimeFBegin.setSelected(true);
        TimeFBegin.setText(_("To the end"));
        TimeFBegin.setToolTipText(_("The end of the region is the end of the file"));
        TimeFBegin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimeFBeginActionPerformed(evt);
            }
        });

        jPanel2.add(TimeFBegin);

        ToGroup.add(TimeFCurrent);
        TimeFCurrent.setText(_("To current time"));
        TimeFCurrent.setToolTipText(_("The end of the region is the selected time"));
        TimeFCurrent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimeFCurrentActionPerformed(evt);
            }
        });

        jPanel2.add(TimeFCurrent);

        PFCurrent.setLayout(new java.awt.BorderLayout());

        jPanel2.add(PFCurrent);

        ToGroup.add(TimeFGiven);
        TimeFGiven.setText(_("To the following time"));
        TimeFGiven.setToolTipText(_("The end of the region is the given time"));
        TimeFGiven.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimeFGivenActionPerformed(evt);
            }
        });

        jPanel2.add(TimeFGiven);

        PFGiven.setLayout(new java.awt.BorderLayout());

        jPanel2.add(PFGiven);

        add(jPanel2);

    }
    // </editor-fold>//GEN-END:initComponents
    
    private void TimeFGivenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimeFGivenActionPerformed
        givFTime.setEnabled(true);
    }//GEN-LAST:event_TimeFGivenActionPerformed
    
    private void TimeSGivenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimeSGivenActionPerformed
        givSTime.setEnabled(true);
    }//GEN-LAST:event_TimeSGivenActionPerformed
    
    private void TimeFCurrentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimeFCurrentActionPerformed
        givFTime.setEnabled(false);
    }//GEN-LAST:event_TimeFCurrentActionPerformed
    
    private void TimeSCurrentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimeSCurrentActionPerformed
        givSTime.setEnabled(false);
    }//GEN-LAST:event_TimeSCurrentActionPerformed
    
    private void TimeFBeginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimeFBeginActionPerformed
        givFTime.setEnabled(false);
    }//GEN-LAST:event_TimeFBeginActionPerformed
    
    private void TimeSBeginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimeSBeginActionPerformed
        givSTime.setEnabled(false);
    }//GEN-LAST:event_TimeSBeginActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup FromGroup;
    private javax.swing.JPanel PFCurrent;
    private javax.swing.JPanel PFGiven;
    private javax.swing.JPanel PSCurrent;
    private javax.swing.JPanel PSGiven;
    private javax.swing.JRadioButton TimeFBegin;
    private javax.swing.JRadioButton TimeFCurrent;
    private javax.swing.JRadioButton TimeFGiven;
    private javax.swing.JRadioButton TimeSBegin;
    private javax.swing.JRadioButton TimeSCurrent;
    private javax.swing.JRadioButton TimeSGiven;
    private javax.swing.ButtonGroup ToGroup;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
    public double getStartTime() {
        return getMultiTime(TimeSCurrent, curSTime, TimeSGiven, givSTime, 0d);
    }
    
    public double getFinishTime() {
        return  getMultiTime(TimeFCurrent, curFTime, TimeFGiven, givFTime, Time.MAX_TIME);
    }
    
    private double getMultiTime(JRadioButton currentB, JTimeSpinner currentS, JRadioButton givenB, JTimeSpinner givenS, double other) {
        if ( currentB.isSelected()) return ((Time)currentS.getModel().getValue()).toSeconds();
        if ( givenB.isSelected()) return ((Time)givenS.getModel().getValue()).toSeconds();
        return other;
    }
    
    
    
    public void setButtonsEnabled(boolean value) {
        TimeSBegin.setEnabled(value);
        TimeSCurrent.setEnabled(value);
        TimeSGiven.setEnabled(value);
        TimeFBegin.setEnabled(value);
        TimeFCurrent.setEnabled(value);
        TimeFGiven.setEnabled(value);
        
        givSTime.setEnabled(value && TimeSGiven.isSelected());
        givFTime.setEnabled(value && TimeFGiven.isSelected());
    }
    
    
    
    private void initTime(Time tstart, Time tfinish) {
        setSpinner(curSTime, tstart);
        setSpinner(givSTime, tstart);
        
        setSpinner(curFTime, tfinish);
        setSpinner(givFTime, tfinish);
    }
    
    
    
    private void setSpinner(JTimeSpinner sp, Time t) {
        sp.getModel().setValue(t);
    }
    
}
