import numpy as np
import os
import cv2 as cv
from PIL import Image


def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    # NOTE: the two images must have the same dimension
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])

    # return the MSE, the lower the error, the more "similar"
    # the two images are
    return err


def mse_with_reshape(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    # NOTE: the two images must have the same dimension

    if len(imageA) == 0 or len(imageB) == 0:
        return 999
    try:
        imga = np.reshape(imageA, (len(imageA) * 17, 740, 3), 'C')
        imgb = np.reshape(imageB, (len(imageB) * 17, 740, 3), 'C')

        err = np.sum((imga.astype("float") - imgb.astype("float")) ** 2)
        err /= float(imga.shape[0] * imga.shape[1])

        # return the MSE, the lower the error, the more "similar"
        # the two images are
        return err
    except:
        return 999


def load_bl_line():
    list_store = []
    for path in os.listdir('bl-lines'):
        if path.endswith(".npy"):
            list_store.append(np.load("bl-lines\\" + path))
    return list_store


def check_for_bl_line(line_item, black_list):
    for bl_item in black_list:
        if mse(line_item, bl_item) < 500:
            return True
    return False


def scale_screenshot(screenshot):
    scale_percent = 200  # percent of original size
    width = int(screenshot.shape[1] * scale_percent / 100)
    height = int(screenshot.shape[0] * scale_percent / 100)
    dim = (width, height)
    resized_img = cv.resize(screenshot, dim, interpolation=4)

    gray = cv.cvtColor(resized_img, cv.COLOR_BGR2GRAY)
    thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1]
    # cv.imshow("Tresh", thresh)
    # cv.waitKey(1000)
    return thresh


def save_line_item_npy_jpg(short_pic_list):
    ifr = 1
    for x in short_pic_list:
        np.save("test{}".format(ifr), x)
        Image.fromarray(x).save("test{}.jpg".format(ifr))
        ifr = ifr + 1


def shorten_picture_input(new_pic_lst, old_pic_lst):
    len_old = len(old_pic_lst)
    len_new = len(new_pic_lst)
    offset = 0
    offset_diff = len_new - len_old
    retval = []

    while mse_with_reshape(old_pic_lst[offset: len_old], new_pic_lst[0: len_new - offset - offset_diff]) > 500.0:
        offset = offset + 1
        # print(offset)
        if offset == len(old_pic_lst):
            break

    if offset > 0:
        retval = new_pic_lst[len_new - offset: len_new]
    return retval


def shorten_input(new_lst, old_lst):
    len_old = len(old_lst)
    len_new = len(new_lst)
    offset = 0
    offset_diff = len_new - len_old
    retval = []

    while old_lst[offset: len_old] != new_lst[0: len_new - offset - offset_diff]:
        offset = offset + 1
        # print(offset)

    if offset > 0:
        retval = new_lst[len_new - offset: len_new]

    return retval


def format_output(data):
    new_list = data.split("\n")

    try:
        while True:
            # new_list.remove() ("",)
            new_list.remove('\x0c')
    except ValueError:
        pass
    return new_list


def update_screenshot_with_short_pic_list(short_pic_lst, lst_bl_items):
    screenshot = []
    # check if shorten line pictures list has any entries
    if len(short_pic_lst) > 0:
        index_lst = []
        # check if any of the line item pictures is on the blacklist and save index
        for x in range(len(short_pic_lst)):
            if check_for_bl_line(short_pic_lst[x], lst_bl_items):
                index_lst.append(x)

        # remove blacklisted items from shorten list
        r_short_pic_lst = np.delete(short_pic_lst, index_lst, axis=0)
        # check if further shortened line pictures list has any entries
        if len(r_short_pic_lst) == 0:
            return screenshot

        # combine shorten line picture list to one picture
        screenshot = np.reshape(r_short_pic_lst, (len(r_short_pic_lst) * 17, 740, 3), 'C')

        # saves all line item pictures to disk for blacklist evaluation
        # save_line_item_npy_jpg(r_short_pic_lst)

    return screenshot
