import random
import math

class Object:
    name = ""
    left = 0;
    right = 0;
    top = 0;
    buttom = 0;
    classification = ""
    owner = ""
    lastOwner = ""
    X = None
    Y = None
    img = None
    imgResized = None
    timestamp = None

    def __contains__(self, substring):
        if substring in self.name:
            return True
        else:
            return False

    def __init__(self,  left=0, right=0, top=0, bottom=0, classification="", name="", img = None):
        if name == "":
            self.name = ''.join(random.choice('ABCDEFGHIJKLMNOPRSTQUVYZ') for _ in range(5))
        else:
            self.name = name
        self.left = left
        self.right = right
        self.top = top
        self.bottom = bottom
        self.classification = classification
        self.X = []
        self.Y = []
        self.owner = ""


    def computeCenterPosition(self):
        x = (self.left + self.right) / 2
        y = (self.top + self.bottom) / 2

        self.X.append(x)
        self.Y.append(y)

    def computeCenterPositionOnce(self):
        x = (self.left + self.right) / 2
        y = (self.top + self.bottom) / 2

        return (x, y)

    def getDeltaArrays(self):
        return self.deltaX, self.deltaY

    def getDeltas(self):
        deltaX = []
        beforeX = 0
        for x in self.X:
            if(len(deltaX) == 0):
                deltaX.append(0)
            else:
                deltaX.append(x - beforeX)
            beforeX = x

        deltaY = []
        beforeY = 0
        for y in self.Y:
            if (len(deltaY) == 0):
                deltaY.append(0)
            else:
                deltaY.append(y - beforeY)
            beforeY = y

        return deltaX, deltaY

    def getXY(self):
        return list(self.X), list(self.Y)




def createObjects(out_classes, class_names, out_boxes):

    objects = []

    for i, c in reversed(list(enumerate(out_classes))):

        predicted_class = class_names[c]
        if predicted_class == "handbag" or predicted_class == "suitcase" or predicted_class == 'backpack' or predicted_class == "person":
            box = out_boxes[i]
            top, left, bottom, right = box
            o = Object(left= left, right=right, top=top, bottom=bottom, classification=predicted_class)
            objects.append(o)
    return objects

def printAllObjects(objects):

    for object in objects:
        print(object.classification)

def computeDistanceBetweenObjects(Object, Object2):
    x1, y1 = Object.computeCenterPositionOnce()
    x2, y2 = Object2.computeCenterPositionOnce()
    x = abs(x1 - x2)
    y = abs(y1 - y2)

    distance = math.sqrt(x**2 + y ** 2)
    return distance

def computeDistance(x1, y1, x2, y2):
    x = abs(x1 - x2)
    y = abs(y1 - y2)

    distance = math.sqrt(x**2 + y ** 2)
    return distance

