import time
start = time.time()
import logging
import os, glob, sys
from importlib import reload
reload(logging)
import coloredlogs
import concurrent.futures
from functions import *
#from handling_json_file import *
import json
import cv2
from datetime import datetime, timedelta
#from moviepy.editor import VideoFileClip
import telegram

table_name="2m"

my_token = '5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4'
logging.getLogger('sqlalchemy').setLevel(logging.ERROR)
logger = logging.getLogger(__name__)
fh = logging.FileHandler('log_file6.log')
fh.setLevel(logging.INFO)
formatter = coloredlogs.ColoredFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
coloredlogs.install(level='INFO')

with open("all_files.json", "r+") as jsonFile6: #insert all changed ts videos
    all_names = json.load(jsonFile6)

with open("state.json", "r+") as jsonFile2:  #start or end for the jingle QUE OUT or QUE IN
        data_stat = json.load(jsonFile2)

with open("level2.json", "r+") as jsonFile5: #date of the level2
        level2 = json.load(jsonFile5)

with open("last_detected_jingle.json", "r+") as jsonFile8: #the last detected jingle
        last_detected_jingle = json.load(jsonFile8)

############# ts_listener.py

segment = str(sys.argv[1])

logger.info('========================================================================================================')
logger.info('post will take %s',segment)
os.system('python3 post.py '+segment)
logger.info('post is done')
#end = time.time()

os.system('python3 main2.py '+segment+' </dev/null > /dev/null 2>&1 & echo '+segment+' >> history_of_ts.txt')



# os.system('python3 main2.py '+segment+' </dev/null > /dev/null 2>&1 ' )

#print(segment)
#if os.path.isfile(table_name+"/"+table_name+"_hls6/"+segment) and ".ts" in segment:
# if os.path.isfile(table_name+"/2m_hls/"+segment) and ".ts" in segment and segment !=all_names['names'] and '@' not in segment and 'jingle' not in segment:

#     #if level2['end']<current_time and data_stat['situation']

#     print("segment => "+segment)
#     #clip = VideoFileClip("2m/2m_hls/"+segment)
#     duration = with_ffprobe("2m/2m_hls/"+segment)
#     # duration = 6
#     ts = time.time()
#     ts = "."+str(ts).split('.')[0]
#     now = str(segment).split('.')[0]

#     home = os.path.expanduser("~")
#     ffmpeg = 'ffmpeg'
#     cwd = os.getcwd()

#     # video = 25 frame par second

#     cmd = cmd = "ffmpeg -y -i 2m/2m_hls/"+segment+" -vf 'select=gt(scene\,0.20)' -vsync vfr -frame_pts true 2m/iframe_live/"+now+"_%d_"+str(duration)+".png"
#     #cmd = "ffmpeg -y -i 2m_hls/"+segment+" -vf 'select=gt(scene\,0.10)' -vsync vfr -frame_pts true iframes_live/"+now+str(ts)+"_%d_"+str(duration)+".png"
#     # cmd = "ffmpeg -y -i 2m_hls/"+segment+" -vf \"select='eq(pict_type,PICT_TYPE_I)'\" -vsync vfr -frame_pts true iframes_live/"+now+str(ts)+"_%d_"+str(duration)+".png"
#     print(cmd)
#     # python3 iframe_scanner.py $file
#     os.system(cmd)


# images=[iframe for iframe in glob.glob("2m/iframe_live/*.png") if segment.split('.')[0] in iframe]
# iframes=[image.split('/')[-1] for image in images]
# logger.info('the number of the iframes is %s', len(iframes))
# #logger.info('ts_listener is done!!!')
# #####################################################" preprocess.py"


# #
# with open("state.json", "r+") as jsonFile2:  #start or end for the jingle QUE OUT or QUE IN
#         data_stat = json.load(jsonFile2)

# with open("detected_jingle.json", "r+") as jsonFile: #get the ts video of the detected video dynamically
#         data_name = json.load(jsonFile)


