#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8:ft=python
#
# Bad Witch
# Copyright © 2020 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#

from appdirs import AppDirs
from pathlib import Path
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
        QAction, qApp, QApplication, QMainWindow, QWidget,
        QFrame, QLabel, QPushButton,
        QGroupBox, QGridLayout, QHBoxLayout, QVBoxLayout
        )
import argparse
import eyed3
import json
import logging
import math
import pathlib
import re
import types
import sys
import youtube_dl

class Library:
    # A thing full of albums
    def __init__(self, file, albums={}):
        self.albums = albums
        self.file = file

    # Load from file
    def load(self):
        try:
            with open(self.file, 'r+') as libfd:
                libfd.seek(0)
                self.albums = json.load(libfd)
        except FileNotFoundError:
            logging.debug('Could not find library, loading empty')
            self.albums = {}
        return

    # Save to file
    def save(self):
        with open(self.file, 'w+') as libfd:
            libfd.seek(0)
            json.dump(self.albums, libfd, indent='\t')
        return

    def validate(self):
        self.load()
        for album, albumcontent in self.albums.items():
            for song, songcontent in albumcontent.items():
                if song == 'meta':
                    continue
                for field in ['track', 'artist', 'source']:
                    if field not in songcontent:
                        raise Exception('Song is missing required field', song, field)

    # Download library
    def download(self, targetalbum=None):
        if targetalbum is not None:
            print('Downloading album: ' + album)
        else:
            print('Downloading entire library')
        for album, albumcontent in self.albums.items():
            # Try to grab a metadata entry
            metadata = {}
            for song, songcontent in albumcontent.items():
                if song == 'meta':
                    metadata = albumcontent['meta']
            # Skip albums that don't match our criterea
            if targetalbum is not None and not album == targetalbum:
                logging.debug('Skipping album ' + album)
                continue
            # Get albumartist
            # Sets to Various Artists if multiple
            albumartist=''
            for song, songcontent in albumcontent.items():
                if song == 'meta':
                    continue
                if albumartist == '':
                    albumartist = songcontent['artist']
                elif albumartist != songcontent['artist']:
                    albumartist = 'Various Artists'
                    break
            destpath = (Path.home() / 'Music' / albumartist / album)
            Path(destpath).mkdir(parents=True, exist_ok=True)
            # Actually download and tag songs
            try:
                for song, songcontent in albumcontent.items():
                    if song == 'meta':
                        continue
                    if metadata == {}:
                        zeroes = int(math.log10(len(albumcontent)) + 1)
                    else:
                        zeroes = int(math.log10(len(albumcontent) - 1) + 1)
                    filename = str(songcontent['track']).zfill(zeroes) + ' - ' + song
                    destfile = str(destpath / filename) + '.%(ext)s'
                    logging.debug('Saving to: ' + destfile)
                    # See if we already have  it
                    if Path(str(destpath / filename) + '.mp3').exists():
                        # Skip downloading
                        logging.info('Already have song: ' + song)
                    else:
                        # Download the song
                        ytdl_opts = {
                                'format': 'bestaudio',
                                'outtmpl': destfile,
                                'playlist_items': 1,
                                'quiet': True,
                                'writethumbnail': True,
                                'postprocessors': [{
                                    'key': 'FFmpegExtractAudio',
                                    'preferredcodec': 'mp3',
                                    'preferredquality': '192'
                                    },{
                                    'key': 'EmbedThumbnail'
                                    }]
                                }
                        with youtube_dl.YoutubeDL(ytdl_opts) as ydl:
                            ydl.download([songcontent['source']])
                        print('Downloaded song: ' + song)
                    # Add tags
                    logging.debug('Adding tags')
                    resultfile = eyed3.load(str(destpath / filename) + '.mp3')
                    resultfile.tag.album_artist = albumartist
                    resultfile.tag.artist = songcontent['artist']
                    resultfile.tag.album = album
                    resultfile.tag.title = song
                    resultfile.tag.track_num = songcontent['track']
                    # Add optional album metadata tags
                    if 'genre' in metadata:
                        resultfile.tag.genre = metadata['genre']
                    if 'publisher' in metadata:
                        resultfile.tag.publisher = metadata['publisher']
                    if 'release_date' in metadata:
                        resultfile.tag.original_release_date = metadata['release_date']
                    if 'composer' in metadata:
                        resultfile.tag.composer = metadata['composer']
                    # Save
                    resultfile.tag.save()
            except (KeyboardInterrupt, EOFError):
                logging.debug('Interrupt received, exiting')

