// Modified version of choc.js
// Source for modifications: https://github.com/daprice/keyswitches.pretty/blob/master/Kailh_socket_PG1350.kicad_mod
// ---------------------------
// Kailh Choc PG1350
// Nets
//    from: corresponds to pin 1
//    to: corresponds to pin 2
// Params
//    hotswap: default is false
//      if true, will include holes and pads for Kailh choc hotswap sockets
//    reverse: default is false
//      if true, will flip the footprint such that the pcb can be reversible
//    keycaps: default is false
//      if true, will add choc sized keycap box around the footprint
//
// note: hotswap and reverse can be used simultaneously

module.exports = {
    params: {
        designator: 'S',
        hotswap: false,
        reverse: false,
        keycaps: false,
        from: undefined,
        to: undefined,
    },
    body: p => {
        const standard = `
            (module PG1350 (layer F.Cu) (tedit 5DD50112)
                ${p.at /* parametric position */}
                (attr smd)

                ${'' /* footprint reference */}
                (fp_text reference "${p.ref}" (at 0 0) (layer F.SilkS) ${p.ref_hide} (effects (font (size 1.27 1.27) (thickness 0.15))))
                (fp_text value "" (at 0 0) (layer F.SilkS) hide (effects (font (size 1.27 1.27) (thickness 0.15))))

                ${'' /* LED hole marks */}
                (fp_line (start -2.6 3.1) (end 2.6 3.1) (layer Dwgs.User) (width 0.15))
                (fp_line (start 2.6 3.1) (end 2.6 6.3) (layer Dwgs.User) (width 0.15))
                (fp_line (start 2.6 6.3) (end -2.6 6.3) (layer Dwgs.User) (width 0.15))
                (fp_line (start -2.6 3.1) (end -2.6 6.3) (layer Dwgs.User) (width 0.15))

                ${'' /* middle shaft */}
                (pad "" np_thru_hole circle (at 0 0) (size 3.429 3.429) (drill 3.429) (layers *.Cu *.Mask))

                ${'' /* stabilizers */}
                (pad "" np_thru_hole circle (at 5.5 0) (size 1.7018 1.7018) (drill 1.7018) (layers *.Cu *.Mask))
                (pad "" np_thru_hole circle (at -5.5 0) (size 1.7018 1.7018) (drill 1.7018) (layers *.Cu *.Mask))
        `;

        const keycap = `
            ${'' /* keycap marks */}
            (fp_line (start -9 -8.5) (end 9 -8.5) (layer Dwgs.User) (width 0.15))
            (fp_line (start 9 -8.5) (end 9 8.5) (layer Dwgs.User) (width 0.15))
            (fp_line (start 9 8.5) (end -9 8.5) (layer Dwgs.User) (width 0.15))
            (fp_line (start -9 8.5) (end -9 -8.5) (layer Dwgs.User) (width 0.15))
        `;

        function switch_marks(reversed) {
            // Draw switch marks on the front side (the side with the switches, not with the sockets)
            let def_side = reversed ? 'B' : 'F';

            return `
                ${'' /* switch marks */}
                (fp_line (start -7 -7) (end -1.5 -7) (layer ${def_side}.SilkS) (width 0.15))
                (fp_line (start 1.5 -7) (end  7 -7) (layer ${def_side}.SilkS) (width 0.15))
                (fp_line (start  7 -7) (end  7  7) (layer ${def_side}.SilkS) (width 0.15))
                (fp_line (start  7  7) (end -7  7) (layer ${def_side}.SilkS) (width 0.15))
                (fp_line (start -7  7) (end -7 -7) (layer ${def_side}.SilkS) (width 0.15))
            `;
        }

        function pins(reversed) {
            let def_neg = reversed ? '' : '-';
            let def_pos = reversed ? '-' : '';
            let def_side = reversed ? 'F' : 'B';
            let def_pin1 = reversed ? p.to : p.from;
            let def_pin2 = reversed ? p.from : p.to;

            if (p.hotswap) {
                return `
                    ${'' /* hotswap socket marks */}
                    (fp_line (start ${def_pos}7    -5.6) (end ${def_pos}7    -6.2)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_neg}1.5  -3.7) (end ${def_pos}1    -3.7)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_arc  (start ${def_pos}1    -2.2) (end ${def_pos}2.5  -2.2) (angle ${def_neg}90) (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_pos}1.5  -8.2) (end ${def_pos}2    -7.7)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_pos}2    -6.7) (end ${def_pos}2    -7.7)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_pos}7    -6.2) (end ${def_pos}2.5  -6.2)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_neg}2    -4.2) (end ${def_neg}1.5  -3.7)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_neg}2    -7.7) (end ${def_neg}1.5  -8.2)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_neg}1.5  -8.2) (end ${def_pos}1.5  -8.2)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_arc  (start ${def_pos}2.5  -6.7) (end ${def_pos}2    -6.7) (angle ${def_neg}90) (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_pos}2.5  -2.2) (end ${def_pos}2.5  -1.5)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_pos}2.5  -1.5) (end ${def_pos}7    -1.5)                      (layer ${def_side}.SilkS) (width 0.15))
                    (fp_line (start ${def_pos}7    -1.5) (end ${def_pos}7    -2  )                      (layer ${def_side}.SilkS) (width 0.15))

                    (fp_arc  (start ${def_pos}2.5  -6.7 ) (end ${def_pos}2    -6.7 ) (angle ${def_neg}90) (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_neg}1.5  -8.2 ) (end ${def_pos}1.5  -8.2 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_neg}2    -7.7 ) (end ${def_neg}1.5  -8.2 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_neg}1.5  -3.7 ) (end ${def_pos}1    -3.7 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_neg}2    -4.2 ) (end ${def_neg}1.5  -3.7 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_pos}7    -6.2 ) (end ${def_pos}2.5  -6.2 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_pos}2    -6.7 ) (end ${def_pos}2    -7.7 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_pos}1.5  -8.2 ) (end ${def_pos}2    -7.7 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_arc  (start ${def_pos}1    -2.2 ) (end ${def_pos}2.5  -2.2 ) (angle ${def_neg}90) (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_pos}2.5  -1.5 ) (end ${def_pos}7    -1.5 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_pos}2.5  -2.2 ) (end ${def_pos}2.5  -1.5 )                      (layer ${def_side}.Fab) (width 0.15))
                    (fp_line (start ${def_neg}2    -4.25) (end ${def_neg}2    -7.7 )                      (layer ${def_side}.Fab) (width 0.12))
                    (fp_line (start ${def_pos}7    -1.5 ) (end ${def_pos}7    -6.2 )                      (layer ${def_side}.Fab) (width 0.12))
                    (fp_line (start ${def_pos}7    -5   ) (end ${def_pos}9.5  -5   )                      (layer ${def_side}.Fab) (width 0.12))
                    (fp_line (start ${def_pos}9.5  -5   ) (end ${def_pos}9.5  -2.5 )                      (layer ${def_side}.Fab) (width 0.12))
                    (fp_line (start ${def_pos}9.5  -2.5 ) (end ${def_pos}7    -2.5 )                      (layer ${def_side}.Fab) (width 0.12))
                    (fp_line (start ${def_neg}2    -4.75) (end ${def_neg}4.5  -4.75)                      (layer ${def_side}.Fab) (width 0.12))
                    (fp_line (start ${def_neg}4.5  -4.75) (end ${def_neg}4.5  -7.25)                      (layer ${def_side}.Fab) (width 0.12))
                    (fp_line (start ${def_neg}4.5  -7.25) (end ${def_neg}2    -7.25)                      (layer ${def_side}.Fab) (width 0.12))

                    ${'' /* holes */}
                    (pad "" np_thru_hole circle (at ${def_pos}5 -3.75) (size 3 3) (drill 3) (layers *.Cu *.Mask))
                    (pad "" np_thru_hole circle (at 0 -5.95) (size 3 3) (drill 3) (layers *.Cu *.Mask))

                    ${'' /* net pads */}
                    (pad 1 smd rect (at ${def_neg}3.275 -5.95 ${p.r}) (size 2.6 2.6) (layers ${def_side}.Cu ${def_side}.Paste ${def_side}.Mask) ${def_pin1})
                    (pad 2 smd rect (at ${def_pos}8.275 -3.75 ${p.r}) (size 2.6 2.6) (layers ${def_side}.Cu ${def_side}.Paste ${def_side}.Mask) ${def_pin2})
                `;
            } else {
                return `
                    ${'' /* pins */}
                    (pad 1 thru_hole circle (at ${def_pos}5 -3.8) (size 2.032 2.032) (drill 1.27) (layers *.Cu *.Mask) ${def_pin1})
                    (pad 2 thru_hole circle (at ${def_pos}0 -5.9) (size 2.032 2.032) (drill 1.27) (layers *.Cu *.Mask) ${def_pin2})
                `;
            }
        }

        if (p.reverse) {
            return `
                ${standard}
                ${p.keycaps ? keycap : ''}
                ${switch_marks(false)}
                ${switch_marks(true)}
                ${pins(false)}
                ${pins(true)}
            )`;
        } else {
            return `
                ${standard}
                ${p.keycaps ? keycap : ''}
                ${switch_marks(false)}
                ${pins(false)}
            )`;
        }
    }
}
