<?php
/*
 *
 */

namespace Sikofitt\Hathor\Command;

use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Sikofitt\Hathor\Common\Music\ID3;

class ScannerCommand extends BaseCommand
{

    protected function configure()
    {
        $this
            ->setName('scan')
            ->setDescription('Scans Music')
            ->addOption('directory', 'd', InputOption::VALUE_OPTIONAL, 'Directory to scan for music.',BASEDIR)
            ->addArgument('extensions',InputArgument::IS_ARRAY, 'Extensions to scan for separated by a space',['mp2','mp3','mp4','wma','ogg']);


    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $extensions = $input->getArgument('extensions');

        // \b(\.mp2|\.mp3|\.mp4|\.wma|\.ogg)\b$
        $extensionRegex = '/\b(';
        foreach($extensions as $extension) {
            $extensionRegex .= sprintf('\.%s|',$extension);
        }


        $extensionRegex[strlen($extensionRegex)-1] = ')';
        $extensionRegex .= '\b$/i';

        $output->writeln("\n Please wait ... finding files.\n");
        $finder = new Finder();
        $finder
            ->files()
            ->in($input->getOption('directory'))
            ->ignoreDotFiles(true)
            ->ignoreUnreadableDirs(true)
            ->name($extensionRegex);
        $progress = new ProgressBar($output, $finder->count());
        $progress->setBarWidth(50);
        $progress->setFormat(" %message%\n <fg=cyan>%current%</><fg=black;options=bold>/</><fg=cyan;options=bold>%max%</> <fg=cyan>[</>%bar%<fg=cyan>]</> %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%");
        $progress->clear();
        $progress->start();
        $getID3 = new ID3;
        $progress->setBarCharacter('<fg=cyan;options=bold>#</>');
        $progress->setProgressCharacter('<fg=cyan>#</>');
        $progress->setEmptyBarCharacter('<fg=cyan>#</>');
        //$progress->setRedrawFrequency(100);

        $cols = shell_exec('tput cols') - 20;
        foreach ($finder as $file) {
            $progress->setMessage(str_repeat('-', $cols - 5));
            //$currentDir = explode(DIRECTORY_SEPARATOR, $file->getRelativePath());
           //$currentDir = $currentDir[count($currentDir)-1];
           //$currentDir = substr($currentDir[count($currentDir)-1],0,60);

            $currentFile = str_pad(substr($file->getFilename(), 0, $cols) . ' ...', $cols, ' ');

            $progress->setMessage("<fg=white;options=bold>Current File - $currentFile </>");

            $getID3->setFile($file->getRealPath());
            try {

                $allMusicData[] = $getID3->getFileData ();
            } catch(\Exception $e) {
                $progress->setMessage('<warning>Caught RuntimeException</warning>');
                continue;
            }

            $progress->advance();

        }
        $progress->setMessage('Finished!');
        $progress->finish();
        $output->writeln('');
        //var_dump($allMusicData);



    }
}
