#!/usr/bin/python
from __future__ import division
from __future__ import with_statement
import os
import sys
import toolbox_idynomics
import toolbox_plotting

path = sys.argv[1]
sim_dir = toolbox_idynomics.SimulationDirectory(path)

color_dict_path = os.path.join(sim_dir.figures_dir, 'color_info.txt')
if os.path.isfile(color_dict_path):
    species_color_dict = toolbox_idynomics.read_color_dict(color_dict_path)
else:
    species_color_dict = toolbox_idynomics.get_default_species_colors(sim_dir)
    toolbox_idynomics.save_color_dict(species_color_dict, color_dict_path)

fig = toolbox_plotting.PlosFigure()
axis = fig.add_subplot('', 111)

plot_left, plot_right, text_left = 30, 35, 38
y, y_diff = 100, -60

max_time = 1
max_abundance = 1

for species_name in sim_dir.get_species_names():
    time = []
    abundance = []
    for iter_info in sim_dir.get_iterate_information():

sim = toolbox_idynomics.SimulationDirectory(options.results_dir)

if not options.file_ext[0] == '.':
    options.file_ext = '.'+options.file_ext

save_name = 'biofilm_'+options.solute_name

num_digits = len(str(sim.get_last_iterate_number()))
    
color_dict_path = os.path.join(sim.figures_dir, 'color_info.txt')
if os.path.isfile(color_dict_path):
    species_color_dict = toolbox_idynomics.read_color_dict(color_dict_path)
else:
    species_color_dict = toolbox_idynomics.get_default_species_colors(sim)
    toolbox_idynomics.save_color_dict(species_color_dict, color_dict_path)

nI, nJ, nK, res = sim.find_domain_dimensions()
if options.i_max > 0:
    nI = options.i_max

counter = 0

if options.figure_type == None:
    if options.height > 0: height = options.height
    else: height = toolbox_plotting.mm2inch(nI * res)
    if options.width > 0: width = options.width
    else: width = toolbox_plotting.mm2inch(nJ * res)
    figure = toolbox_plotting.SlideFigure(width=width, height=height)
else:
def isPredecessor(early, late):
    if not ( early.vars['family'] == late.vars['family'] ):
        return False
    early_genealogy = int(early.vars['genealogy'])
    late_genealogy = int(late.vars['genealogy'])
    late_generation = int(late.vars['generation'])
    return (late_genealogy == early_genealogy + 2**(late_generation))

for path in args:
    sim_dir = toolbox_idynomics.SimulationDirectory(path)
    color_dict_path = os.path.join(sim_dir.figures_dir, 'color_info.txt')
    if os.path.isfile(color_dict_path):
        species_color_dict= toolbox_idynomics.read_color_dict(color_dict_path)
    else:
        species_color_dict = \
                         toolbox_idynomics.get_default_species_colors(sim_dir)
        toolbox_idynomics.save_color_dict(species_color_dict, color_dict_path)
    fig = toolbox_plotting.PlosFigure()
    axis = fig.add_subplot('', 111)
    max_time = 0.0
    max_val = 0.0
    min_val = 0.0
    cells = []
    for iter_info in sim_dir.get_iterate_information():
        max_time = max(max_time, iter_info.time)
        for cell in iter_info.agent_output.get_all_cells():
            family = int(cell.vars['family'])
            genealogy = int(cell.vars['genealogy'])
            attribute_val = float(cell.vars[options.attribute])
            max_val = max(max_val, attribute_val)
            min_val = min(min_val, attribute_val)