import re

class parser:

    def __init__(self):
        print("Address of self = ",id(self))

    def check_image_count(self,image_list, text):
        pair_obj = findTag(text)
        if pair_obj['code'] == 1:
            if len(image_list)!=len(pair_obj['pair']):
                return {'msg':'圖片與標籤數量不一致!','code':-1}
            else:
                return {code:1}
        else:
            return pair_obj
            

    def findTag(self,text):
        left_tag = [m.start() for m in re.finditer('{', text)]
        right_tag = [m.start() for m in re.finditer('}', text)]
        if len(left_tag)!=len(right_tag):
            return {'msg':'圖片標籤錯誤，左右數量不符','code':-1}
        pair = []
        for idx in range(len(left_tag)):
            pair.append({'left':left_tag[idx],'right':right_tag[idx]})
        obj = {'code':1,'pair':pair}
        return obj

    def replace_list(self,text):
        rep_ls = []
        pair_obj = self.findTag(text)
        for p in pair_obj['pair']:
            rep_ls.append(text[p['left']:p['right']+1])
        return rep_ls
    
    def image_clip_info(self,dict_in):
        #if 'image_idx' in dic:
    #            new_dic['image_obj'] = {'start':dic['start'],'idx':dic['image_idx']}
        stopPoint = 0 # sec
        time_info = []
        img_idx = 1 #start from 1
        added_idx = []
        for dic in dict_in:
            if 'image_obj' in dic :
                if dic['image_obj']['idx'] not in added_idx:
                    added_idx.append(dic['image_obj']['idx'])
                    time_info.append({'index':img_idx,'start':dic['start']})
                    img_idx += 1
            if dic['start']+dic['duration'] > stopPoint:
                stopPoint = dic['start']+dic['duration']
        
        for idx in range(len(time_info)-1):
            time_info[idx]['duration'] = time_info[idx+1]['start']-time_info[idx]['start']
        time_info[-1]['duration'] = stopPoint - time_info[-1]['start']
        #index start duration
        return time_info               