class AlbumWidget(QFrame):
    def __init__(self, album, albumcontent):
        super().__init__()
        self.album = albumcontent
        self.title = album
        self.initUI()

    def initUI(self):
        self.label = QLabel(self.title, self)
        pass

class BadWitchGUI(QMainWindow):
    # A Qt5 GUI
    def __init__(self, badwitch, library):
        super().__init__()
        # Basics
        self.badwitch = badwitch
        self.lib = library
        # Set central widget to something blank
        # We don't care because we can access it later via method
        self.setCentralWidget(QWidget())
        # Initialize our UI
        self.initActions()
        self.initUI()
        # Populate it
        self.populateAlbums()
        # Statusbar
        self.statusBar().showMessage('Ready')

    def initActions(self):
        self.saveAct = QAction(QIcon.fromTheme('document-save'), '&Save', self)
        saveAct = self.saveAct
        saveAct.setShortcut('Ctrl+S')
        saveAct.setStatusTip('Save outstanding library changes')
        saveAct.triggered.connect(self.saveLib)
        self.exitAct = QAction(QIcon.fromTheme('application-exit'), '&Exit', self)
        exitAct = self.exitAct
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit Bad Witch')
        exitAct.triggered.connect(qApp.quit)
        self.refreshAct = QAction(QIcon.fromTheme('view-refresh'), 'Reload Library', self)
        refreshAct = self.refreshAct
        refreshAct.setShortcut('Ctrl+R')
        refreshAct.setStatusTip('Reload albums from library')
        refreshAct.triggered.connect(self.populateAlbums)

    def initUI(self):
        # Main window shenanigans
        self.resize(900, 900)
        self.setWindowTitle('Bad Witch')
        self.setWindowIcon(QIcon.fromTheme('audio-headphones'))
        # Menubar
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(self.saveAct)
        fileMenu.addSeparator()
        fileMenu.addAction(self.exitAct)
        viewMenu = menubar.addMenu('&View')
        viewMenu.addAction(self.refreshAct)

        # Container box
        self.containerBox = QGridLayout()
        containerBox = self.containerBox
        containerBox.setSpacing(10)
        self.centralWidget().setLayout(containerBox)
        # Song browser box
        self.libraryGroup = QGroupBox()
        self.libraryBox = QVBoxLayout()
        self.libraryGroup.setLayout(self.libraryBox)
        containerBox.addWidget(self.libraryGroup, 0, 0)
        # Queue box
        self.queueGroup = QGroupBox()
        self.queueBox = QVBoxLayout()
        self.queueGroup.setLayout(self.queueBox)
        containerBox.addWidget(self.queueGroup, 0, 1, 0, 2)

        # Show
        self.show()

    def populateAlbums(self):
        statusBar = self.statusBar()
        # Remove all albums
        statusBar.showMessage('Reloading library')
        for widget in self.libraryGroup.findChildren(AlbumWidget):
            widget.deleteLater()
        self.lib.load()
        for album, albumcontent in self.lib.albums.items():
            albumWidget = AlbumWidget(album, albumcontent)
            self.libraryBox.addWidget(albumWidget)
        statusBar.showMessage('Library reloaded')

    def saveLib(self):
        statusBar = self.statusBar()
        statusBar.showMessage('Saving library')
        self.lib.save()
        statusBar.showMessage('Library saved')

