import logging, argparse, os, sys, re
import numpy as np
from collections import OrderedDict
from .utils import getWorkDirs, checkSymLink, getEnergy4Key
from ..ccsgp.ccsgp import make_panel
from ..ccsgp.utils import getOpts
from ..ccsgp.config import default_colors

medium_opts = 'with filledcurves lt 1 lw 4 pt 0 lc %s' % default_colors[8]
cocktail_opts = 'with lines lc %s lw 5 lt 1' % default_colors[-2]
data_opts = 'lt 1 lw 4 ps 1.5 lc %s pt 18' % default_colors[0]

def gp_panel(version, skip):
  """example for a panel plot using QM12 data (see gp_xfac)

  .. image:: pics/panelQM12.png
     :width: 700 px

  :param version: plot version / input subdir name
  :type version: str
  """
  inDir, outDir = getWorkDirs()
  inDir = os.path.join(inDir, version)
  data = {}
  for file in os.listdir(inDir):
    energy = re.compile('\d+').search(file).group()
    if skip is not None and energy == skip: continue
    data_type = re.sub('%s\.dat' % energy, '', file)
    file_url = os.path.join(inDir, file)
    data_import = np.loadtxt(open(file_url, 'rb'))
    data_import = data_import[data_import[:,0] < 1.1]
    if data_type == 'cocktail': data_import[:,2:] = 0.
    elif data_type == '+medium': data_import[:,2] = 0.
    key = getEnergy4Key(energy)
    if key not in data: data[key] = [ data_import ]
    else: data[key].append(data_import)
  make_panel(
    dpt_dict = OrderedDict(
      (' '.join([k, 'GeV']), [
        data[k],
        [ medium_opts, cocktail_opts, data_opts ] if len(data[k]) > 2
        else [ cocktail_opts, data_opts],
        [ '+medium', 'cocktail', 'data' ] if len(data[k]) > 2
        else [ 'cocktail', 'data' ]
      ]) for k in sorted(data, key=float)
    ),
    name = os.path.join(
      outDir, 'panel%s%s' % (version, 'No'+skip if skip is not None else '')
    ),
    ylabel = '1/N@_{mb}^{evt} dN@_{ee}^{acc.}/dM_{ee} [ (GeV/c^2)^{-1} ]',
    xlabel = 'invariant dielectron mass, M_{ee} (GeV/c^{2})',
    ylog = True, xr = [0, 1.1], yr = [1e-4, 20],
    lmargin = 0.1, bmargin = 0.15,
    arrow_length = 0.4, arrow_bar = 0.002,
    gpcalls = ['mxtics 2'] + (['label %d "" at graph 0.4,0.7' % (
      8 if skip is None else 6
    )] if version == 'QM12Latest200' else []),
    labels = {'STAR Preliminary': [0.4,0.7,False]}
  )
  return 'done'

if __name__ == '__main__':
  checkSymLink()
  parser = argparse.ArgumentParser()
  parser.add_argument("version", help="version = subdir name of input dir")
  parser.add_argument("--skip", help="skip an energy", metavar="energy")
  parser.add_argument("--log", help="show log output", action="store_true")
  args = parser.parse_args()
  loglevel = 'DEBUG' if args.log else 'WARNING'
  logging.basicConfig(
    format='%(message)s', level=getattr(logging, loglevel)
  )
  print gp_panel(args.version, args.skip)

@@ -264,213 +264,8 @@
 rs%0A%0A
-medium_opts = 'with filledcurves lt 1 lw 4 pt 0 lc %25s' %25 default_colors%5B8%5D%0Acocktail_opts = 'with lines lc %25s lw 5 lt 1' %25 default_colors%5B-2%5D%0Adata_opts = 'lt 1 lw 4 ps 1.5 lc %25s pt 18' %25 default_colors%5B0%5D%0A%0A
 def 
@@ -1083,63 +1083,323 @@
 %5D = 
-%5B data_import %5D%0A    else: data%5Bkey%5D.append(data_import)
+%7B%7D%0A    data%5Bkey%5D%5Bdata_type%5D = data_import%0A  plot_order = %5B'+medium', 'cocktail', 'data'%5D%0A  plot_opts = %7B%0A    '+medium': 'with filledcurves lt 1 lw 4 pt 0 lc %25s' %25 default_colors%5B8%5D,%0A    'cocktail': 'with lines lc %25s lw 5 lt 1' %25 default_colors%5B-2%5D,%0A    'data': 'lt 1 lw 4 ps 1.5 lc %25s pt 18' %25 default_colors%5B0%5D%0A  %7D
 %0A  m
@@ -1479,16 +1479,18 @@
     
+ %5B
  data%5Bk%5D
 ,%0A  
@@ -1489,79 +1489,53 @@
 a%5Bk%5D
-,%0A        %5B medium_opts, cocktail_opts, data_opts %5D if len(
+%5Bdt%5D for dt in plot_order if dt in 
 data%5Bk%5D
-) %3E 2
+ %5D,
 %0A   
@@ -1543,103 +1543,64 @@
     
-else %5B cocktail_opts, data_opts%5D,%0A        %5B '+medium', 'cocktail', 'data' %5D if len(
+%5B plot_opts%5Bdt%5D for dt in plot_order if dt in 
 data%5Bk%5D
-) %3E 2
+ %5D,
 %0A   
@@ -1608,33 +1608,50 @@
     
-else %5B 'cocktail', '
+%5B dt for dt in plot_order if dt in 
 data
-'
+%5Bk%5D
  %5D%0A 

fix plot order for all versions