#! /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
import argparse
import json
import logging
import pathlib
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):
        with open(self.file, 'r+') as libfd:
            libfd.seek(0)
            self.albums = json.load(libfd)
        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()
        try:
            for album, albumcontent in self.albums.items():
                for song, songcontent in albumcontent.items():
                    if songcontent['artist'] is None:
                        raise Exception
        except:
            logging.warn('Library not valid')
            raise Exception('Library not valid')

    # 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():
            # Skip albums that don't match our criterea
            if targetalbum is not None and not album == targetalbum:
                logging.debug('Skipping album ' + album)
                continue
            # God have mercy on my soul
            artist = next(iter(albumcontent.values()))['artist']
            destpath = (Path.home() / 'Music' / artist / album)
            Path(destpath).mkdir(parents=True, exist_ok=True)
            for song, songcontent in albumcontent.items():
                # See if we already have  it
                matches = sorted(Path(destpath).glob(song + '.*'))
                if not matches == []:
                    logging.debug('Using cached song: ' + song)
                    continue
                # Download the song
                ytdl_opts = {
                        'audio-format': 'best',
                        'x'
                        'playlist-items': 1
                        }
                with youtube_dl.YoutubeDL(ytdl_opts) as ydl:
                    ydl.download([songcontent['source']])
                logging.info('Downloaded song: ' + song)

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='f', 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', 'list', 'test'],
                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)
        elif self.args.verbose:
            logging.basicConfig(level=logging.INFO)
        # 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
        elif self.args.action == 'list':
            for album, albumcontent in lib.albums.items():
                for song, songcontent in albumcontent.items():
                    print(album
                            + ' - ' + str(songcontent['track'])
                            + ' - ' + song
                            + ' by ' + songcontent['artist'])
            return
        elif self.args.action == 'test':
            # Set up a test album
            lib.albums['Bad Witch'] = {
                    'Shit Mirror': {
                        'track': 1,
                        'artist': 'Nine Inch Nails',
                        'source': 'https://www.youtube.com/watch?v=yeqjz5mXrLQ'
                        },
                    'Ahead of Ourselves': {
                        'track': 2,
                        'artist': 'Nine Inch Nails',
                        'source': 'https://www.youtube.com/watch?v=4Ab1O-i4ep4'
                        },
                    'Play the Goddamned Part': {
                        'track': 3,
                        'artist': 'Nine Inch Nails',
                        'source': 'https://www.youtube.com/watch?v=85UgvBkMfr8'
                        },
                    'God Break Down the Door': {
                        'track': 4,
                        'artist': 'Nine Inch Nails',
                        'source': 'https://www.youtube.com/watch?v=eeJ_DzRJUI4'
                        },
                    'I\'m Not From This World': {
                        'track': 5,
                        'artist': 'Nine Inch Nails',
                        'source': 'https://www.youtube.com/watch?v=9fjbcSUSt9w'
                        },
                    'Over and Out': {
                        'track': 6,
                        'artist': 'Nine Inch Nails',
                        'source': 'https://www.youtube.com/watch?v=h-XlN3N2fis'
                        }
                    }
            lib.save()
            return

badwitch = BadWitch()
badwitch.execute()