import time
start = time.time()
import logging
import os, glob, sys, random
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_file.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)

with open("data.json", "r+") as jsonFile4: #get the level 1,2,3
        data_level = json.load(jsonFile4)

segment = str(sys.argv[1])

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 ="ffmpeg -y -i 2m/2m_hls/"+segment+" -vf 'select=gt(scene\,0.10)' -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)

# if data_stat['situation']=="end":
#     jingles=[image for num in [8,11] for image in glob.glob("new_jingles/start/jingle"+str(num)+"/*.png")]

# if data_stat['situation']=="start":
#     jingles=[image for num in [i for i in range(1,12)] for image in glob.glob("new_jingles/end/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:
        if data_stat['situation']=="end":
            jingles=[image for num in [8,11] for image in glob.glob("new_jingles/start/jingle"+str(num)+"/*.png")]

        if data_stat['situation']=="start":
            jingles=[image for num in [i for i in range(1,12)] for image in glob.glob("new_jingles/end/jingle"+str(num)+"/*.png")]
    #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)
                side=jingle.split('/')[1]
                if side=="end" and data_stat['situation']=="end":
                    logger.info("this a jingle of a program not for an Ad")
                else:
                    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('/')[2]

                        data_name['name']=parent
                        jsonFile = open("detected_jingle.json", "w+")
                        jsonFile.write(json.dumps(data_name))
                        jsonFile.close()
                        logger.info("the parent is %s", parent)
                        logger.info("the time_in_ts is %s", time_in_ts)

                        if parent=="jingle8":
                            parent=parent+'_'+data_stat['situation']
                            dict_time=data_time[parent]
                            time_in_jingle=float(dict_time[jingle.split('/')[3].split('.')[0]])
                        
                        else:
                            dict_time=data_time[parent]
                            time_in_jingle=float(dict_time[jingle.split('/')[3].split('.')[0]])
                        diff=time_in_ts-time_in_jingle
                        logger.info('111111111111111111111111')
                        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'
                            logger.info('222222222222222222222222')

                        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.
                                logger.info('33333333333333333333333333')



                            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('4444444444444444444444444444')

                        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 ##########################')
                            logger.info('55555555555555555555555555555')


                            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')


if data_level['level']==3 and data_level['insert_new_adbreak']=="on":

    logger.info('the level 3 is activated')
    data_level['insert_new_adbreak']="off"
    data_level['inside_adbreak']="yes"
    jsonFile4= open("data.json", "w+")
    jsonFile4.write(json.dumps(data_level))
    jsonFile4.close()



    random_file=random.choice(os.listdir("ts_videos_of_jingles"))
    os.system("echo '#EXT-X-DISCONTINUITY' >> result.m3u8")
    os.system("echo '#EXTINF:3.160000,'  >> result.m3u8")
    os.system("echo 'ts_videos_of_jingles/"+random_file+"'  >> result.m3u8")
    os.system("echo '#EXT-X-DISCONTINUITY' >> result.m3u8")
    os.system("echo '#EXT-X-CUE-OUT' >> result.m3u8")
    os.system("echo '#EXT-X-DISCONTINUITY' >> result.m3u8")

    res= data_level['adbreak_duration']/3
    num= res if data_level['adbreak_duration']%3==0 else int(res)+1
    # if data_level['adbreak_duration']%3==0:
    #     num=res
    # else:
    #     num=int(res)+1
    for i in range(num):

        os.system("echo '#EXTINF:3.000000,'  >> result.m3u8")
        os.system("echo 'output_3s.ts'  >> result.m3u8")

    os.system("echo '#EXT-X-CUE-IN' >> result.m3u8")
    os.system("echo '#EXT-X-DISCONTINUITY' >> result.m3u8")
    os.system("echo '#EXTINF:3.160000,'  >> result.m3u8")
    os.system("echo 'ts_videos_of_jingles/"+random_file+"'  >> result.m3u8")   
    os.system("echo '#EXT-X-DISCONTINUITY' >> result.m3u8")

    #data_level['dept_in_seconds']=data_level['dept_in_seconds']+data_level['adbreak_duration']
    data_level['dept_in_seconds']=data_level['dept_in_seconds']+num*3
    data_level['inside_adbreak']="no"
    jsonFile4= open("data.json", "w+")
    jsonFile4.write(json.dumps(data_level))
    jsonFile4.close()