include <idt.scad>
include <bond_pad.scad>

// creates the input and output idts
// [TODO] clean this
module bi_directional_idt(
    c, 
    f, 
    n_fingers, 
    distance = 100, 
    finger_length = 50, 
    gap = 0, 
    thickness = 0, 
    impedance = 50,

    // bond pad prams
    has_bond_pads = false,
    bond_pad_leg_length = 0.00005, 
    bond_pad_leg_width = 0.00001875, 
    bond_pad_size = 0.000075,
    bond_pad_angle = 45,
    bond_pad_bond_pad_shape = "square",

    // id number prams
    id_number = 0,
    has_id = false,
    id_flip_dir = [1, 1, 1],
    // extra
    post_scale = 1,
    echo_stats = false,
    center = false
) {
        

    lambda = c/f * post_scale;
    // dont worry about these magic fractions
    length_of_input = n_fingers*lambda+1.74*lambda;


    should_be_centered = center ? 1 : 0;
    center_offset_y = length_of_input + distance * lambda/2;
    center_offset_x = finger_length*lambda/2 + gap/2 + thickness;

    translate([-center_offset_x * should_be_centered, -center_offset_y * should_be_centered, 0]) {

    if (has_id) {
        color([255/255, 0/255, 255/255])
        translate([finger_length*lambda/2, has_bond_pads ? -(bond_pad_leg_length * post_scale * cos(bond_pad_angle)) : -lambda*finger_length/2, 0])
        scale(id_flip_dir)
        text(str(id_number), size=id_text_size, halign="center");
    }
    
    

    // echo(str("lambda_scale = ", post_scale));
    delay_line(lambda, n_fingers, finger_length, gap, thickness);


    color([0/255, 255/255, 128/255]) {
        translate([0, distance*lambda+length_of_input, 0]) {
            delay_line(lambda, n_fingers, finger_length, gap, thickness);
        }
    }

    // near ori
    rotate([0, 0, -90]) {
        if (has_bond_pads) {
            color([255/255, 255/255, 255/255]) {
                bond_pad(
                    leg_length_tweak = lambda/4,
                    leg_width=c/f/4, 
                    leg_length=length(
                        [0,0,0], 
                        [bond_pad_leg_length, bond_pad_leg_length, 0]
                    ), 
                    leg_angle = -bond_pad_angle,
                    size = bond_pad_size, 
                    post_scale=scale_unit
                );
            }
        }
    }
    // near side
    
    translate([lambda*finger_length+gap+thickness*2+lambda, 0, 0]) {
        rotate([0, 0, -90]) {
                if (has_bond_pads) {
                    bond_pad(
                        leg_length_tweak = lambda/5,
                        leg_width=c/f/4, 
                        leg_length=length(
                            [0,0,0], 
                            [bond_pad_leg_length, bond_pad_leg_length, 0]
                        ), 
                        leg_angle = bond_pad_angle,
                        size = bond_pad_size, 
                        post_scale=scale_unit
                    );
            }
        }
    }

    // far side
    translate([0, length_of_input+distance*lambda+length_of_input, 0]) {   
            rotate([0, 0, 90]) {                    
                if (has_bond_pads) {
                    bond_pad(
                        leg_length_tweak = lambda/5,
                        leg_width=c/f/4, 
                        leg_length=length(
                            [0,0,0], 
                            [bond_pad_leg_length, bond_pad_leg_length, 0]
                        ), 
                        leg_angle = bond_pad_angle,
                        size = bond_pad_size, 
                        post_scale=scale_unit
                    );
                }
            }
            // furthest side
            translate([lambda*finger_length+gap+thickness*2+lambda, 0, 0]) {
                rotate([0, 0, 90]) {                    
                        if (has_bond_pads) {
                            bond_pad(
                                leg_length_tweak = lambda/4,
                                leg_width=c/f/4, 
                                leg_length=length(
                                    [0,0,0], 
                                    [bond_pad_leg_length, bond_pad_leg_length, 0]
                                ), 
                                leg_angle = -bond_pad_angle,
                                size = bond_pad_size, 
                                post_scale=scale_unit
                            );
                    }
                }
            }
    } 
    }


    if (echo_stats) {
        echo("===== DELAY LINE IDT INFO ===== ");
        echo(str("lambda = ", lambda));
        echo(str("min feature size lambda/4 = ", lambda/4));
        echo(str("impedance = ", impedance));
        echo(str("total capacitance = ", 1/(2*PI*f*impedance)));
        echo("===== DELAY LINE IDT INFO ===== ");
    }
}
