import os
import re
import shutil

from django.conf import settings

WORKDIR = os.path.join(settings.ASSET_DIR, 'markup', 'work')


def pdf_rect(rect, container_height):
    x1 = min(rect[0], rect[2])
    y1 = max(rect[1], rect[3])
    x2 = max(rect[0], rect[2])
    y2 = min(rect[1], rect[3])
    # and convert from pdf to image coords
    return Rect(x1, container_height - y1, x2, container_height - y2)


def cv2_rect(l, t, w, h):
    return Rect(l, t, l + w, t + h)


def overlaps(r1, r2, threshold):
    A = r1.to_dict()
    B = r2.to_dict()

    # https://stackoverflow.com/questions/9324339/how-much-do-two-rectangles-overlap
    SA = A['w'] * A['h']
    SB = B['w'] * B['h']
    SI = max([0, 1 + min([A['x2'], B['x2']]) - max([A['x1'], B['x1']])]) * max([0, 1 + min([A['y2'], B['y2']]) - max([A['y1'], B['y1']])])
    SU = SA + SB - SI
    overlap = float(SI) / float(SU)

    #print('overlap: {}%'.format(int(overlap * 100)))
    return overlap > threshold


class Rect(object):

    def __init__(self, l, t, r, b):
        self.left = l
        self.top = t
        self.right = r
        self.bottom = b

    def translate(self, x, y):
        self.left += x
        self.top += y
        self.right += x
        self.bottom += y
        return self

    def scale(self, x, y):
        self.left *= x
        self.top *= y
        self.right *= x
        self.bottom *= y
        return self

    def p1(self, page_height):
        return (self.left, self.top)

    def p2(self, page_height):
        return (self.right, self.bottom)

    def to_dict(self):
        return {'x1': self.left,
                'y1': self.top,
                'x2': self.right,
                'y2': self.bottom,
                'w': self.right - self.left,
                'h': self.bottom - self.top }

    def __repr__(self):
        return 'Rect[l={}, t={}, r={}, b={}]'.format(int(self.left), int(self.top), int(self.right), int(self.bottom))


def ensure_dir(dir):
    if not os.path.exists(dir):
        os.makedirs(dir)
    os.chmod(dir, 0o775)
    shutil.chown(dir, group='procat')


def set_file_perms(file):
    os.chmod(file, 0o664)
    shutil.chown(file, group='procat')


def clean_path(path):
    """Replace filesystem-hostile characters"""
    path = re.sub(r'[<>]', '', path)
    path = re.sub(r'[^\w@]', '_', path)
    return path
