import argparse
import sys
from . import split, merge, compress, encrypt, watermark

def split_command(args):
    split.split_pdf(args.input, args.output_folder)

def merge_command(args):
    merge.merge_pdfs(args.input_files, args.output)

def compress_command(args):
    compress.compress_pdf(args.input, args.output, args.quality)

def encrypt_command(args):
    encrypt.encrypt_pdf(args.input, args.output, args.password)

def decrypt_command(args):
    encrypt.decrypt_pdf(args.input, args.output, args.password)

def watermark_command(args):
    watermark.add_watermark(args.input, args.watermark, args.output)

def main():
    parser = argparse.ArgumentParser(description='PDF Tools - Various utilities for handling PDFs')
    subparsers = parser.add_subparsers(dest='command', help='Commands')

    # Split command
    split_parser = subparsers.add_parser('split', help='Split PDF into individual pages')
    split_parser.add_argument('input', help='Input PDF file')
    split_parser.add_argument('--output-folder', '-o', default='output', help='Output folder')

    # Merge command
    merge_parser = subparsers.add_parser('merge', help='Merge multiple PDFs')
    merge_parser.add_argument('input_files', nargs='+', help='Input PDF files')
    merge_parser.add_argument('--output', '-o', required=True, help='Output PDF file')

    # Compress command
    compress_parser = subparsers.add_parser('compress', help='Compress PDF')
    compress_parser.add_argument('input', help='Input PDF file')
    compress_parser.add_argument('--output', '-o', required=True, help='Output PDF file')
    compress_parser.add_argument('--quality', '-q', type=int, default=50, help='Compression quality (0-100)')

    # Encrypt command
    encrypt_parser = subparsers.add_parser('encrypt', help='Encrypt PDF')
    encrypt_parser.add_argument('input', help='Input PDF file')
    encrypt_parser.add_argument('--output', '-o', required=True, help='Output PDF file')
    encrypt_parser.add_argument('--password', '-p', required=True, help='Encryption password')

    # Decrypt command
    decrypt_parser = subparsers.add_parser('decrypt', help='Decrypt PDF')
    decrypt_parser.add_argument('input', help='Input PDF file')
    decrypt_parser.add_argument('--output', '-o', required=True, help='Output PDF file')
    decrypt_parser.add_argument('--password', '-p', required=True, help='Decryption password')

    # Watermark command
    watermark_parser = subparsers.add_parser('watermark', help='Add watermark to PDF')
    watermark_parser.add_argument('input', help='Input PDF file')
    watermark_parser.add_argument('--watermark', '-w', required=True, help='Watermark PDF file')
    watermark_parser.add_argument('--output', '-o', required=True, help='Output PDF file')

    args = parser.parse_args()

    if args.command == 'split':
        split_command(args)
    elif args.command == 'merge':
        merge_command(args)
    elif args.command == 'compress':
        compress_command(args)
    elif args.command == 'encrypt':
        encrypt_command(args)
    elif args.command == 'decrypt':
        decrypt_command(args)
    elif args.command == 'watermark':
        watermark_command(args)
    else:
        parser.print_help()
        sys.exit(1)

if __name__ == '__main__':
    main() 