#  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 math
import pcbnew

import HelpfulFootprintWizardPlugin
import PadArray as PA


class THTElCapWizard(HelpfulFootprintWizardPlugin.HelpfulFootprintWizardPlugin):

    def GetName(self):
        return "CAPELTHT"

    def GetDescription(self):
        return "THT Electrolytic Capacitor Footprint Wizard v1.0"

    def GenerateParameterList(self):
        self.AddParam("Pads", "size", self.uMM, 10)
        self.AddParam("Pads", "height", self.uMM, 10)
        self.AddParam("Pads", "marking clearance", self.uMM, 0.3)
        self.AddParam("Pads", "raster", self.uMM, 5)
        self.AddParam("Pads", "pad diameter", self.uMM, 2)
        self.AddParam("Pads", "drill hole", self.uMM, 1)
        #self.AddParam("Pads", "marking width", self.uMM, 2)

    def CheckParameters(self):
        return
     
    def GetReferencePrefix(self):
        return "C"

    def GetValue(self):
        size = self.parameters["Pads"]["size"] / 100000
        height = self.parameters["Pads"]["height"] / 100000
        raster = self.parameters["Pads"]["raster"] / 100000
        hole = self.parameters["Pads"]["drill hole"] / 100000
        size, sfrag = divmod(size, 10)
        height, hfrag = divmod(height, 10)
        raster, rfrag = divmod(raster, 10)
        hole, hlfrag = divmod(hole, 10)
        if sfrag==0:
            t_size = "%d" % size
        else:
            t_size = "%d.%d" % (size, sfrag)
        if hfrag==0:
            t_height = "%d" % height
        else:
            t_height = "%d.%d" % (height, hfrag)
        if rfrag==0:
            t_raster = "%d" % raster
        else:
            t_raster = "%d.%d" % (raster, rfrag)
        if hlfrag==0:
            t_hole = "%d" % hole
        else:
            t_hole = "%d.%d" % (hole, hlfrag)
        return "c_elec_tht_D%s_L%s_P%s_H%s" % (t_size, t_height, t_raster, t_hole) 

    def BuildThisFootprint(self):
        pads = self.parameters["Pads"]
        size = pads["size"]
        height = pads["height"]
        pad_diameter = pads["pad diameter"]
        drill_hole = pads["drill hole"]
        pad_offset = pads["raster"] / 2
        mark_clearance = pads["marking clearance"]
        m_width = size / 2
        first_pad = PA.PadMaker(self.module).THPad(pad_diameter, pad_diameter, drill_hole, pcbnew.PAD_SHAPE_RECT)
        normal_pad = PA.PadMaker(self.module).THRoundPad(pad_diameter, drill_hole)        
        pin1Pos = pcbnew.wxPoint(0, 0)
        array = PA.PadLineArray(normal_pad, 2, pad_offset * 2, False, pin1Pos)
        array.SetFirstPadInArray(1)
        array.SetFirstPadType(first_pad)
        array.AddPadsToModule(self.draw) 
        r = size / 2
        self.draw.Circle(0,0,r)
        m_clearance = pcbnew.FromMM(0.5)
        # #arc_starty = (pad_width / 2) + m_clearance
        # #angle_intercept = math.acos(arc_starty / r)
        # #sx = -(math.sin(angle_intercept) * r)                
        # #self.draw.Arc(0, 0, sx, -arc_starty, (2 * angle_intercept) * 572.9578)
        # #self.draw.Arc(0, 0, -sx, arc_starty, (2 * angle_intercept) * 572.9578)
        # #m_offs = self.draw.GetWidth() / 1.1 # marking line offset
        m_offs = self.draw.dc["lineThickness"] / 2
        # #m_num = int(round((m_width-(r+sx)) / m_offs, 0)) 
        rm = (pad_diameter / 2) + mark_clearance + self.draw.dc["lineThickness"] / 2
        self.draw.Circle(pad_offset,0,rm)
        rm = rm + self.draw.dc["lineThickness"] / 2.2
        m_num = int(round(m_width / m_offs, 0))
        for i in range(1,m_num):
            offs = i * m_offs
            angle_intercept = math.acos(offs/r)
            y = math.sin(angle_intercept) * r - m_offs / 2
            if (offs > pad_offset - rm) and (offs < pad_offset + rm):
                angle_intercept2 = math.acos((offs - pad_offset) / rm)
                y2 = math.sin(angle_intercept2) * rm - m_offs / 2
                self.draw.VLine(offs,-y,y-y2)
                self.draw.VLine(offs,y2,y-y2)
            else:
                self.draw.VLine(offs,-y,y*2)                 
            # # angle_intercept = math.acos((r-offs)/r)
            # # y = -(math.sin(angle_intercept) * r - m_offs / 2)
            # # self.draw.VLine(-r+offs,y,abs(y)-arc_starty) 
            # # self.draw.VLine(-r+offs,arc_starty,abs(y)-arc_starty)      
        # # self.draw.HLine(sx,arc_starty,(m_num - 1) * m_offs) 
        # # self.draw.HLine(sx,-arc_starty,(m_num - 1) * m_offs)
        # # notch = size / 6        
        # # self.draw.Polyline([
            # # (- size / 2, - arc_starty),
            # # (- size / 2, - size / 2),
            # # (size / 2 - notch, - size / 2),
            # # (size / 2, - (size / 2 - notch)),
            # # (size / 2, - arc_starty)
        # # ])  
        # # self.draw.Polyline([
            # # (- size / 2, arc_starty),
            # # (- size / 2, size / 2),
            # # (size / 2 - notch, size / 2),
            # # (size / 2, size / 2 - notch),
            # # (size / 2, arc_starty)
        # # ])             
        text_size = pcbnew.FromMM(1.2) 
        text_offset = text_size + size / 2
        self.draw.Value(0, text_offset, text_size)
        self.draw.Reference(0, -text_offset, text_size)

THTElCapWizard().register()
