#  This program 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 2 of the License, or
#  (at your option) any later version.
#
#  This program 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.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#

from __future__ import division
import pcbnew

import HelpfulFootprintWizardPlugin
import PadArray as PA


class CONNWizard(HelpfulFootprintWizardPlugin.HelpfulFootprintWizardPlugin):

    def GetName(self):
        return "CONN"

    def GetDescription(self):
        return "Connector Footprint Wizard v1.1"

    def GenerateParameterList(self):
        self.AddParam("Pads", "n", self.uNatural, 10)
        self.AddParam("Pads", "pad pitch", self.uMM, 2.54)
        self.AddParam("Pads", "pad width", self.uMM, 1.5)
        self.AddParam("Pads", "pad length", self.uMM, 2)
        self.AddParam("Pads", "drill", self.uMM, 0.9)
        self.AddParam("Body", "body offset", self.uMM, -2.5)
        self.AddParam("Body", "body length", self.uMM, 5)
        self.AddParam("Body", "body width", self.uMM, 5)
        #self.AddParam("Body", "body width", self.uMM, 2.54)

    def CheckParameters(self):
        self.CheckParamInt("Pads", "*n")

    def GetValue(self):
        return "CONN %d" % self.parameters["Pads"]["*n"]

    def BuildThisFootprint(self):
        pads = self.parameters["Pads"]
        body = self.parameters["Body"]
        pad_pitch = pads["pad pitch"]
        pad_length = pads["pad length"]
        pad_width = pads["pad width"]
        drill = pads["drill"]
        body_offset = body["body offset"]
        body_length = body["body length"]
        body_width = body["body width"]
        pad_num = pads["*n"]
        first_pad = PA.PadMaker(self.module).THPad(pad_length, pad_width, drill, pcbnew.PAD_RECT)
        normal_pad = PA.PadMaker(self.module).THPad(pad_length, pad_width, drill, pcbnew.PAD_OVAL)        
        pin1Pos = pcbnew.wxPoint(0, 0)
        array = PA.PadLineArray(normal_pad, pad_num, pad_pitch, False, pin1Pos)
        array.SetFirstPadInArray(1)
        array.SetFirstPadType(first_pad)
        array.AddPadsToModule(self.draw)  
        self.draw.Box(0,body_offset + body_length / 2, body_width, body_length)
        #xoffset = -(pad_num - 1) * pad_pitch / 2       
        #for i in range(pad_num):
        #    self.draw.Box(xoffset + i * pad_pitch , body_offset + body_length / 2, body_width, body_length)        
        
        text_size = pcbnew.FromMM(1.2) 
        text_offset = text_size - min( -pad_length / 2, body_offset) 
        self.draw.Value(0, -text_offset, text_size)
        self.draw.Reference(0, max(body_offset + body_length, pad_length / 2) + text_size, text_size)

CONNWizard().register()
