from itertools import chain
from kicad_sym import read_txt
from kicad_footprint import *

def finger(number, x, y, s):
    width = 0.7
    height = 7.0
    return pad(number, x, y, width, height, s)

def number_side(number):
    if number[0] == 'A':
        return side.back
    elif number[0] == 'B':
        return side.front

def pads(lines):
    spacing = 1.0
    x = 0.65 + (0.7 / 2.0)
    y = 3.5
    for (number, name, type) in lines:
        yield finger(number, x, y, number_side(number))
        x += spacing
        if '36' in number:
            x += 0.65 + (0.7 / 2) + 6

def cut(x0, y0, x1, y1):
    return fp_line(x0, y0, x1, y1, 0.10, "Edge.Cuts")

def cuts():
    yield cut(0, -4,
              0, 8)
    yield cut(37, -4,
              37, 8)
    yield cut(37 + 6, -4,
              37 + 6, 8)
    yield cut(37 + 6 + 32, -4,
              37 + 6 + 32, 8)

    yield cut(0, 8,
              37, 8)

    yield cut(37, -4,
              37 + 6, -4)

    yield cut(37 + 6, 8,
              37 + 6 + 32, 8)

lines = read_txt("cn1.txt")
from pprint import pprint
a_pads = filter(lambda l: 'A' in l[0], lines)
b_pads = filter(lambda l: 'B' in l[0], lines)
print(footprint("SATURN_CN1", "SATURN_CN1",
                "\n".join(chain(pads(a_pads),
                                pads(b_pads),
                                cuts(),
                                )
                          )
                )
      )
