# Path: scripts/model/result.py
# class Result:
#     # tracker : trakcer name
#     # seqName : Sequence name
#     # startFrame : start frame number
#     # endFrame : end frame number
#     # res : results 
#     # resType : result type
#     # fps
# 
#     def __init__(self, tracker, seqName, startFrame=0, endFrame=0,
#         resType='rect', evalType='OPE', res=[], fps=0, shiftType=None, tmplsize=None):
#         self.tracker = tracker
#         self.seqName = seqName
#         self.startFrame = startFrame
#         self.endFrame = endFrame
#         self.resType = resType
#         self.evalType = evalType
#         self.res = res
#         self.fps = fps
#         self.shiftType = shiftType
#         self.tmplsize = tmplsize
#         
#         self.__dict__ = OrderedDict([
#             ('tracker', self.tracker),
#             ('seqName', self.seqName),
#             ('startFrame', self.startFrame),
#             ('endFrame', self.endFrame),
#             ('evalType', self.evalType),
#             ('fps', self.fps),
#             ('shiftType', self.shiftType),
#             ('resType', self.resType),
#             ('tmplsize', self.tmplsize),
#             ('res', self.res)])
# 
#     def refresh_dict(self):
#         self.__dict__ = OrderedDict([
#             ('tracker', self.tracker),
#             ('seqName', self.seqName),
#             ('startFrame', self.startFrame),
#             ('endFrame', self.endFrame),
#             ('evalType', self.evalType),
#             ('fps', self.fps),
#             ('shiftType', self.shiftType),
#             ('resType', self.resType),
#             ('tmplsize', self.tmplsize),
#             ('res', self.res)])


import os
import sys
import json
    import scipy.io as sio
from model import Sequence
from scripts.model.result import Result
    seq = Sequence(**j)
    seq.path = os.path.join(os.path.abspath(seq.path), '')
    return seq


def save_seq_result(RESULT_SRC, result):
    tracker = result.tracker
    seqName = result.seqName
    evalType = result.evalType
    src = RESULT_SRC.format(evalType) + tracker
    if not os.path.exists(src):
        os.makedirs(src)
    try:
        string = json.dumps(result, default=lambda o : o.__dict__)
    except:
        print map(type, result[0].__dict__.values())
        sys.exit()
    fileName = src + '/{0}.json'.format(seqName)
    resultFile = open(fileName, 'wb')
    resultFile.write(string)
    resultFile.close()


def load_seq_result(result_src):
    resultFile = open(result_src)
    string = resultFile.read()
    jsonList = json.loads(string)
    if type(jsonList) is list:
        return [Result(**j) for j in jsonList]
    elif type(jsonList) is dict:

# Path: scripts/model/result.py
# class Result:
#     # tracker : trakcer name
#     # seqName : Sequence name
#     # startFrame : start frame number
#     # endFrame : end frame number
#     # res : results 
#     # resType : result type
#     # fps
# 
#     def __init__(self, tracker, seqName, startFrame=0, endFrame=0,
#         resType='rect', evalType='OPE', res=[], fps=0, shiftType=None, tmplsize=None):
#         self.tracker = tracker
#         self.seqName = seqName
#         self.startFrame = startFrame
#         self.endFrame = endFrame
#         self.resType = resType
#         self.evalType = evalType
#         self.res = res
#         self.fps = fps
#         self.shiftType = shiftType
#         self.tmplsize = tmplsize
#         
#         self.__dict__ = OrderedDict([
#             ('tracker', self.tracker),
#             ('seqName', self.seqName),
#             ('startFrame', self.startFrame),
#             ('endFrame', self.endFrame),
#             ('evalType', self.evalType),
#             ('fps', self.fps),
#             ('shiftType', self.shiftType),
#             ('resType', self.resType),
#             ('tmplsize', self.tmplsize),
#             ('res', self.res)])
# 
#     def refresh_dict(self):
#         self.__dict__ = OrderedDict([
#             ('tracker', self.tracker),
#             ('seqName', self.seqName),
#             ('startFrame', self.startFrame),
#             ('endFrame', self.endFrame),
#             ('evalType', self.evalType),
#             ('fps', self.fps),
#             ('shiftType', self.shiftType),
#             ('resType', self.resType),
#             ('tmplsize', self.tmplsize),
#             ('res', self.res)])


# Path: scripts/UAV_script.py
import os
import sys
import json
    import scipy.io as sio
from model import Sequence
from scripts.model.result import Result

    seq = Sequence(**j)
    seq.path = os.path.join(os.path.abspath(seq.path), '')
    return seq


def save_seq_result(RESULT_SRC, result):
    tracker = result.tracker
    seqName = result.seqName
    evalType = result.evalType
    src = RESULT_SRC.format(evalType) + tracker
    if not os.path.exists(src):
        os.makedirs(src)
    try:
        string = json.dumps(result, default=lambda o : o.__dict__)
    except:
        print map(type, result[0].__dict__.values())
        sys.exit()
    fileName = src + '/{0}.json'.format(seqName)
    resultFile = open(fileName, 'wb')
    resultFile.write(string)
    resultFile.close()


def load_seq_result(result_src):
    resultFile = open(result_src)
    string = resultFile.read()
    jsonList = json.loads(string)
    if type(jsonList) is list:
        return [Result(**j) for j in jsonList]
    elif type(jsonList) is dict:

        return [Result(**jsonList)]