//***************************************************************************************************************************************************
//
// 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 lib ;

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




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

import java.io.File                                    ;
import java.awt.Component                              ;
import javax.swing.JOptionPane                         ;
import javax.swing.JFileChooser                        ;
import javax.swing.filechooser.FileFilter              ;
import javax.swing.filechooser.FileNameExtensionFilter ;

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




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

public class VerifiedFileChooser
{
  //=================================================================================================================================================

  private       Component    parent                 ;
  private final FileFilter   fileFilter             ;
  private final JFileChooser fileChooser            ;
  private       File         lastVerifiedOpenTarget ;
  private       File         lastVerifiedSaveTarget ;

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

  public VerifiedFileChooser
  (
    Component  parent                           ,  // Parent swing component
    File       initialOpenTarget                ,  // Will be offered as the initial default open target, can be null
    File       initialSaveTarget                ,  // Will be offered as the initial default save target, can be null
    String     descriptionForAcceptedExtensions ,
    String ... acceptedExtensions
  )
  {
    this.parent                 = parent                                                                               ;
    this.fileFilter             = new FileNameExtensionFilter( descriptionForAcceptedExtensions , acceptedExtensions ) ;
    this.fileChooser            = new JFileChooser()                                                                   ;
    this.lastVerifiedOpenTarget = initialOpenTarget                                                                    ;
    this.lastVerifiedSaveTarget = initialSaveTarget                                                                    ;

    fileChooser.setFileFilter         ( fileFilter ) ;
    fileChooser.addChoosableFileFilter( fileFilter ) ;
  }

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

  public void setParent                 ( Component parent                 )  { this.parent                 = parent                 ; }
  public void setLastVerifiedOpenTarget ( File      lastVerifiedOpenTarget )  { this.lastVerifiedOpenTarget = lastVerifiedOpenTarget ; }
  public void setLastVerifiedSaveTarget ( File      lastVerifiedSaveTarget )  { this.lastVerifiedSaveTarget = lastVerifiedSaveTarget ; }

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

  public JFileChooser getFileChooser            ()  { return fileChooser            ; }
  public File         getLastVerifiedOpenTarget ()  { return lastVerifiedOpenTarget ; }
  public File         getLastVerifiedSaveTarget ()  { return lastVerifiedSaveTarget ; }

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

  public File getOpenTarget
  (
    String title      ,  // File chooser       dialog title
    String errorTitle    // File missing error dialog title
  )
  {
    fileChooser.setDialogTitle( title ) ;

    if ( lastVerifiedOpenTarget != null )  { fileChooser.setSelectedFile( lastVerifiedOpenTarget ) ; }

    if ( fileChooser.showOpenDialog( parent ) != JFileChooser.APPROVE_OPTION )  { return null ; }

    File file = fileChooser.getSelectedFile() ;

    if ( ! file.exists() )
    {
      String message = file.getName() + " does not exist!" ;

      JOptionPane.showMessageDialog( parent , message , errorTitle , JOptionPane.ERROR_MESSAGE ) ;

      return null ;
    }

    lastVerifiedOpenTarget = file ;

    return file ;
  }

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

  public File getSaveTarget
  (
    String title        ,  // File chooser           dialog title
    String confirmTitle    // File overwrite warning dialog title
  )
  {
    fileChooser.setDialogTitle( title ) ;

    if ( lastVerifiedSaveTarget != null )  { fileChooser.setSelectedFile( lastVerifiedSaveTarget ) ; }

    if ( fileChooser.showSaveDialog( parent ) != JFileChooser.APPROVE_OPTION )  { return null ; }

    File file = fileChooser.getSelectedFile() ;

    if ( file.exists() )
    {
      String message = file.getName() + " already exists.\n" + "Do you want to replace it?" ;

      int choice = JOptionPane.showConfirmDialog( parent , message , confirmTitle , JOptionPane.YES_NO_OPTION , JOptionPane.WARNING_MESSAGE ) ;

      if ( choice != 0 )  { return null ; }
    }

    lastVerifiedSaveTarget = file ;

    return file ;
  }

  //=================================================================================================================================================
}

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