#!/usr/bin/python

import sys
import os
from head_pose_output_parser import HeadPoseOutputParser as output_parser


class ArffGenerator(object):

    _CLASS_PAYING_ATTENTION = "paying_attention"
    _CLASS_NOT_PAYING_ATTENTION = "not_paying_attention"

    _ARFF_FILE_RELATION = "@RELATION attention"
    _ARFF_FILE_RX = "@ATTRIBUTE rx	REAL"
    _ARFF_FILE_RY = "@ATTRIBUTE ry	REAL"
    _ARFF_FILE_RZ = "@ATTRIBUTE rz	REAL"
    _ARFF_FILE_CLASS = "@ATTRIBUTE class 	{paying_attention,not_paying_attention}"
    _ARFF_FILE_DATA = "@DATA"
    _ARFF_FILE_NAME = "/original.arff"

    def __init__(self, path_to_main_image_folder):
        self._path_to_main_dir = path_to_main_image_folder

    def generate_arff_from_openface_output(self):
        """main caller function"""
        self._construct_arff_file(self._path_to_main_dir)

    def _construct_arff_file(self, output_path):
        """initialises the .arff file with metadata"""
        arff_file_location = output_path + self._ARFF_FILE_NAME
        with open(arff_file_location, 'w+') as arff_file:
            arff_file.write(arff_metadata)
        return arff_file_location

    def _generate_arff_metadata(self):
        """generates the metadata for the original.arff file"""
        arff_metadata = "%s\n\n%s\n%s\n%s\n%s\n\n%s\n" \
                    % (_ARFF_FILE_RELATION, _ARFF_FILE_RX, _ARFF_FILE_RY, _ARFF_FILE_RZ, _ARFF_FILE_CLASS, _ARFF_FILE_DATA)
        return arff_metadata

    def _combine_all_output(self):
        """generator, yields paths? nice!"""
        output_parser = HeadPoseOutputParser()
        for attention_yes_no_dir in os.listdir(self._path_to_main_dir):
            current_dir = self._path_to_main_dir + "/" + attention_yes_no_dir
            for image_dir in attention_yes_no_dir:
                current_image_dir = current_dir + "/" + image_dir + "/" + "OUTPUTFILEHIERZO"
                pass

    def _extract_head_pose_from_output_file(self, path_to_file):
        """extract the estimated head pose for an image. 
        IF SUCCESSFULL -> extract yes/no (dirpath), rx, ry, rz"""
        # use code found in attentionscorecalculator!

if __name__ == "__main__":
    path_to_main_image_folder = sys.argv[1]
    arff_generator = ArffGenerator(path_to_main_image_folder)
    pass
