"""Simple application for adjusting image brightness.

Author: CS 149 Instructors
Version: 2/27/2022
"""

from PIL import Image
import tkinter as tk
from tkinter import filedialog
import color_utils


def adjust_brightness(original, amount):
    """Return a new image with the brightness adjusted.

    Args:
        original (Image): The original image
        amount (int):  Amount to adjust the brightness, could be positive or negative.

    Returns:
        Image: A new image with the overall brightness increased or
               decreased as appropriate.

    """
    # Create a copy of the original image and replace all pixel values.
    result = original.copy()
    for x in range(original.width):
        for y in range(original.height):
            pxl = original.getpixel((x, y))
            new_pxl = color_utils.adjust_brightness(pxl, amount)
            result.putpixel((x, y), new_pxl)
    return result


def main():
    """Run the application."""
    root = tk.Tk()
    root.withdraw()

    print("Select an image (must be a jpg)...")
    file = filedialog.askopenfilename(title="Select An Image",
                                            initialdir="./",
                                            initialfile="face.jpg",
                                            filetypes=[("Image files", ".jpg .JPG")])

    amount = int(input("Enter an integer amount to increase or decrease brightness: "))

    im = Image.open(file)
    out = adjust_brightness(im, amount)
    out.show()


if __name__ == "__main__":
    main()
    quit()
