# -*- encoding: utf-8 -*-
import sys
from django.core.management.base import BaseCommand, CommandParser, CommandError
from ...generator import ImageCaptchaGenerator


class Command(BaseCommand):
    help = 'Generates a CAPTCHA challenge and writes PNG bytes into stdin.'

    def add_arguments(self, parser: CommandParser):
        parser.add_argument('secret', action='store', default='', type=str,
                            nargs=1, help='The not-so-secret text that will be on the image')

    def handle(self, **options):
        secret: str = next(iter(options['secret']))
        png_img: bytes = ImageCaptchaGenerator().generate_bytes(secret)
        sys.stdout.buffer.write(png_img)
