<?php

class AudioVideoHandler
{
    private $directories;

    public function __construct()
    {
        echo "[INFO] Fetching directories...";
        $this->directories = glob( DOWNLOAD_PATH . '/*', GLOB_ONLYDIR );
        echo "OK\n";

        $dir = realpath( DOWNLOAD_PATH . '/' . readline( "Artist: " ) );
        $dir = str_replace( "\\", '/', $dir );
        echo "[INFO] Loading results...\n";
        echo $dir;
        $matches = 0;
        foreach ( $this->directories as $index => $_dir )
        {

            if ( ( $lev = levenshtein( $_dir, $dir ) ) < 3 )
            {
                echo "[MATCH#$lev] Did you mean: $_dir? [$index]\n";
                $matches++;
            }
            #echo $lev . "\n";

        }
        if ( $matches === 0 )
        {
            echo "[INFO] No matches found!\n";
            return;
        }

        $index = readline( "Name the index-number: " );
        if ( isset( $this->directories[$index] ) === false )
        {
            echo "[ERROR] Index is invalid!\n";
            return;
        }
        $dir = $this->directories[$index];


        echo "[INFO] Scanning Artist directory...";
        $files = scandir( $dir );
        echo "OK\n";

        if ( count( $files ) < 1 )
        {
            echo "[WARNING] Directory $dir is empty!\n";
            return;
        }
        $_titleOrFull = readline( "Do you want to download all songs or just an explicit title?[A/t]: " );
        if ( stripos( $_titleOrFull, 't' ) !== false )
        {
            echo "\n";
            // Only one
            $song = readline( "Enter the Song name: " );
            echo "[INFO] Searching...\n";
            $matches = 0;
            $artist = basename( $dir );
            foreach ( $files as $index => $name )
            {
                $name = str_replace( array( '.txt', '.mp3', '.mp4' ), '', $name );
                if ( ( $lev = levenshtein( $name, $artist . ' - ' . $song ) ) < 4 )
                {
                    echo "[MATCH#$lev] Did you mean: $name? [$index]\n";
                    $matches++;
                }
            }
            if ( $matches === 0 )
            {
                echo "[WARNING] No matches found!\n";
                return;
            }
            $index = readline( "Name the index-number: " );
            if ( isset( $files[$index] ) === false )
            {
                echo "[ERROR] Index is invalid!\n";
                return;
            }
            $files[$index] = str_replace( array( '.txt', '.mp3', '.mp4' ), '', $files[$index] );
            $this->download( $files[$index] );

        } else
        {
            echo "\n";
            // All files
            echo "[INFO] Downloading " . count( $files ) . " songs...\n";
            foreach ( $files as $file )
            {
                if ( $file === '.' || $file === '..' || strpos( $file, '.mp3' ) !== false || strpos( $file, '.mp4' ) !== false )
                {
                    continue;
                }
                $file = str_replace( array( '.txt', '.mp3', '.mp4' ), '', $file );

                $this->download( $file );

            }
        }
    }


    private function download( $file )
    {
        echo "[INFO] Downloading $file...";
        exec( 'youtube-dl.exe --extract-audio --no-warnings -q --embed-thumbnail --audio-format mp3 --recode-video mp4 --audio-quality 4 -k -o downloaded.%(ext)s "ytsearch: ' . $file . '"' );
        echo "OK\n";
        list( $artist, $title ) = explode( ' - ', $file );
        if ( file_exists( './downloaded.f135.mp4' ) === true )
        {
            rename( "./downloaded.f135.mp4", DOWNLOAD_PATH . '/' . $artist . "/$artist - $title.mp4" );
            echo "[SUCCESS] MP4 saved!\n";
        }
        if ( file_exists( './downloaded.mp3' ) === true )
        {
            rename( "./downloaded.mp3", DOWNLOAD_PATH . '/' . $artist . "/$artist - $title.mp3" );
            echo "[SUCCESS] MP3 saved!\n";
        }

        array_map( 'unlink', glob( "./downloaded.*" ) );
    }

}
