#!/usr/bin/env python3
"""Warmed Home under throttle: record the screen while the sidebar moves; find when the picture settles after each key."""
import json, os, subprocess, sys, time
from lib import *
factor=sys.argv[1]; name=sys.argv[2]
keys=[('Down',1),('Down',2),('Down',3),('Up',2),('Up',1),('Up',0)]
b('ActivateWindow(Home)',300); b('SetFocus(9000,0)',2000)
th=subprocess.Popen([sys.executable,HERE+'/throttle.py',factor]) if float(factor)>1 else None
time.sleep(3)
mov=HERE+'/perf/%s.mov'%name
if os.path.exists(mov): os.remove(mov)
rec=subprocess.Popen(['screencapture','-x','-v','-V','44',mov],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
t0=time.time(); time.sleep(3)
marks=[]
for key,pos in keys:
    marks.append(round(time.time()-t0,2))
    do({'op':'builtin','cmd':'Action(%s)'%key})
    time.sleep(6)
rec.wait()
if th: th.terminate(); th.wait(); subprocess.run(['pkill','-CONT','-x','Kodi'])
json.dump(marks,open(HERE+'/perf/%s.json'%name,'w')); print(marks)
