import sys
import pickle as pkl

import sciunit_tree
import ExecutionTree as exT
from algorithms import pc


def replay_sequence(tree_binary, cache_size, replay_order_binary):
    sciunit_execution_tree, _ = sciunit_tree.tree_load(tree_binary)
    tree = exT.create_tree('SCIUNIT', tree_binary)
    tree.cache_size = cache_size
    pc(tree)
    with open(replay_order_binary, 'wb') as robf:
        pkl.dump((sciunit_execution_tree, tree), robf)


if __name__ == '__main__':
    if len(sys.argv) < 5:
        print('Usage replay-order.py pc|prpv1|prpv2|lfu <cache_size> <input tree.bin> <output replay-order.bin>')

    if sys.argv[1] in ['pc']:
        replay_sequence(sys.argv[3], float(sys.argv[2]), sys.argv[4])
    elif sys.argv[1] in ['prpv1', 'prpv2', 'lfu']:
        raise NotImplementedError(f'{sys.argv[1]} Replay Order Not Implemented')
    else:
        print('Usage replay-order.py pc|prpv1|prpv2|lfu <cache_size> <input tree.bin> <output replay-order.bin>')
