#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Stashr - File Naming
"""

"""
MIT License

Copyright (c) 2020 Andrew Vanderbye

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

"""-------------------------------------------------------------------------------------------
-- IMPORTS
-------------------------------------------------------------------------------------------"""

""" --- HUEY IMPORT --- """
""" --- PYTHON IMPORTS --- """
""" --- STASHR DEPENDENCY IMPORTS --- """
from pathvalidate import sanitize_filename

""" --- STASHR CORE IMPORTS --- """
from stashr import log, database
from stashr.config import stashrconfig

""" --- CREATE LOGGER --- """
logger = log.stashr_logger(__name__)

"""-------------------------------------------------------------------------------------------
-- FILE AND FOLDER NAMING
-------------------------------------------------------------------------------------------"""


def volume_folder_name_from_cv_item(volume):

    # name_scheme = "{volume_name} ({volume_year}) [{volume_id}]"
    name_scheme = stashrconfig['NAMING']['folder']

    print(volume.results['name'])

    return sanitize_filename(
            name_scheme.format(
                volume_id = volume.results['id'],
                volume_name = volume.results['name'],
                volume_year = volume.results['start_year'],
                volume_publisher_id = volume.results['publisher']['id'],
                publisher_name = volume.results['publisher']['name'],
            ))


def folder_name(volume_id):

    # name_scheme = "{volume_name} ({volume_year}) [{volume_id}]"
    name_scheme = stashrconfig['NAMING']['folder']

    volume = database.session \
        .query(database.Volumes) \
        .filter(database.Volumes.volume_id == volume_id) \
        .first()

    return name_scheme.format(
        volume_id = volume.volume_id,
        volume_name = volume.volume_name,
        volume_year = volume.volume_year,
        volume_publisher_id = volume.volume_publisher_id,
        volume_url = volume.volume_url,
        volume_slug = volume.volume_slug,
        volume_sort_title = volume.volume_sort_title,
        publisher_name = volume.publisher.publisher_name,
    )


def file_name_by_id(issue_id):

    # name_scheme = "{volume_name} - {issue_number:0>3}"
    name_scheme = stashrconfig['NAMING']['file']
    
    issue = database.session \
        .query(database.Issues) \
        .filter(database.Issues.issue_id == issue_id) \
        .first()

    return name_scheme.format(
        issue_id = issue.issue_id,
        volume_name = issue.volume.volume_name,
        issue_number = issue.issue_number,
        volume_id = issue.volume.volume_id,
        issue_name = issue.issue_name,
        release_date = issue.issue_release_date,
        description = issue.issue_description,
        volume_description = issue.volume.volume_description,
        volume_slug = issue.volume.volume_slug
    )


def file_name_by_db(issue):

    # name_scheme = "{volume_name} - {issue_number:0>3}"
    name_scheme = stashrconfig['NAMING']['file']

    return sanitize_filename(
        name_scheme.format(
            issue_id = issue.issue_id,
            volume_name = issue.volume.volume_name,
            issue_number = issue.issue_number,
            volume_id = issue.volume.volume_id,
            issue_name = issue.issue_name,
            release_date = issue.issue_release_date,
            description = issue.issue_description,
            volume_description = issue.volume.volume_description,
            volume_slug = issue.volume.volume_slug
        )
    )