# -*- coding: utf-8 -*-
"""
Created on Thu Aug 10 19:29:00 2017

@author: Steven
"""
import numpy as np
import cv2
import os
from imutils.object_detection import non_max_suppression

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

def get_start():
    root = os.path.dirname(os.path.abspath(__file__))
    data_path = os.path.join(root, 'data')
    starting_value = 1
    file_name = 'training_data-{}.npy'.format(starting_value)
    print('Beginning with {}'.format(os.path.join(data_path, file_name)))
    return data_path, file_name, starting_value

def exists(path):
    if os.path.isfile(path):
        print('{} exists, moving along'.format(path))
        return True
    else:
        print('{} does not exist, starting here.'.format(path))
        return False

def instructions():
    usecase = "Cycling through negatives. \'q\' to exit, \'s\' to save frame"
    + " to negative data file, and \'r\' to reverse. When using reverese, if a"
    + " saved frame is reversed over it will be removed from the save list and "
    + "must be added again (with \'s\')."
    return usecase

def get_saves(data_path):
    pos_starting_value = 1
    neg_starting_value = 1
    pos_name = os.path.join('bal', 'positive_firing_data-{}.npy'.format(pos_starting_value))
    neg_name = os.path.join('bal', 'negative_firing_data-{}.npy'.format(neg_starting_value))
     
    while(True):
        if exists(os.path.join(data_path, pos_name)):
            pos_starting_value += 1
        else:
            break
     
    while(True):
        if exists(os.path.join(data_path, neg_name)):
            pos_starting_value += 1
        else:
            break
        
    return pos_name, neg_name, pos_starting_value, neg_starting_value

#TODO: send to misc.py
def check_click(click, i):
    if 1 in click:
        print("Frame {}: Left Click down".format(i))
    if 2 in  click:
        print("Frame {}: Right Click down".format(i))
        
#TODO: send to misc.py
def display(frame):
    cv2.imshow("preview", frame)
    key = cv2.waitKey(0)
    return key
        
def check_pos_data(data):
    i = 0
    conf_pos = []
    saved = []
    print(instructions())
    
    
    while i < len(data):
        print("Displaying pos. {}".format(i + 1))
        check_click(data[i][3], i)
        key = display(data[i][0])       
            
        if key == ord("q"):