# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021-2022 Philipp Wolfer
# Copyright (C) 2021-2024 Laurent Monin
#
# This program 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; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


import os
import os.path

from PyQt6.QtCore import (
    QCoreApplication,
    QStandardPaths,
)

from picard import (
    PICARD_APP_NAME,
    PICARD_ORG_NAME,
)


# Ensure the application is properly configured for the paths to work
QCoreApplication.setApplicationName(PICARD_APP_NAME)
QCoreApplication.setOrganizationName(PICARD_ORG_NAME)


def config_folder():
    return os.path.normpath(
        os.environ.get(
            'PICARD_CONFIG_DIR', QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppConfigLocation)
        )
    )


def cache_folder():
    return os.path.normpath(
        os.environ.get(
            'PICARD_CACHE_DIR', QStandardPaths.writableLocation(QStandardPaths.StandardLocation.CacheLocation)
        )
    )


def plugin_folder():
    appdata_folder = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppDataLocation)
    return os.path.normpath(os.environ.get('PICARD_PLUGIN_DIR', os.path.join(appdata_folder, 'plugins3')))


def sessions_folder():
    """Get the sessions folder path.

    Returns
    -------
    str
        The path to the sessions folder. If a custom path is configured,
        returns that path. Otherwise, returns the default path
        <config_folder>/sessions.
    """
    from pathlib import Path

    from picard.config import get_config

    config = get_config()
    custom_path = config.setting['session_folder_path']
    if custom_path:
        return str(Path(custom_path).resolve())
    else:
        return str(Path(config_folder()) / 'sessions')
