import os.path
import base64
import json
from args import *

class Job:
  next_id = 0
  def __init__(self, in_file, base_dir = None, out_dir = None):
    if not os.path.isfile(in_file):
      raise Exception("Failed: couldn't open " + in_file)
    self.id = Job.next_id
    Job.next_id += 1
    self.in_file = abspath(in_file)
    split = os.path.split(self.in_file)
    self.base_dir = base_dir if base_dir is not None else split[0]
    if not file_in_path(self.in_file, self.base_dir):
      raise Exception("Failed: " + in_file + " is not in base dir " + self.base_dir)
    self.name = os.path.splitext(split[1])[0]
    self.done = False
    self.graphics = []
    with open(self.in_file, mode="r") as f:
      graphics = []
      try:
        file_json = json.loads(f.read())
      except:
        raise Exception("Failed: " + in_file + " does not contain valid data")
      if not "graphics" in file_json or not isinstance(file_json["graphics"], list):
        raise Exception("Failed: " + in_file + " does not contain a \"graphics\" list")
      for graphic_json in file_json["graphics"]:
        self.graphics.append(Graphic(graphic_json))

  def get_cmd(self):
    return {"graphics":[g.get_cmd() for g in self.graphic], "id":self.id}

class Graphic:
  def __init__(self, json):
    self.json = json
    
  def get_cmd():
    return {}
