from pathlib import Path

from pymodaq.daq_utils.config import Config
from qtpy import QtWidgets

config = Config()


def select_file(start_path=config('data_saving', 'h5file', 'save_path'), save=True, ext=None, filter=None):
    """Save or open a file with Qt5 file dialog, to be used within an Qt5 loop.

    Usage::

        from pymodaq.daq_utils.daq_utils import select_file
        select_file(start_path="C:\\test.h5",save=True,ext='h5')

    =============== ======================================= ===========================================================================
    **Parameters**     **Type**                              **Description**

    *start_path*       Path object or str or None, optional  the path Qt5 will open in te dialog
    *save*             bool, optional                        * if True, a savefile dialog will open in order to set a savefilename
                                                             * if False, a openfile dialog will open in order to open an existing file
    *ext*              str, optional                         the extension of the file to be saved or opened
    =============== ======================================= ===========================================================================

    Returns
    -------
    Path object
        the Path object pointing to the file

    Examples
    --------



    """
    if ext is None:
        ext = '.h5'

    if filter is None:
        if not save:
            if not isinstance(ext, list):
                ext = [ext]
            filter = "Data files ("
            for ext_tmp in ext:
                filter += '*.' + ext_tmp + " "
            filter += ")"
    
    if start_path is not None:
        if not isinstance(start_path, str):
            start_path = str(start_path)
    if save:
        # fname = QtWidgets.QFileDialog.getSaveFileName(None, 'Enter a .' + ext + ' file name', start_path,
        #                                               ext + " file (*." + ext + ")")
        fname = QtWidgets.QFileDialog.getSaveFileName(None, 'Enter a file name', start_path,
                                                      filter)
    else:
        fname = QtWidgets.QFileDialog.getOpenFileName(None, 'Select a file name', start_path, filter)

    fname = fname[0]
    if fname != '':  # execute if the user didn't cancel the file selection
        fname = Path(fname)
        # if save:
        #     parent = fname.parent
        #     filename = fname.stem
        #     fname = parent.joinpath(filename + "." + ext)  # forcing the right extension on the filename
    return fname  # fname is a Path object


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    file = select_file(save=True, filter="Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)")
    print(file)
    sys.exit(app.exec_())
