# -*- coding: utf-8 -*-
"""
Created on Mon Mar  4 18:36:31 2024

@author: ym
"""
import numpy as np
from ytracking.tracking.dotrack.dotracks import doTracks, ShoppingCart
from ytracking.tracking.dotrack.track_back import backTrack


class doBackTracks(doTracks):
    def __init__(self, bboxes, features_dict):
        
        super().__init__(bboxes, features_dict)
        
        self.tracks = [backTrack(b) for b in self.lboxes]
        
        # self.similar_dict = self.similarity()
        
        
        self.shopcart = ShoppingCart(bboxes)

# =============================================================================
#     def array2list(self):
#         '''           0, 1,   2,  3,     4,      5,    6,      7,          8
#             bboxes: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]
#             lboxes：[x1, y1, x2, y2, track_id, score, cls, frame_index, box_index] 
#         '''
# 
#         track_ids = set(self.bboxes[:, 4])
#         lboxes = []
#         for t_id in track_ids:
#             idx = np.where(self.bboxes[:, 4] == t_id)[0]
#             box = self.bboxes[idx, :]
#             
#             x = (box[:, 0] + box[:, 2]) / 2
#             y = (box[:, 1] + box[:, 3]) / 2
#             
#             # box: [x, y, w, h, track_id, score, cls, frame_index]
#             box[:, 2] = box[:, 2] - box[:, 0]    
#             box[:, 3] = box[:, 3] - box[:, 1]
#             box[:, 0] = x
#             box[:, 1] = y
#       
#             lboxes.append(box)
# 
#         
#         return lboxes
# =============================================================================

            
    
    def classify(self):
        '''
        功能：对 tracks 中元素分类
        
        '''
        
        tracks = self.tracks
        shopcart = self.shopcart

        # 提取手的frame_id，并和动目标的frame_id 进行关联
        hand_tracks = [t for t in tracks if t.cls==0]
        self.Hands.extend(hand_tracks)
        tracks = self.sub_tracks(tracks, hand_tracks)
        
        
        
        # 提取小孩的track，并计算状态：left, right, incart
        kid_tracks = [t for t in tracks if t.cls==9]
        kid_states = [self.kid_state(t) for t in kid_tracks]
        self.Kids = [x for x in zip(kid_tracks, kid_states)]
        
        tracks = self.sub_tracks(tracks, kid_tracks)
        


        '''静态情况 1: 目标关键点最小相对运动轨迹 < 0.2, 指标值偏大
           feature = [trajlen_min, trajlen_max, 
                      trajdist_min,  trajdist_max,
                      trajlen_rate, trajdist_rate]
        '''        
        track1 = [t for t in tracks if t.feature[5] < 0.2
                                    or t.feature[3] < 120
                                    ] 
        
        
        '''静态情况 2: 目标初始状态为静止，适当放宽关键点最小相对运动轨迹 < 0.5'''
        track2 = [t for t in tracks if t.static_index.size > 0
                                   and t.static_index[0, 0] <= 2                      
                                   and t.feature[5] < 0.5]
        
        '''静态情况 3: 目标初始状态和最终状态均为静止'''
        track3 = [t for t in tracks if t.static_index.shape[0] >= 2
                                   and t.static_index[0, 0] <= 2
                                   and t.static_index[-1, 1] >= t.frnum-3]
        
        track12 = self.join_tracks(track1, track2) 
        
        '''提取静止状态的 track'''
        static_tracks = self.join_tracks(track12, track3)
        self.Static.extend(static_tracks)
        
        
        '''剔除静止目标后的 tracks'''
        tracks = self.sub_tracks(tracks, static_tracks)
        
        
        '''购物框边界外具有运动状态的干扰目标'''
        trcak4 = [t for t in tracks if self.isouttrack(t)]

        tracks = self.sub_tracks(tracks, trcak4)

        
        '''轨迹循环归并'''
        # merged_tracks = self.merge_tracks(tracks)
        merged_tracks = self.merge_tracks_loop(tracks)

        self.Residual = merged_tracks


        
    def merge_tracks(self, Residual):
        """
        对不同id，但可能是同一商品的目标进行归并
        """ 
        mergedTracks = self.base_merge_tracks(Residual)
        
        oldtracks, newtracks = [], []
        for tracklist in mergedTracks:
            if len(tracklist) > 1:
                boxes = np.empty((0, 9), dtype=np.float32)
                for i, track in enumerate(tracklist):
                    if i==0: ntid, ncls=track.boxes[0, 4], track.boxes[0, 6]
                    iboxes = track.boxes.copy()
                    
                    iboxes[:, 4], iboxes[:, 6] = ntid, ncls
                    boxes = np.concatenate((boxes, iboxes), axis=0)
                    
                    oldtracks.append(track)
                    
                fid_indices = np.argsort(boxes[:, 7])
                boxes_fid = boxes[fid_indices]    
                    
                newtracks.append(backTrack(boxes_fid))
            elif len(tracklist) == 1:
                oldtracks.append(tracklist[0])
                newtracks.append(tracklist[0])
                

        redu = self.sub_tracks(Residual, oldtracks)
        merged = self.join_tracks(redu, newtracks)

        return merged      
        
    def kid_state(self, track):
        
        left_dist = track.cornpoints[:, 2]
        right_dist = 1024 - track.cornpoints[:, 4]
        
        if np.sum(left_dist<30)/track.frnum>0.8 and np.sum(right_dist>512)/track.frnum>0.7:
            kidstate = "left"
        elif np.sum(left_dist>512)/track.frnum>0.7 and np.sum(right_dist<30)/track.frnum>0.8:
            kidstate = "right"
        else:
            kidstate = "incart"
        
        return kidstate
    
    

    def hand_association(self):
        """
        分析商品和手之间的关联性
        """
        pass 
    
    def isouttrack(self, track):     
        if track.posState <= 1:
            isout = True
        else:
            isout = False  
        return isout
    
    

      
       

    def isuptrack(self, track):
        Flag = False
        
        return Flag
    
    def isdowntrack(self, track):
        Flag = False
        
        return Flag
    
    def isfreetrack(self, track):
        Flag = False
        
        return Flag