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
import requests


# response = requests.get(f"http://207.180.254.4:9060/DAIManagement/vast_reader/99/1")
# print(response.text)

print("============ +++++++ ========== we are in main3.py")

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')

import asyncio
from telegram import Bot
async def send_telegram_message(chat_id, msg):
    bot = Bot("5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4")
    await bot.send_message(chat_id=chat_id, text=msg)


async def send_bot_message(chat_id, bot_token, msg):
    bot = Bot(bot_token)
    await bot.send_message(chat_id=chat_id, text=msg)


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)


# This The New Edit  15-01-2024
# jingles=[image for num in [i for i in range(1,7)] for image in glob.glob("2m_jingles/jingle"+str(num)+"/*.png")]
iframes_jingles = glob.glob("2m_jingles/**/*.png", recursive=True)
iframes_programs = glob.glob("2m_programs/**/*.png", recursive=True)

jingles = iframes_jingles + iframes_programs


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:
                # asyncio.run(send_telegram_message("-1001778433367", "from server 2 there is similarity between "+jingle+" and "+filename +" with results = "+str(result)))
                #send("from server 2 there is similarity between "+jingle+" and "+filename +" with results = "+str(result),"-1001778433367", token=my_token)
                # add_time()  #add 20 s
                # This The New Edit  15-01-2024
                if '2m_programs' in jingle: 
                    # 
                    # asyncio.run(send_telegram_message("-1001778433367", "from server 2 (program start)  there is similarity between "+jingle+" and "+filename +" with results = "+str(result)))
                    asyncio.run(
                        send_bot_message(
                        "-1001778433367", 
                        "5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4",
                        f"""
                        - Show       : {' '.join([word.capitalize() for word in jingle.split('/')[1].split('_')])}
                        - Time       : {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
                        - Message    : matching iframe [{filename}]
                        - Similarity : {str(result)}
                        """
                        )
                    )
                    #  
                    asyncio.run(
                        send_bot_message(
                            "-1002134982127", 
                            "5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4",
                            f"""
                            - Show: {' '.join([word.capitalize() for word in jingle.split('/')[1].split('_')])}
                            - Time: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
                            - Msg: matching iframe [{filename}]
                            """
                        )
                    ) 
                if '2m_jingles'  in jingle:
                    asyncio.run(send_telegram_message("-1001778433367", "from server 2 there is similarity between "+jingle+" and "+filename +" with results = "+str(result)))
                    # asyncio.run(
                    #     send_bot_message(
                    #         "-1001778433367", 
                    #         "5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4",
                    #         f"""
                    #         - Jingle: {' '.join([word.capitalize() for word in jingle.split('/')[1].split('_')])}
                    #         - Time: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
                    #         - Message: matching iframe [{filename}]
                    #         - Similarity: {str(result)}
                    #         """
                    #     )
                    # )
                    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 are inside the same jingle')
                    else:
                        logger.info('the first time we detect this jingle !!')
                        date_of_last_jingle = datetime.strptime(last_detected_jingle['date'], '%Y-%m-%d %H:%M:%S.%f')
                        date_of_current_jingle = now+timedelta(seconds=15)
                        duration_since_last_jignle = date_of_current_jingle - date_of_last_jingle
                        duration_since_last_jignle = duration_since_last_jignle.total_seconds()

                        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("python3 CBA_video.py")

                            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+"")
                            #os.system("ffmpeg -i temp/"+prev_ts_name+" -ss 00 -t "+str(time_to_cut)+" -c copy -copyts 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 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()

                            lastHourDateTime = datetime.now() - timedelta(hours = 1)
                            lastHourDateTime_api = lastHourDateTime.strftime("%Y-%m-%d %H:%M:%S")
                            lastHourDateTime_api = str(lastHourDateTime_api).replace(" ", "_")
                            logger.info('duration since last = %s ', str(round(duration_since_last_jignle)))
                            # os.system("python3 api_request.py 1 "+lastHourDateTime_api+" "+str(round(duration_since_last_jignle))+" jingle"+)
                            os.system("python3 api_request.py 1 "+str(round(duration_since_last_jignle)))
                            #os.system("echo python3 api_request.py 1 "+str(round(duration_since_last_jignle))+" >> api_logs.txt")
                            logger.info('called the python function api_request.py 1 %s ',str(round(duration_since_last_jignle)))
                            #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 -copyts 2m/2m_hls/@"+prev_ts_name+"")
                            

                            


                            

                            logger.info('we are inside data_stat == start ########################## end of if')
                            # logger.info('we are inside data_stat == start ##########################')

                            # os.system("cp 2m/2m_hls/"+prev_ts_name+" temp/"+prev_ts_name+"")
                            # lastHourDateTime = datetime.now() - timedelta(hours = 1)
                            # lastHourDateTime_api = lastHourDateTime.strftime("%Y-%m-%d %H:%M:%S")
                            # lastHourDateTime_api = str(lastHourDateTime_api).replace(" ", "_")
                            # logger.info('duration since last = %s ', str(round(duration_since_last_jignle)))
                            # # os.system("python3 api_request.py 1 "+lastHourDateTime_api+" "+str(round(duration_since_last_jignle))+" jingle"+)
                            # os.system("python3 api_request.py 1 "+str(round(duration_since_last_jignle)))
                            # # os.system("echo python3 api_request.py 1 "+str(round(duration_since_last_jignle))+" >> api_logs.txt")
                            # logger.info('called the python function api_request.py 1 %s ',str(round(duration_since_last_jignle)))
                            # #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+"")
                            # #os.system("ffmpeg -i temp/"+prev_ts_name+" -ss "+str(time_to_cut)+" -t "+str(duration)+" -c copy -copyts 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()





import requests
import json

with open("state_ts_video.json", "r+") as jsonFile7: #insert all changed ts videos
    state_video = json.load(jsonFile7)

values_list = list(state_video.values())

# Get the last two values from the list
latest_values = values_list[-2:]

# Check if the two latest values are similar
if latest_values[0] == latest_values[1]:
    message="we detect "+str(latest_values[0])+" after "+str(latest_values[0])
    base_url = 'https://api.telegram.org/bot5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4/sendMessage?chat_id=-1001778433367&text="{}"'.format(message)
    requests.get(base_url)
    

    



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()
