"""
Project management classes and functions
"""

import os
from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
import time
import datetime
from shutil import copy
try:
    from PIL import Image
except:
    import Image
import cvutils
import numpy as np

from app_config import get_project_path, get_project_config_path, config_section_exists, update_config_without_sections
from video import get_framerate

def create_project(identifier, video_filename):
    config_dict = {}
    _update_config_dict_with_defaults(config_dict)

    _create_project_dir(identifier, config_dict)

def update_homography(identifier, homography_path, unitpixelratio):
    pass

def update_project_config(identifier, config_dict):
    _update_config_dict_with_defaults(config_dict)

    project_path = get_project_path(identifier)
    tracking_path = os.path.join(project_path, "tracking.cfg")
    update_config_without_sections(tracking_path, config_dict)

def default_config_dict():
    return {
        'max_features_per_frame': 1000,
        'num_displacement_frames': 10,
        'min_feature_displacement': 0.0001,
        'max_iterations_to_persist': 200,
        'min_feature_frames': 15,
        'max_connection_distance': 1.0,
        'max_segmentation_distance': 0.7,
    }

def _update_config_dict_with_defaults(config_dict):
    default_dict = default_config_dict()

    # Remove things that we don't want the user to be able to configure
    for key in config_dict.keys():
        if key not in default_dict.keys():
            del config_dict[key]

    # Add defaults for things that don't exist
    for (key, value) in default_dict.iteritems():
        if key not in config_dict.keys():
            config_dict[key] = value

def _create_project_dir(identifier, config_dict, video_filename):
    test_object_dir = os.path.join(".temp", "test", "test_object")
    test_feature_dir = os.path.join(".temp", "test", "test_feature")

    directory_names = ["homography", test_object_dir, test_feature_dir, "run", "results"]
    project_path = get_project_path(identifier)

    if not os.path.exists(project_path):
        # Create proj dirs
        for new_dir in directory_names:
            os.makedirs(os.path.join(project_path, new_dir))

        # TODO: unitpixelratio
        _write_to_project_config(identifier)

        default_files_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "default")

        tracking_path = os.path.join(project_path, "tracking.cfg")
        classifier_path = os.path.join(project_path, "classifier.cfg")

        copy(os.path.join(default_files_dir, "tracking.cfg"), tracking_path)
        copy(os.path.join(default_files_dir, "classifier.cfg"), classifier_path)

        # Add classifier path along with configuration
        config_dict['classifier-filename'] = classifier_path
        update_config_without_sections(tracking_path, config_dict)

        update_dict = {
            'pbv-svm-filename': os.path.join(default_files_dir, 'modelPBV.xml'),
            'bv-svm-filename': os.path.join(default_files_dir, 'modelBV.xml')
        }
        update_config_without_sections(classifier_path, update_dict)

    else:
        print("Project exists. No new project created.")

def _write_to_project_config(self, identifier, video_filename, unitpixelratio=None):
    ts = time.time()
    vid_ts = datetime.datetime.now()
    #This line needs to be updated to no longer need the ui class. Load video and pull time.
    timestamp = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y %H:%M:%S %Z')
    video_timestamp = vid_ts.strftime('%d-%m-%Y %H:%M:%S %Z')
    config_parser = SafeConfigParser()
    config_parser.add_section("info")
    config_parser.set("info", "project_name", identifier)
    config_parser.set("info", "creation_date", timestamp)
    config_parser.add_section("video")
    config_parser.set("video", "name", os.path.basename(video_filename))
    config_parser.set("video", "source", video_filename)
    config_parser.set("video", "framerate", get_framerate(video_filename))
    config_parser.set("video", "start", video_timestamp)
    if unitpixelratio:
        config_parser.add_section("homography")
        config_parser.set("homography", "unitpixelratio", unitpixelratio)

    with open(get_project_config_path(identifier), 'wb') as configfile:
        config_parser.write(configfile)

    os.remove(os.path.join(get_project_path(identifier), "project_name.cfg"))

@@ -2315,24 +2315,40 @@
 g(identifier
+, video_filename
 )%0A%0A        d
@@ -3282,14 +3282,8 @@
 fig(
-self, 
 iden
@@ -3901,25 +3901,8 @@
 e%22, 
-os.path.basename(
 vide
@@ -3904,33 +3904,32 @@
  video_filename)
-)
 %0A    config_pars
@@ -4028,16 +4028,59 @@
 amerate(
+os.path.join(get_project_path(identifier), 
 video_fi
@@ -4087,16 +4087,17 @@
 lename))
+)
 %0A    con
@@ -4408,83 +4408,4 @@
 le)%0A
-%0A    os.remove(os.path.join(get_project_path(identifier), %22project_name.cfg%22))%0A

Fix pm.py