//***************************************************************************************************************************************************
//
// Copyright (C) 2021 Selim Temizer.
//
// This file is part of Course Statistics.
//
// Course Statistics is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// Course Statistics 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Course Statistics. If not, see <http://www.gnu.org/licenses/>.
//
//***************************************************************************************************************************************************




//***************************************************************************************************************************************************

package gui ;

//***************************************************************************************************************************************************




//***************************************************************************************************************************************************

import java.awt.GridLayout        ;
import javax.swing.JLabel         ;
import javax.swing.ImageIcon      ;
import javax.swing.JTextField     ;
import javax.swing.SwingConstants ;

//***************************************************************************************************************************************************




//***************************************************************************************************************************************************

public class GuiData2 extends GuiDataSource
{
  //=================================================================================================================================================

  private        final JTextField [][] data                                     ;
  private static final long            serialVersionUID = -2250335527530804401L ;

  //=================================================================================================================================================

  public GuiData2 ( int numAssessments , int numCLOs )
  {
    //-----------------------------------------------------------------------------------------------------------------------------------------------

    initComponents() ;

    //-----------------------------------------------------------------------------------------------------------------------------------------------

    this.data = new JTextField[ numAssessments ][ numCLOs + 3 ] ;

    ImageIcon icon = new ImageIcon( this.getClass().getResource( "/media/Icon.png" ) ) ;    setIconImage( icon.getImage() ) ;

    //-----------------------------------------------------------------------------------------------------------------------------------------------
    // Panel

    panel.setLayout( new GridLayout( /* rows */ numAssessments + 1 , /* cols */ numCLOs + 4 , /* hgap */ 1 , /* vgap */ 1 ) ) ;

    //-----------------------------------------------------------------------------------------------------------------------------------------------
    // Header row

    panel.add( new JLabel( "<html><center>" + "<span style='color:red;'>Data</span>" + "</center>" , SwingConstants.CENTER ) ) ;
    panel.add( new JLabel( "<html><center>" + "Name<br>[ label ]"                    + "</center>" , SwingConstants.CENTER ) ) ;
    panel.add( new JLabel( "<html><center>" + "% Weight<br>[ 0 ]"                    + "</center>" , SwingConstants.CENTER ) ) ;
    panel.add( new JLabel( "<html><center>" + "Max Grade<br>[ 100 ]"                 + "</center>" , SwingConstants.CENTER ) ) ;

    for ( int c = 1 ; c <= numCLOs ; c ++ )
    {
      panel.add( new JLabel( "<html><center><span style='color:blue;'>% CLO-" + c + "</span><br>[ 0 ]</center>" , SwingConstants.CENTER ) ) ;
    }

    //-----------------------------------------------------------------------------------------------------------------------------------------------
    // Body rows

    for ( int row = 0 ; row < numAssessments ; row++ )
    {
      panel.add( new JLabel( "Assessment-" + ( row + 1 ) + " " ) ) ;

      for ( int col = 0 ; col < numCLOs + 3 ; col++ )  { data[ row ][ col ] = new JTextField() ;    panel.add( data[ row ][ col ] ) ; }
    }

    for ( int row = 0 ; row < numAssessments ; row++ ) {
      for ( int col = 1 ; col < numCLOs + 3 ; col++ ) {
        data[row][col].setHorizontalAlignment( JTextField.CENTER ) ; } }

    //-----------------------------------------------------------------------------------------------------------------------------------------------

    pack() ;

    //-----------------------------------------------------------------------------------------------------------------------------------------------
  }

  //=================================================================================================================================================
  // "assessmentNo" and "cloNo" start from 1

  @Override public String getAssessmentName ( int assessmentNo )
  {
    try
    {
      String name = data[ assessmentNo - 1 ][ 0 ].getText().trim() ;

      if ( name.isEmpty() )  { throw new RuntimeException( "Empty Name" ) ; }

      return name ;
    }
    catch ( Exception e )  { return "Assessment-" + assessmentNo ; }
  }

  @Override public double getAssessmentWeight ( int assessmentNo )
  {
    try { return Double.parseDouble( data[ assessmentNo - 1 ][ 1         ].getText().trim() ) ; } catch ( Exception e )  { return   0.0 ; }
  }

  @Override public double getAssessmentMaxGrade ( int assessmentNo )
  {
    try { return Double.parseDouble( data[ assessmentNo - 1 ][ 2         ].getText().trim() ) ; } catch ( Exception e )  { return 100.0 ; }
  }

  @Override public double getAssessmentCLO ( int assessmentNo , int cloNo )
  {
    try { return Double.parseDouble( data[ assessmentNo - 1 ][ cloNo + 2 ].getText().trim() ) ; } catch ( Exception e )  { return   0.0 ; }
  }

  //=================================================================================================================================================

  /**
   * 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.
   */
  @SuppressWarnings( "unchecked" )
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents()
  {

    l1 = new javax.swing.JLabel();
    panel = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Assessments and CLOs");
    setLocationByPlatform(true);

    l1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    l1.setText("<html>Do <span style='color:red;'>not</span> close this window before generating and saving the excel file.");

    javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
    panel.setLayout(panelLayout);
    panelLayout.setHorizontalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 0, Short.MAX_VALUE)
    );
    panelLayout.setVerticalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 78, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(l1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  /**
   * @param args the command line arguments
   */
  public static void main ( String args[] )
  {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try
    {
      for ( javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels() )
      {
        if ( "Windows".equals( info.getName() ) )
        {
          javax.swing.UIManager.setLookAndFeel( info.getClassName() );
          break;
        }
      }
    }
    catch ( ClassNotFoundException ex )
    {
      java.util.logging.Logger.getLogger(GuiData2.class.getName() ).log( java.util.logging.Level.SEVERE , null , ex );
    }
    catch ( InstantiationException ex )
    {
      java.util.logging.Logger.getLogger(GuiData2.class.getName() ).log( java.util.logging.Level.SEVERE , null , ex );
    }
    catch ( IllegalAccessException ex )
    {
      java.util.logging.Logger.getLogger(GuiData2.class.getName() ).log( java.util.logging.Level.SEVERE , null , ex );
    }
    catch ( javax.swing.UnsupportedLookAndFeelException ex )
    {
      java.util.logging.Logger.getLogger(GuiData2.class.getName() ).log( java.util.logging.Level.SEVERE , null , ex );
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater( new Runnable()
    {
      @Override public void run ()  { new GuiData2( 3 , 5 ).setVisible( true ) ; }
    } ) ;
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel l1;
  private javax.swing.JPanel panel;
  // End of variables declaration//GEN-END:variables
}

//***************************************************************************************************************************************************