# with open("ref.json", "r+") as jsonFile1:  #one we detect the fist jingle we stop for sometime
#         data_ref = json.load(jsonFile1)

# with open("state_ts_video.json", "r+") as jsonFile7: #insert all changed ts videos
#         state_video = json.load(jsonFile7)

# with open('time.json') as df: #times in jingle
#     data_time = json.load(df)


# jingles=[image for num in [i for i in range(1,7)] for image in glob.glob("jingles_iframes/jingle"+str(num)+"/*.png")]


# for filename in iframes:
#     #filename = str(sys.argv[1])
#     ts_name=filename.split('_')[0]

#     #clip = VideoFileClip("2m/2m_hls/"+ts_name+".ts")
#     duration = with_ffprobe("2m/2m_hls/"+ts_name+".ts")
#     fileready = ""
#     logger.info('we handling the %s file',ts_name)
#     end='no'

#     with open("all_files.json", "r+") as jsonFile6: #insert all changed ts videos
#         all_names = json.load(jsonFile6)
#     if ts_name+'.ts' in all_names['names']:
#         logger.info('handling the jingle which is %s',ts_name+'.ts')
#         pass
#     else:
#     #def main(jingle):
#         for jingle in jingles:

#             time_to_cut=0.0
#             time_in_ts=0.0
#             time_in_jingle=''
#             ts_length=duration

#             result=compare_image(jingle,"2m/iframe_live/"+filename)
#             # logger.info('measure similarity between %s and %s with result %s',filename,jingle,result)
#             if result>=0.1:
#                 flag='0'
#                 # logger.info('No similarity ')

#             #else:
#             if result<0.1:

#                 send("from server 2 there is similarity between "+jingle+" and "+filename +" with results = "+str(result),"-1001778433367", token=my_token)
#                 #add_time()  #add 20 s
#                 logger.info('the %s and %s are similar',filename,jingle)
#                 now = datetime.now()
#                 if datetime.strptime(last_detected_jingle['date'], '%Y-%m-%d %H:%M:%S.%f')>now:
#                     logger.info('we already detected the first jingle !!!!')
#                 else:
#                     logger.info('the first time we detect this jingle !!')
#                     last_detected_jingle['date']=now+timedelta(seconds=15)
#                     jsonFile8 = open("last_detected_jingle.json", "w+")
#                     jsonFile8.write(json.dumps(last_detected_jingle,default=str))
#                     jsonFile8.close()
#                     time_in_ts=int(filename.split('_')[1])/25
#                     parent=jingle.split('/')[1]

#                     data_name['name']=parent
#                     jsonFile = open("detected_jingle.json", "w+")
#                     jsonFile.write(json.dumps(data_name))
#                     jsonFile.close()


#                     dict_time=data_time[parent]
#                     time_in_jingle=float(dict_time[jingle.split('/')[2].split('.')[0]])
#                     diff=time_in_ts-time_in_jingle

#                     if diff>=0:  ################## cut in the same ts
#                         logger.info('they are in the same Ts. the ts_time is %s and ts_jingle is %s',time_in_ts,time_in_jingle)
#                         time_to_cut=diff

#                         flag='1'

#                     else:################## cut in the previous ts

#                         logger.info('they are not in the same Ts. the ts_time is %s and ts_jingle is %s',time_in_ts,time_in_jingle)
#                         if ts_length -(time_in_jingle-time_in_ts)>=0:
#                             flag='2'
#                             #prev_ts_name=get_right_ts(filename,flag=0)
#                             time_to_cut=ts_length -(time_in_jingle-time_in_ts)  #7 example of duration of ts video.



#                         else:

#                             flag='3'
#                             #prev_ts_name=get_right_ts(filename,flag=1)
#                             time_to_cut=2*ts_length -(time_in_jingle-time_in_ts)
#                             #logger.info('the previous ts is %s',prev_ts_name)

#                     logger.info('The value of the flag is %s ',flag)

