#!/usr/bin/env python

# We will use the inkex module with the predefined Effect base class.
import inkex
# The simplestyle module provides functions for style parsing.
from simplestyle import *
from math import tan, radians

def points_to_svgd(p, close=True):
  """ convert list of points (x,y) pairs
    into a closed SVG path list
  """
  f = p[0]
  p = p[1:]
  svgd = 'M%.4f,%.4f' % f
  for x in p:
    svgd += 'L%.4f,%.4f' % x
  if close:
    svgd += 'z'
  return svgd
  
def create_path(points, color, group):
  # Create path
  path = points_to_svgd( points )
  # define style using basic dictionary
  style = { 'stroke': color, 'fill': 'none', 'stroke-width': 0.5 }
  # convert style into svg form (see import at top of file)
  mypath_attribs = { 'style': formatStyle(style), 'd': path }
  # add path to scene (Create SVG Path under group)
  inkex.etree.SubElement(group, inkex.addNS('path','svg'), mypath_attribs )

class HoneycombEffect(inkex.Effect):
  def __init__(self):
    inkex.Effect.__init__(self)

    self.OptionParser.add_option("--numx", action="store",
      type="int", dest="numx", default="3",
      help="Specifies the number of horizontal divisions")
    self.OptionParser.add_option("--numy", action="store",
      type="int", dest="numy", default="4",
      help="Specifies the number of vetical divisions")
    self.OptionParser.add_option("--height", action="store",
      type="int", dest="height", default="10",
      help="Specifies the height of each division")
    self.OptionParser.add_option("--width", action="store",
      type="int", dest="width", default="5",
      help="Specifies the width of each division")

  def effect(self):
    numx = self.options.numx
    numy = self.options.numy
    height = self.options.height
    width = self.options.width

    t = "translate(%s,%s)" % (self.view_center[0], self.view_center[1])
    g_attribs = { inkex.addNS("Label", "inkscape"): "HoneycombPattern", "transform": t }
    topgroup = inkex.etree.SubElement(self.current_layer, "g", g_attribs)

    total_height = numy*height+height
    total_width = numx*width*4+width

    l = 0
    for x in range(1, numx * 4 + 1):
      l += 1
      l %= 4
      if l == 0 or l == 1:
        color = "blue"
      else:
        color = "red"
      create_path([(x*width,0), (x*width,total_height)], color, topgroup)

    fold = "red"
    cut = False
    x = 0
    x2 = 1
    for y in range(1, numy + 1):
      while x < numx * 4 + 1:
        if cut:
          color = "green"
        else:
          color = fold
        create_path([(x*width,y*height), (x2*width,y*height)], color, topgroup)

        if cut:
          if x == 0:
            x += 2
            x2 += 1
          else:
            x += 3
            x2 +=1
        else:
          x += 1
          x2 += 3
          if x2 > numx * 4 + 1:
            x2 = numx * 4 + 1

        cut = not cut

      if fold == "red":
        fold = "blue"
        cut = True
        x = 0
        x2 = 2
      else:
        fold = "red"
        cut = False
        x = 0
        x2 = 1

    #outline
    create_path([(0,0), (total_width,0), (total_width,total_height), (0,total_height)], "black", topgroup)

effect = HoneycombEffect()
effect.affect()