import subprocess
import sys
from pathlib import Path
import os
import re
import numpy as np
from matplotlib import pyplot as plt



output_folder_name ="/bulk_convert_out/"

droppedFile = sys.argv[1]
droppedName = Path(droppedFile).name
originalfiles = sys.orig_argv[2:]

size_list=[]
time_list=[]
seconds_list=[]
summed_list=[]
duration=""

#originalfiles = ['E:\\Shadowplay\\Returnal\\test.mov']

#long path shenanigans 
joined = (' '.join(originalfiles))
re.sub (' +', ' ', joined)
listed = re.split(r'(.+?mkv|.+?mov|.+?mp4)', joined)
listed = list(filter(None, listed))
listed = [i.lstrip() for i in listed]

#print(droppedName)

#get path of script location and remove the tail
head_tail = os.path.split(sys.argv[0])
output_path = head_tail[0]


#skip first part of sys.argv since it points to script location
arguments = sys.argv[1:]

#check if output folder exists, otherwise create it
isExist = os.path.exists(output_path+output_folder_name)
if not isExist:
    os.makedirs(output_path+output_folder_name)



ffprobe_args = '.\\ffprobe.exe -v error -select_streams v:0 -i "{file}" -show_entries stream=duration:frame=pkt_size,pkt_pts_time'.format(file=listed[0])
print(ffprobe_args)
ffprobe = subprocess.Popen(["powershell.exe", ffprobe_args], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='UTF-8')

for line in iter(ffprobe.stdout.readline,""):

    time_in = re.findall('(?<=pkt_pts_time=).*',line)
    size_in = re.findall('(?<=pkt_size=).*',line)
    for each in time_in:
        time_list.append(float(each))
    for each in size_in:
        size_list.append(float(each)/1000)



    #time_list = time_list + time_in
    #size_list = size_list + size_in
num_time = len(time_list)
num_size = len(size_list)
duration = time_list[num_time-1]
print("duration: ", duration)

print("time length: ", num_time)
print("size length:" , num_size)


def sum_seconds(list, x):
    for each in list:
        if each <= x:
            pass
        else:
            highest_index = time_list.index(each)
            return(int(highest_index))
a=0
b=len(time_list)        
x=1
while x < duration+1:
    
    outp = sum_seconds(time_list[a:b], x)
    x+=1
    
    seconds_list.append(outp)
    #print(outp)
    if x < duration+1:
        summed_list.append(sum(size_list[a:outp]))
        #print("summed list: ", summed_list)
    a=outp
#print(summed_list)

length=len(summed_list)
newl=range(length)
print(length)
print(sum(summed_list))
print(sum(size_list))
        
#print(time_list)
#print("printing out", ffprobe_out)

#plt.plot(size_list, time_list)
#plt.show()
#print(type(time))
#print(time[1:2])
fig, ax = plt.subplots()
ax.plot(newl, summed_list)
ax.set(xlabel='time', ylabel='KBps', label="test")
plt.fill_between(newl, summed_list)
plt.ticklabel_format(style='plain')
plt.title(listed[0])
plt.axhline(y=np.nanmean(summed_list), color='red', linestyle='--', label='Avg: '+str(round(np.nanmean(summed_list)))+'KBps')
plt.grid()
plt.margins(0)
plt.legend()
plt.show()


#k=input("press close to exit") 


#to question
# If I sum up all the frame bytes, and convert byte -> MebiByte (because windows says MegaByte but means Mebi, because MS), it is ~identical to file size in windows.
# So, why does my graph, that tracks nmcoder correctly, shows a much higher value? Nmcoder peaks at ~67725k, while mine peaks at 