#                # if flag!='0': #we should apply the cut based on the value of the flag
#                     prev_ts_name=get_right_ts(filename,flag)
#                     logger.info('the right ts to cut is %s',prev_ts_name)
#                     logger.info('the time to cut is %s',time_to_cut)

#                     with open("state_ts_video.json", "r+") as jsonFile7: #insert all changed ts videos
#                         state_video = json.load(jsonFile7)
#                         for key, value in state_video.items():
#                             if key==prev_ts_name:
#                                 logger.info('we found ts in json file ##########################')
#                                 fileready = "ok"

#                     if data_stat['situation']=='end' and fileready == "":
#                         #logger.info('we are inside data_stat == end ##########################')


#                         os.system("cp 2m/2m_hls/"+prev_ts_name+" temp/"+prev_ts_name+"")
#                         os.system("ffmpeg -i temp/"+prev_ts_name+" -ss 00 -t "+str(time_to_cut)+" -c copy 2m/2m_hls/@"+prev_ts_name+"")
#                         logger.info('the cutting is done!')

#                         state_video.update({prev_ts_name:"end"})
#                         jsonFile7 = open("state_ts_video.json", "w+")
#                         jsonFile7.write(json.dumps(state_video))
#                         jsonFile7.close()

#                         current_time=datetime.now()
#                         level2['start']=current_time
#                         level2['end']=current_time+timedelta(minutes=10)
#                         jsonFile5 = open("level2.json", "w+")
#                         jsonFile5.write(json.dumps(level2,default=str))
#                         jsonFile5.close()



#                         end='yes'


#                         logger.info('we are inside data_stat == end ########################## end of if')


#                     if data_stat['situation']=='start' and fileready == "":
#                         logger.info('we are inside data_stat == start ##########################')

#                         os.system("cp 2m/2m_hls/"+prev_ts_name+" temp/"+prev_ts_name+"")
#                         #os.system("ffmpeg -i temp/"+prev_ts_name+" -ss "+str(time_to_cut)+" -t "+str(duration)+" -c:v libx264 -c:a copy -vf scale=640:360 -color_primaries bt470bg 2m/2m_hls/@"+prev_ts_name+"")
#                         os.system("ffmpeg -i temp/"+prev_ts_name+" -ss "+str(time_to_cut)+" -t "+str(duration)+" -c copy 2m/2m_hls/@"+prev_ts_name+"")
#                         logger.info('the cutting is done!')

#                         state_video.update({prev_ts_name:"start"})
#                         jsonFile7 = open("state_ts_video.json", "w+")
#                         jsonFile7.write(json.dumps(state_video))
#                         jsonFile7.close()


#                         data_stat['situation']="end"
#                         jsonFile2 = open("state.json", "w+")
#                         jsonFile2.write(json.dumps(data_stat))
#                         jsonFile2.close()

#                         logger.info('we are inside data_stat == start ########################## end of if')

#                     if fileready == "":
#                         save_changed_file(prev_ts_name)
#                         logger.info('save the changed file which is %s', prev_ts_name)

#                     if end=='yes' and fileready == "":
#                         data_stat['situation']="start"
#                         jsonFile2 = open("state.json", "w+")
#                         jsonFile2.write(json.dumps(data_stat))
#                         jsonFile2.close()

#             #get the latest video after applied the cut
#             #ts_result=get_latest_ts() ######""take it just from here







# new_current_time=datetime.now()
# if new_current_time>datetime.strptime(level2['end'], '%Y-%m-%d %H:%M:%S.%f') and data_stat['situation']=="start": #it means we did not detect the last jingle
#     data_stat['situation']="end"
#     jsonFile2 = open("state.json", "w+")
#     jsonFile2.write(json.dumps(data_stat))
#     jsonFile2.close()
#     logger.info('we miss the end of the jingle !!!!!')

# logger.info('preprocess is done')
##################" post.py"
# logger.info('post will take %s',segment)
# os.system('python3 post.py '+segment)
# logger.info('post is done')
end = time.time()
logger.info('the time of this script is %s',str(end-start))
