#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
reload(sys)
sys.setdefaultencoding('utf-8')

import cv2
import time

#import rospy
#from std_msgs.msg import String

from EmeraldAI.Entities.PredictionObject import PredictionObject
from EmeraldAI.Logic.ComputerVision.ComputerVision import ComputerVision
from EmeraldAI.Config.Config import *
from EmeraldAI.Logic.ComputerVision.ModelMonitor import ModelMonitor


def EnsureModelUpdate():
    monitor = ModelMonitor()
    predictionModules = Config().GetList("ComputerVision", "Modules")

    for moduleName in predictionModules:
        if(monitor.CompareHash(moduleName, monitor.GetStoredHash(moduleName))):
            print "Model '{0}' up to date".format(moduleName)
            continue
        print "Rebuild Model '{0}'...".format(moduleName)
        monitor.Rebuild(moduleName)


def RunCV():
    #pub = rospy.Publisher('to_brain', String, queue_size=10)
    #rospy.init_node('CV_node', anonymous=True)
    #rospy.Rate(10) # 10hz

    camera = cv2.VideoCapture(Config().GetInt("ComputerVision", "CameraID"))
    camera.set(3, Config().GetInt("ComputerVision", "CameraWidth"))
    camera.set(4, Config().GetInt("ComputerVision", "CameraHeight"))

    cv = ComputerVision()

    predictionObjectList = []

    predictionModules = Config().GetList("ComputerVision", "Modules")
    for moduleName in predictionModules:
        model, dictionary = cv.LoadModel(moduleName)
        if (model == None or dictionary == None):
            continue
        print "load", moduleName
        predictionObjectList.append(PredictionObject(moduleName, model, dictionary, 1500)) # last one distance

    clockFace = time.time()
    clockBody = time.time()

    while True:
        ret, image = camera.read()

        cv2.imshow("image", image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        rawBodyData = cv.DetectBody(image)
        if (len(rawBodyData) > 0):
            #rospy.loginfo("CV|BODY|{0}".format(len(rawBodyData)))
            #pub.publish("CV|BODY|{0}".format(len(rawBodyData)))

            # TODO - move to config  so we can detect all the time or this way
            predictionResult, thresholdReached, timeoutReached, rawFaceData = cv.PredictMultipleStream(image, predictionObjectList, threshold=7500)

            takeImage = True
            for predictorObject in predictionResult:
                if len(predictorObject.PredictionResult) > 0 and (thresholdReached or timeoutReached):

                    if (predictorObject.Name == "Person"):
                        for key, face in predictorObject.PredictionResult.iteritems():
                            bestResult = predictorObject.GetBestPredictionResult(key, False)
                            bestResultPerson = predictorObject.GetBestPredictionResult(key, True)

                            if(bestResult[0] != "Unknown"):
                                takeImage = False

                            print "Face Detection", bestResult, bestResultPerson, thresholdReached, timeoutReached
                            #rospy.loginfo("CV|PERSON|{0}|{1}|{2}|{3}|{4}".format(key, bestResult[0], bestResult[1], thresholdReached, timeoutReached))
                            #pub.publish("CV|PERSON|{0}|{1}|{2}|{3}|{4}".format(key, bestResult[0], bestResult[1], thresholdReached, timeoutReached))

                    if (predictorObject.Name == "Mood"):
                        print "Mood: ", predictorObject.PredictionResult
                        #rospy.loginfo("CV|Mood|{0}|{1}|{2}|{3}|{4}".format("TODO"))
                        #pub.publish("CV|Mood|{0}|{1}|{2}|{3}|{4}".format("TODO"))


            if(takeImage and clockFace <= (time.time()-1) and cv.TakeImage(image, "Person", rawFaceData, grayscale=True)):
                clockFace = time.time()

            if(clockBody <= (time.time()-1) and cv.TakeImage(image, "Body", rawBodyData)):
                clockBody = time.time()



if __name__ == "__main__":
    try:
        EnsureModelUpdate()
        RunCV()
    except KeyboardInterrupt:
        print "End"

@@ -949,16 +949,21 @@
 f RunCV(
+camID
 ):%0A    #
@@ -1103,34 +1103,40 @@
     
-camera = cv2.VideoCapture(
+if(camID  %3C 0):%0A        camID = 
 Conf
@@ -1176,16 +1176,53 @@
 meraID%22)
+%0A%0A    camera = cv2.VideoCapture(camID
 )%0A    ca
@@ -4117,16 +4117,197 @@
 ain__%22:%0A
+    camID = -1%0A    if len(sys.argv) %3E 1:%0A        for arg in sys.argv:%0A            if (arg.lower().startswith(%22-cam%22)):%0A                camID = int(arg.lower().replace(%22-cam%22, %22%22))%0A%0A
     try:
@@ -4349,16 +4349,21 @@
   RunCV(
+camID
 )%0A    ex

add command line parameter for cam selection