class BadWitch:
    # Our program
    def __init__(self):
        # Flags and arguments
        self.argparser = argparse.ArgumentParser(
                description='Manage a declarative music library through YouTube scraping')
        self.argparser.add_argument('-l', '--library', metavar='FILE', nargs='?',
                help='Override default library file with this one')
        self.argparser.add_argument('-v', '--verbose', action='store_true',
                help='Show more status messages')
        self.argparser.add_argument('-d', '--debug', action='store_true',
                help='Show even more status messages')
        self.argparser.add_argument('action', metavar='action', nargs='?',
                choices=['download', 'edit', 'playlist', 'gui', 'list'],
                help='Action to perform on the library')
        # Set up appdirs
        self.dirs = AppDirs('badwitch', 'rehashedsalt')
        Path(self.dirs.user_data_dir).mkdir(parents=True, exist_ok=True)

    def execute(self):
        self.args = self.argparser.parse_args() 
        # Parse flags
        if self.args.debug:
            logging.basicConfig(level=logging.DEBUG)
            eyed3.log.level = logging.WARNING
        elif self.args.verbose:
            logging.basicConfig(level=logging.INFO)
            eyed3.log.level = logging.WARNING
        # Initialize library
        libfile = self.args.library or self.dirs.user_data_dir + '/lib.json'
        lib = Library(file=libfile)
        lib.load()
        lib.validate()
        # Perform action
        if self.args.action == 'download':
            lib.download()
            return
        if self.args.action == 'gui':
            qapp = QApplication(sys.argv)
            gui = BadWitchGUI(self, lib)
            sys.exit(qapp.exec_())
        elif self.args.action == 'edit':
            self.prompt(lib)
            return
        elif self.args.action == 'playlist':
            self.playlist(lib)
            return
        elif self.args.action == 'list':
            for album, albumcontent in lib.albums.items():
                print(album)
                for song, songcontent in albumcontent.items():
                    if song == 'meta':
                        continue
                    print('\t' + str(songcontent['track'])
                            + ' - ' + song
                            + ' by ' + songcontent['artist'])
            return
        else:
            print('Nothing to do')
            return

    def playlist(self, lib):
        print('Bad Witch playlist importer')
        print('Loaded library ' + lib.file)
        in_playlist = input('YouTube Playlist URL: ')
        playlist = {}
        metadata = {}
        artist = ''
        album = ''
        # Get our video URLs
        with youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s', 'quiet':True,}) as ydl:
            print('Extracting information...')
            result = ydl.extract_info(in_playlist, download=False)
            if 'entries' in result:
                entries = result['entries']
                for i, item in enumerate(entries):
                    playlist[i] = {
                            'title': entries[i]['title'],
                            'source': entries[i]['webpage_url']
                            }
                    # Song tags like these are per-song, so we have to detect them here
                    if 'artist' in entries[i] and entries[i]['artist'] is not None:
                        if artist != '' and artist != entries[i]['artist']:
                            print('Detected multiple artists')
                            artist = 'Various Artists'
                        elif artist != entries[i]['artist']:
                            print('Detected artist ' + entries[i]['artist'])
                            artist = entries[i]['artist']
                    if 'album' in entries[i] and entries[i]['album'] is not None and album == '':
                        print('Detected album title ' + entries[i]['album'])
                        album = entries[i]['album']
                    # Sometimes this just comes back None
                    if 'release_date' in entries[i] and entries[i]['release_date'] is not None and 'release_date' not in metadata:
                        metadata['release_date'] = str(entries[i]['release_date'])
                        # Format up into a more proper date
                        metadata['release_date'] = metadata['release_date'][:4] + '-' + metadata['release_date'][4:6] + '-' + metadata['release_date'][6:]
                        print('Detected release date ' + metadata['release_date'])
                # If YouTube's song tags didn't work, just grab metadata from the playlist
                if album == '':
                    album = result['title']
                if artist == '':
                    artist = result['uploader']
        # Remove junk from album titles
        for regex in [
                r'^' + re.escape(artist) + r'[ ]*[-:;~]*[ ]*', # Remove artist from beginning
                r'[ ]*[(\[\{]*[0-9]{4}[)\]\}]*$' # Take years off the end
                ]:
            cregex = re.compile(regex, re.IGNORECASE)
            album = cregex.sub('', album)
        # Remove junk from track titles
        for regex in [
                r'^' + re.escape(artist) + r' *[-:;~]* *', # Remove artist from beginning of songs
                r' *\(.*(Official|Video|Audio|Audio|Lyric|VEVO).*\)' # Remove (Offical Audio), (Lyric Video), etc.
                ]:
            cregex = re.compile(regex, re.IGNORECASE)
            for song, songcontent in playlist.items():
                songcontent['title'] = cregex.sub('', songcontent['title'])
        for field in ['genre', 'publisher', 'release_date']:
            if field in metadata:
                continue
            in_value = input('Metadata: ' + field + ' = ')
            if in_value is not '': metadata[field] = in_value
        # Build up an album
        playlistalbum = {}
        playlistalbum['meta'] = metadata
        for song, songcontent in playlist.items():
            playlistalbum[songcontent['title']] = {
                'track': song + 1,
                'artist': artist,
                'source': songcontent['source']
                }
        lib.albums[album] = playlistalbum
        print(f'Successfully added "{album}" by {artist}')
        for song, songcontent in lib.albums[album].items():
            if song == 'meta':
                continue
            print(str(songcontent['track']) + ': ' + song + ' (' + songcontent['source'] + ')')
        lib.save()

    def prompt(self, lib):
        print('Bad Witch interactive library editor')
        print('^C to abort, ^D to finish changes')
        print('Loaded library ' + lib.file)
        try:
            while True:
                in_album = input('\t*Album: ')
                auto_artist = input('\tArtist (leave blank to assign per song): ')
                auto_track = int(input('\tStarting track number (leave blank to assign per song): ') or -1)
                if in_album not in lib.albums:
                    lib.albums[in_album] = {}
                else:
                    print('\tLoaded existing album')
                album = lib.albums[in_album]
                try:
                    # Input metadata
                    if 'meta' not in album:
                        album['meta'] = {}
                    metadata = album['meta']
                    # Metadata fields sorted in order of popularity... ish
                    for field in ['genre', 'publisher', 'release_date', 'composer']:
                        in_value = input('\t' + field + ': ')
                        if in_value is not '': metadata[field] = in_value
                    # Input songs
                    while True:
                        in_song = input('\t\t*Song title: ')
                        if auto_artist is not '':
                            in_artist = auto_artist
                        else:
                            in_artist = input('\t\t*Artist: ')
                        if auto_track is not -1:
                            in_track = auto_track
                            auto_track += 1
                        else:
                            in_track = input('\t\t*Track number: ')
                        in_source = input('\t\t*Source URL: ')
                        # Only assign values if we gave them
                        if in_song not in album:
                            album[in_song] = {}
                        else:
                            print('\t\tLoaded existing song')
                        song = album[in_song]
                        if in_track is not '': song['track'] = int(in_track)
                        if in_artist is not '': song['artist'] = in_artist
                        if in_source is not '': song['source'] = in_source
                        # Bail if song is bad
                        if in_song in ['meta']:
                            print('\t\tError: Song title collides with a reserved field: ' + in_song)
                            continue
                        if '' in [in_song, in_track, in_artist, in_source]:
                            print('\t\tError: A critical field was empty')
                            continue
                        album[in_song] = song
                except KeyboardInterrupt:
                    print('\n\t\tAborting, changes were not saved')
                except EOFError:
                    album[in_song] = song
                    print('\n\t\tChanges cached, ^D again to save')
                lib.albums[in_album] = album
                lib.save()
        except KeyboardInterrupt:
            print('\n\tAborting, changes were not saved')
        except EOFError:
            lib.save()
            print('\n\tSaving changes')
        print('Closing library')
        return

eyed3.log.level = logging.CRITICAL
badwitch = BadWitch()
badwitch.execute()