# Copyright 2014 Bah

# This file is part of SCFleetGen.

# SCFleetGen is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

# SCFleetGen is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

# The full text of the GPL license is available at http://www.gnu.org/licenses/.


import os
import subprocess

target_width = 600  # in pixels
input_dir = 'input/'
output_dir = 'output/'
temp_dir = 'temp/'
result_file = 'fleet.png'
bg_color = '#808080'

# data array. Fleet will have the same ordering as the array
ships = []
ships.append({"name":"M50", "qty":83, "infile":"m50.png", "outfile":"m50.png"})
ships.append({"name":"Aurora", "qty":517, "infile":"aurora.png", "outfile":"aurora.png"})
# ships.append({"name":"Aurora ES", "qty":54, "infile":"aurora-es.png", "outfile":"aurora-es.png"})
# ships.append({"name":"Aurora MR", "qty":84, "infile":"aurora-mr.png", "outfile":"aurora-mr.png"})
# ships.append({"name":"Aurora CL", "qty":3, "infile":"aurora-cl.png", "outfile":"aurora-cl.png"})
# ships.append({"name":"Aurora LN", "qty":245, "infile":"aurora-ln.png", "outfile":"aurora-ln.png"})
# ships.append({"name":"Aurora LX", "qty":131, "infile":"aurora-lx.png", "outfile":"aurora-lx.png"})
ships.append({"name":"Avenger", "qty":206, "infile":"avenger.png", "outfile":"avenger.png"})
ships.append({"name":"Hornet", "qty":605, "infile":"hornet.png", "outfile":"hornet.png"})
# ships.append({"name":"Hornet F7C", "qty":115, "infile":"hornet-f7c.png", "outfile":"hornet-f7c.png"})
# ships.append({"name":"Hornet F7C-S (Ghost)", "qty":84, "infile":"hornet-f7c-s.png", "outfile":"hornet-f7c-s.png"})
# ships.append({"name":"Hornet F7C-R (Tracker)", "qty":46, "infile":"hornet-f7c-r.png", "outfile":"hornet-f7c-r.png"})
# ships.append({"name":"Hornet F7C-M (Super Hornet)", "qty":360, "infile":"hornet-f7c-m.png", "outfile":"hornet-f7c-m.png"})
ships.append({"name":"Gladiator", "qty":143, "infile":"gladiator.png", "outfile":"gladiator.png"})
ships.append({"name":"Scythe", "qty":25, "infile":"scythe.png", "outfile":"scythe.png"})
ships.append({"name":"300i", "qty":570, "infile":"300i.png", "outfile":"300i.png"})
# ships.append({"name":"300i", "qty":93, "infile":"300i.png", "outfile":"300i.png"})
# ships.append({"name":"315p", "qty":119, "infile":"315p.png", "outfile":"315p.png"})
# ships.append({"name":"325a", "qty":204, "infile":"325a.png", "outfile":"325a.png"})
# ships.append({"name":"350r", "qty":154, "infile":"350r.png", "outfile":"350r.png"})
ships.append({"name":"Cutlass", "qty":194, "infile":"cutlass.png", "outfile":"cutlass.png"})
ships.append({"name":"Freelancer", "qty":252, "infile":"freelancer.png", "outfile":"freelancer.png"})
ships.append({"name":"Constellation", "qty":352, "infile":"constellation.png", "outfile":"constellation.png"})
ships.append({"name":"Retaliator", "qty":271, "infile":"retaliator.png", "outfile":"retaliator.png"})
ships.append({"name":"Caterpillar", "qty":87, "infile":"caterpillar.png", "outfile":"caterpillar.png"})
ships.append({"name":"Starfarer", "qty":110, "infile":"starfarer.png", "outfile":"starfarer.png"})
ships.append({"name":"Idris", "qty":43, "infile":"idris.png", "outfile":"idris.png"})
# ships.append({"name":"Idris-P", "qty":31, "infile":"idris-p.png", "outfile":"idris-p.png"})
# ships.append({"name":"Idris-M", "qty":12, "infile":"idris-m.png", "outfile":"idris-m.png"})
ships.append({"name":"Banu Merchantmen", "qty":52, "infile":"bmm.png", "outfile":"bmm.png"})


for ship in ships:
    ship_w = int(subprocess.check_output('identify -format "%w" ' + input_dir + ship['infile'], shell=True))
    ship_h = int(subprocess.check_output('identify -format "%h" ' + input_dir + ship['infile'], shell=True))
    per_line = int(target_width/ship_w)      #rounding down
	#Main body: all lines are completely filled with ships)
    num_lines = int(ship['qty']/per_line)	 #rounding down 	
    main_w = ship_w * per_line
    main_h = ship_h * num_lines	
	#Extra line: whenever the last line isn't completely filled
    extra_num = ship['qty'] - per_line * num_lines 
    extra_w = ship_w * extra_num

    os.system('convert ' + input_dir + ship['infile'] + ' ( -clone 0 ) +append -write mpr:sometile +delete -size ' + str(main_w) + 'x' + str(main_h) + ' -background ' + bg_color + ' tile:mpr:sometile ' + output_dir + ship['outfile'])
	# If there's an extra line, add that to the main body image
    if extra_num != 0:
        os.system('convert ' + output_dir + ship['outfile'] + ' ( -clone 0 ) +append -background ' + bg_color + ' -write mpr:sometile +delete -size ' + str(extra_w) + 'x' + str(ship_h) + ' tile:mpr:sometile '+ temp_dir + ship['outfile'])
        os.system('convert  ' + output_dir + ship['outfile'] + ' ' + temp_dir + ship['outfile'] + ' -append -background ' + bg_color + ' ' + output_dir + ship['outfile'])

# Creates 1 pixel thin image file so that there's something to append to
os.system('convert -size ' + str(target_width) + 'x1 xc:none '  + output_dir + result_file);		
		
# Second loop on the dictionary, must be done separately so that all temp images are in place		
for ship in ships:
    os.system('convert ' + output_dir + result_file + ' ' + output_dir + ship['outfile'] + ' -append -background ' + bg_color + ' ' + output_dir + result_file)
