from django.core.management.base import BaseCommand
import os
import pygal
from pygal.models import Item


class Command(BaseCommand):
    def handle(self, *args, **options):
        lil = self.lost_item_list()
        if len(lil) == 0:
            self.stdout.write(self.style.SUCCESS('No lost items. Nothing to delete.'))
        else:
            self.stdout.write(self.style.WARNING('Lost Items:'))
            for item in lil:
                self.stdout.write(self.style.WARNING('  - %s' % str(item)))
            response = input('Delete the listed items and all data inside permanently? [y/N]: ')
            if response.lower() == 'y':
                self.stdout.write(self.style.SUCCESS('Deleting Items:'))
                for item in lil:
                    item.delete()
                    self.stdout.write(self.style.SUCCESS('  - Deleted: %s' % str(item)))

    def lost_item_list(self):
        lil = []
        for i in Item.objects.all():
            if not os.path.exists(pygal.get_full_path(i.rel_path)):
                lil.append(i)
        return lil
