#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from faint_scripts.log import Logger

log = Logger("run-test-props.py")

assert(len(images) == 1)

def test_file(name):
    return os.path.join(os.getcwd(), "out", name)

def create_file(path):
    with open(path,'w') as f:
        f.write("Generated by run-test-props.py")

create_file(test_file("file.noframe"))
create_file(test_file("file.oldprops"))
create_file(test_file("file.color"))
create_file(test_file("file.bitmap"))

# Format that add no frame should
# Should cause an error message.
def no_frame(file_path, image_props):
    pass

add_format(no_frame, None, "No frame", "noframe")
app.open_files((test_file("file.noframe"),))

log.error_if(len(images) != 1, 'An image was added from "noframe" format')

# Format which stores its props so that the expiration behavior can be
# tested.
old_props = []
def store_expired_props(file_path, image_props):
    old_props.append(image_props)
    old_props.append(image_props.add_frame((10,10)))

add_format(store_expired_props, None, "Old props", "oldprops")
app.open_files((test_file("file.oldprops"),))

log.error_if(len(images) != 2, "Failed loading file.oldprops")

image_props, frame_props = old_props
assert(str(image_props) == "Retired ImageProps")
log.error_if(str(image_props) != "Retired ImageProps",
             str(image_props) + ' != "Retired ImageProps"')

log.error_if(str(frame_props) != 'Retired FrameProps',
             str(frame_props) + ' != "Retired FrameProps"')

try:
    old_props[0].add_frame(640,480)
except ValueError as v:
    assert(str(v) == "Operation attempted on expired ImageProps")
else:
    assert False # Expected exception

try:
    old_props[1].Line((0,0,10,10))
except ValueError as v:
    assert(str(v) == "Operation attempted on expired FrameProps")
else:
    assert False # Expected exception


# Color background instead of Bitmap.
# (Allows less memory use when loading large vector images)
def color_background(file_path, image_props):
    frame = image_props.add_frame((10,10))
    frame.set_background(((255,0,255),(10,10)))

add_format(color_background, None, "Color background", "color")
app.open_files((test_file("file.color"),))

assert len(images) == 3

# No bitmap allocated
assert images[2].get_background() is None


# Bitmap background
def bitmap_background(file_path, image_props):
    frame = image_props.add_frame()
    frame.set_background(Bitmap((10,10),(255,0,0)))

add_format(bitmap_background, None, "Bitmap background", "bitmap")
app.open_files((test_file("file.bitmap"),))

assert len(images) == 4
assert(images[3].get_background().get_size() == (10,10))

os.remove(test_file("file.noframe"))
os.remove(test_file("file.oldprops"))
os.remove(test_file("file.color"))
os.remove(test_file("file.bitmap"))
