# -*- coding: utf-8 -*-
"""
Created on Tue Dec 10 14:30:16 2024

@author: ym
"""
import os
import sys
import numpy as np
sys.path.append(r"D:\DetectTracking")
from tracking.utils.read_data import read_tracking_output, read_similar #, extract_data, read_deletedBarcode_file


IMG_FORMAT = ['.bmp', '.jpg', '.jpeg', '.png']


def creat_shopping_event(eventPath):
    '''构造放入商品事件字典，这些事件需满足条件：
        1) 前后摄至少有一条轨迹输出
        2) 保存有帧图像，以便裁剪出 boxe 子图
    '''

    '''evtName 为一次购物事件'''    
    evtName = os.path.basename(eventPath)
    evtList = evtName.split('_')

    '''================ 0. 检查 evtName 及 eventPath 正确性和有效性 ================'''
    if evtName.find('2024')<0 and len(evtList[0])!=15:
        return
    if not os.path.isdir(eventPath): 
        return
    
    if len(evtList)==1 or (len(evtList)==2 and len(evtList[1])==0):
        barcode = ''
    else:
        barcode = evtList[-1]
    
    if len(evtList)==3 and evtList[-1]== evtList[-2]:
        evtType = 'input'
    else:
        evtType = 'other'

    '''================ 1. 构造事件描述字典，暂定 9 items ==============='''
       

    
    
    event = {}
    event['barcode'] = barcode
    event['type'] = evtType
    event['filepath'] = eventPath
    event['back_imgpaths'] = []
    event['front_imgpaths'] = []
    event['back_boxes'] = np.empty((0, 9), dtype=np.float64)
    event['front_boxes'] = np.empty((0, 9), dtype=np.float64)
    event['back_feats'] = np.empty((0, 256), dtype=np.float64)
    event['front_feats'] = np.empty((0, 256), dtype=np.float64)
    event['feats_compose'] = np.empty((0, 256), dtype=np.float64)
    event['one2one'] = None
    event['one2n'] = None
    event['feats_select'] = np.empty((0, 256), dtype=np.float64)
    
    
    '''================= 2. 读取 data 文件 ============================='''
    for dataname in os.listdir(eventPath):
        # filename = '1_track.data'
        datapath = os.path.join(eventPath, dataname)
        if not os.path.isfile(datapath): continue
        
        CamerType = dataname.split('_')[0]
        ''' 2.1 读取 0/1_track.data 中数据，暂不考虑'''
        # if dataname.find("_track.data")>0:
        #     bboxes, ffeats, trackerboxes, tracker_feat_dict, trackingboxes, tracking_feat_dict = extract_data(datapath)
  
        ''' 2.2 读取 0/1_tracking_output.data 中数据'''
        if dataname.find("_tracking_output.data")>0:
            tracking_output_boxes, tracking_output_feats = read_tracking_output(datapath)
            if len(tracking_output_boxes) != len(tracking_output_feats): continue
            if CamerType == '0':
                event['back_boxes'] = tracking_output_boxes
                event['back_feats'] = tracking_output_feats
            elif CamerType == '1':
                event['front_boxes'] = tracking_output_boxes
                event['front_feats'] = tracking_output_feats
                
        if dataname.find("process.data")==0:
            simiDict = read_similar(datapath)
            event['one2one'] = simiDict['one2one']
            event['one2n'] = simiDict['one2n']


    if len(event['back_boxes'])==0 or len(event['front_boxes'])==0:
        return None
    
    '''2.3 事件的特征表征方式: 特征选择、特征集成'''
    bk_feats = event['back_feats']
    ft_feats = event['front_feats']
    
    '''2.3.1 特征集成'''
    feats_compose = np.empty((0, 256), dtype=np.float64)
    if len(ft_feats):
        feats_compose = np.concatenate((feats_compose, ft_feats), axis=0)
    if len(bk_feats):
        feats_compose = np.concatenate((feats_compose, bk_feats), axis=0)
    event['feats_compose'] = feats_compose
            
    '''2.3.1 特征选择'''
    if len(ft_feats):
        event['feats_select'] = ft_feats


    '''================ 3. 读取图像文件地址，并按照帧ID排序 ============='''
    frontImgs, frontFid = [], []
    backImgs, backFid = [], []
    for imgname in os.listdir(eventPath):
        name, ext = os.path.splitext(imgname)
        if ext not in IMG_FORMAT or name.find('frameId')<0: continue

        CamerType = name.split('_')[0]
        frameId = int(name.split('_')[3])
        imgpath = os.path.join(eventPath, imgname)
        if CamerType == '0':
            backImgs.append(imgpath)
            backFid.append(frameId)
        if CamerType == '1':
            frontImgs.append(imgpath)
            frontFid.append(frameId)
    
    frontIdx = np.argsort(np.array(frontFid))
    backIdx = np.argsort(np.array(backFid))
    
    '''3.1 生成依据帧 ID 排序的前后摄图像地址列表'''
    frontImgs = [frontImgs[i] for i in frontIdx]
    backImgs = [backImgs[i] for i in backIdx]

    '''3.2 将前、后摄图像路径添加至事件字典'''   
    
    
    bfid = event['back_boxes'][:, 7].astype(np.int64)
    ffid = event['front_boxes'][:, 7].astype(np.int64)
    if len(bfid) and max(bfid) <= len(backImgs):
        event['back_imgpaths'] = [backImgs[i-1] for i in bfid]
    if len(ffid) and max(ffid) <= len(frontImgs):
        event['front_imgpaths'] = [frontImgs[i-1] for i in ffid]

    
    '''================ 4. 判断当前事件有效性，并添加至事件列表 =========='''
    condt1 = len(event['back_imgpaths'])==0 or len(event['front_imgpaths'])==0
    condt2 = len(event['front_feats'])==0 and len(event['back_feats'])==0
    
    if condt1 or condt2:
        print(f"Event: {evtName}, Error, condt1: {condt1}, condt2: {condt2}")
        return None

    return event