#!/usr/bin/env python3
import datetime, os
from subprocess import call
from threading import Thread
import sys

_INSTANCES = {
    "typist": 10,
    "anyway": 10,
    "tic-tac-toe": 10
}

# ./bin/run "prime" "aman-aplan" "four" "ranges"
# ./bin/run "triples" "powers" "glob" "alphabetical" 
# ./bin/run  "order" "abba" "warmup" "backrefs" 
# ./bin/run "balance" "long-count" "anchors" 

INSTANCES = {
    "warmup": 10,
    "anchors": 10,
    "ranges": 10,
    "backrefs": 10,
    "abba": 10,
    "aman-aplan": 10,
    "prime": 15,
    "four": 10,
    "order": 10,
    "triples": 30,
    "glob": 20,
    "balance": 10,
    "powers": 10,
    "long-count": 270,
    "alphabetical": 20,
}

SEEDS = [2967301147, 5600405384, 1632819610, 1412034944, 9724161943, 4037724866, # 5
         8370032799, 2767986875, 3141506592, 3313543896, 8657690939, 9696640194, # 11
         6621545651, 2492462788, 3404588599, 2330977742, 6361187489, 2805026820, # 17
         4323456912, 8361101999, 9935136846, 5217407358, 5619256299, 4560422870,
         5236888092, 1337360711, 7659837985, 2521100877, 8801535107, 9651599436]

DEPTHS = [1, 3, 5, 7, 10, 13, 15, 18, 20]
# DEPTHS = [0]

BUDGETS = [i * 1000 for i in (300, )]

FOLDER_NAME = 'depth'


def execute(cmd):
    cmd = list(map(str, cmd))
    call(cmd, shell=os.name == 'nt')

instances = INSTANCES.keys() # if len(sys.argv) == 1 else sys.argv[1:]
DEPTHS = sys.argv[1:]


# Validating
for instance in instances:
    if instance not in INSTANCES.keys():
        print("Invalid instance name:", instance)
        exit(1)

program = 'ILS_Shrink'
for index, seed in enumerate(SEEDS):
    for depth in DEPTHS:
        for budget in BUDGETS:        
            for instance in instances:
                weight = INSTANCES[instance]
                today = datetime.date.today().strftime("%Y-%m-%d")
                resultsDir = FOLDER_NAME
                absResultsDir = os.path.join(os.getcwd(), "results", resultsDir)

                if not os.path.isdir(absResultsDir):
                    os.mkdir(absResultsDir)

                absCurrentDir = os.path.join(os.getcwd(), "results", "current")
                if not os.path.isdir(absCurrentDir):
                    os.mkdir(absCurrentDir)

                absCsvFilePath = os.path.join(absResultsDir, "%s.csv" % program)
                csvFilePath = os.path.join(resultsDir, "%s.csv" % program)

                if not os.path.isfile(absCsvFilePath):
                    call(['touch', absCsvFilePath])

                cmd = ["npm", "run", "localsearch", "--",
                    "--name", program,
                    "--instance", instance,
                    "--weight", weight,
                    "--depth", depth,
                    "--index", "%s" % index,
                    "--seed", seed,
                    "--log-level", 2,
                    "--budget", budget,
                    "--csv", csvFilePath]

                print(" ".join(map(lambda x: str(x), cmd)))

                thread = Thread(target=execute, args=(cmd,))
                thread.start()
                thread.join()
