#!/usr/bin/ python3

"""
Converter

converter is standalone python script for converting betwen different image formats.
This script can convert jpegs, png and others from one format to the other
"""

from signal import SIGINT, signal
# configure signal for execution abort using ctr-c
signal(SIGINT, lambda signum, frame: sys.exit(1))

import os
import sys
from argparse import ArgumentParser
import logging

from PIL import Image

# configure logging level
logging.basicConfig(level=logging.DEBUG)


def main():
    # Exit on ctrl-c
    def handler(signum, frame):
        cancel(1)

    # Register handler
    signal(SIGINT, handler)

    # Parse command-line arguments
    parser = ArgumentParser(description="A command-line tool for converting betwen different image formats.")
    parser.add_argument("-i", "--input", type=str, help="path to the input file")
    parser.add_argument("-o", "--output", type=str, help="path to the output file")
    args = parser.parse_args(sys.argv[1:])

    # Check for input
    if args.input and os.path.exists(args.input):
        input_file = os.path.abspath(args.input)
    else:
        print("Invalid input")
        sys.exit()

    # Check for output
    if args.output:
        output_file = os.path.abspath(args.output)
    else:
        logging.error("invalid output")
        sys.exit()

    logging.info(f"converting {input_file} --> {output_file}")

    converter = Converter(input=input_file, output=output_file)
    converter.convert()

def cancel(code=0):
    sys.exit(code)


class Converter:

    def __init__(self, input, output):
        self.input = input
        self.output = output

    def convert(self):
        if self.output.endswith("jpeg"):
            self.convert_to_jpeg()
        elif self.output.endswith("png"):
            self.convert_to_png()
        else:
            raise NotImplementedError

    def convert_to_jpeg(self):
        try:
            Image.open(self.input).convert("RGB").save(self.output)
            logging.info(f"conversion successfull \nOUTPUT: {self.output}")
        except OSError as error:
            logging.error(f"conversion unsuccessfull {error}")

    def convert_to_png(self):
        try:
            Image.open(self.input).convert("RGB").save(self.output)
            logging.info(f"conversion successfull \nOUTPUT: {self.output}")
        except OSError as error:
            logging.error(f"conversion unsuccessfull {error}")

if __name__ == "__main__":
    main()