import * as _0603 from '@typecad/passives/0603';
import { PCB, TrackBuilder, Component } from '@typecad/typecad';
import { BQ24210DQCR } from './BQ24210DQCR';

interface Ird_bq24210 {
    chargeCurrentMa?: number;  // Fast charge current in mA (default: 500mA)
    temperatureMonitoring?: boolean;  // Enable temperature monitoring (default: true)
    passives?: typeof _0603;
    pcb: PCB;
}

/**
 * ### BQ24210 - 800-mA Single-Cell Li-Ion Battery Solar Charger Package
 * 
 * Complete implementation of TI's BQ24210 Li-Ion battery charger with essential
 * external components based on the typical application circuit.
 * 
 * **Features:**
 * - Programmable fast charge current (50mA to 800mA)
 * - Input voltage dynamic power management (VBUS-DPM)
 * - Battery tracking mode for solar panel compatibility
 * - Temperature monitoring with NTC thermistor support
 * - Auto-enabling charging operations (EN tied to PG)
 * 
 * **Default Configuration (from schematic):**
 * - Charge current: 500mA (R_ISET = 787Ω)
 * - Temperature monitoring: Enabled (RT1 = 21.5kΩ voltage divider, no NTC simulation)
 * - Temperature monitoring disabled: RT1 + R_NTC (10kΩ) simulates NTC thermistor
 * - VDPM: Floating (battery tracking mode)
 * - Input/Output capacitors: 1µF ceramic
 * 
 * **Required External Connections:**
 * - VBUS: Input power source (3.5V to 18V)
 * - BAT: Battery connection and system load
 * - VSS/EP: Ground connection
 * - CHG: Connect LED with series resistor for charge status (optional)
 * - PG: Connect LED with series resistor for power good status (optional)
 * 
 * **Usage Example:**
 * ```typescript
 * let charger = new rd_bq24210({ 
 *   chargeCurrentMa: 500,
 *   temperatureMonitoring: true,
 *   pcb: typecad 
 * });
 * 
 * // Connect power source
 * typecad.net(solarPanel.positive, charger.U1.VBUS);
 * typecad.net(solarPanel.negative, charger.U1.VSS);
 * 
 * // Connect battery
 * typecad.net(battery.positive, charger.U1.BAT);
 * typecad.net(battery.negative, charger.U1.VSS);
 * ```
 */
export class rd_bq24210 {
    #passives: typeof _0603;
    pcb: PCB;
    components: (Component | TrackBuilder)[] = [];
    
    // Main IC
    U1: BQ24210DQCR;
    
    // Essential external components
    C1: _0603.Capacitor;  // VBUS bypass capacitor
    C2: _0603.Capacitor;  // BAT bypass capacitor
    R_ISET: _0603.Resistor;  // Fast charge current setting
    RT1: _0603.Resistor;  // Temperature sensing voltage divider (always present)
    R_NTC?: _0603.Resistor;  // NTC simulation resistor (when temp monitoring disabled)

    constructor({ chargeCurrentMa = 500, temperatureMonitoring = true, passives, pcb }: Ird_bq24210) {
        this.#passives = passives || _0603;
        this.pcb = pcb;

        // Calculate ISET resistor value: R_ISET = 390 / I_OUT (from datasheet)
        const isetResistorValue = Math.round(390 / (chargeCurrentMa / 1000));

        // Create the main IC
        this.U1 = new BQ24210DQCR();

        // Create essential external components with values from schematic
        this.C1 = new this.#passives.Capacitor({ 
            value: '1uF', 
            voltage: '10V',
            description: 'VBUS bypass capacitor (C1)' 
        });
        
        this.C2 = new this.#passives.Capacitor({ 
            value: '1uF', 
            voltage: '6V',
            description: 'BAT bypass capacitor (C2)' 
        });
        
        this.R_ISET = new this.#passives.Resistor({ 
            value: `${isetResistorValue}`,
            wattage: '0.1W',
            description: 'Fast charge current setting resistor (R_ISET)' 
        });

        // Always create temperature monitoring voltage divider resistor
        this.RT1 = new this.#passives.Resistor({ 
            value: '21.5k',
            description: 'Temperature sensing voltage divider resistor (RT1)' 
        });

        // Create NTC simulation resistor if temperature monitoring is disabled
        if (!temperatureMonitoring) {
            this.R_NTC = new this.#passives.Resistor({ 
                value: '10k',
                description: 'NTC thermistor simulation resistor (R_NTC)' 
            });
        }
        
        // VBUS bypass capacitor (C1: VBUS to VSS)
        this.pcb.net(this.U1.VBUS, this.C1.pin(1));
        this.pcb.net(this.U1.VSS, this.C1.pin(2));
        
        // BAT bypass capacitor (C2: BAT to VSS)  
        this.pcb.net(this.U1.BAT, this.C2.pin(1));
        this.pcb.net(this.U1.VSS, this.C2.pin(2));
        
        // Fast charge current setting resistor (R_ISET: ISET to VSS)
        this.pcb.net(this.U1.ISET, this.R_ISET.pin(1));
        this.pcb.net(this.U1.VSS, this.R_ISET.pin(2));
        
        // Auto-enabling: EN tied to PG (from schematic)
        this.pcb.net(this.U1.EN, this.U1.PG);
        
        // Temperature monitoring voltage divider (always present)
        // RT1: VTSB to TS (voltage divider for NTC thermistor)
        this.pcb.net(this.U1.VTSB, this.RT1.pin(1));
        this.pcb.net(this.U1.TS, this.RT1.pin(2));
        
        // NTC simulation resistor (only when temperature monitoring is disabled)
        if (!temperatureMonitoring && this.R_NTC) {
            // R_NTC: TS to VSS (simulates 10k NTC thermistor)
            this.pcb.net(this.U1.TS, this.R_NTC.pin(1));
            this.pcb.net(this.U1.VSS, this.R_NTC.pin(2));
        }
        
        // Connect thermal pad to ground
        this.pcb.net(this.U1.EP, this.U1.VSS);
        
        // Note: VDPM left floating for battery tracking mode (as per schematic)

        // Add all components to the components array
        this.components.push(this.U1, this.C1, this.C2, this.R_ISET, this.RT1);
        if (this.R_NTC) {
            this.components.push(this.R_NTC);
        }

        this.components.push(this.pcb.track().from({ x: 142.285, y: 105.07 }, "F.Cu", 0.3).to({ x: 140.91, y: 105.07, layer: "F.Cu", width: 0.3 }).to({ x: 140.615, y: 104.775, layer: "F.Cu", width: 0.3 }));
        this.components.push(this.pcb.track().from({ x: 142.285, y: 106.07 }, "F.Cu", 0.2).to({ x: 143.215, y: 106.07, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 144.145, y: 105.07 }, "F.Cu", 0.3).to({ x: 145.755, y: 105.07, layer: "F.Cu", width: 0.3 }).to({ x: 146.05, y: 104.775, layer: "F.Cu", width: 0.3 }));
        this.components.push(this.pcb.track().from({ x: 141.825, y: 105.57 }, "F.Cu", 0.2).to({ x: 140.715, y: 106.68, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 142.285, y: 105.57 }, "F.Cu", 0.2).to({ x: 141.825, y: 105.57, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 144.145, y: 106.57 }, "F.Cu", 0.2).to({ x: 144.145, y: 107.07, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 141.605, y: 106.753 }, "F.Cu", 0.2).to({ x: 141.605, y: 107.315, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 139.651, y: 107.809 }, "F.Cu", 0.2).to({ x: 138.875, y: 108.585, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 141.788, y: 106.57 }, "F.Cu", 0.2).to({ x: 141.605, y: 106.753, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 141.111, y: 107.809 }, "F.Cu", 0.2).to({ x: 139.651, y: 107.809, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 141.605, y: 107.315 }, "F.Cu", 0.2).to({ x: 141.111, y: 107.809, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 142.285, y: 106.57 }, "F.Cu", 0.2).to({ x: 141.788, y: 106.57, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 142.285, y: 107.41 }, "F.Cu", 0.2).to({ x: 141.11, y: 108.585, layer: "F.Cu", width: 0.2 }).to({ x: 140.525, y: 108.585, layer: "F.Cu", width: 0.2 }));
        this.components.push(this.pcb.track().from({ x: 142.285, y: 107.07 }, "F.Cu", 0.2).to({ x: 142.285, y: 107.41, layer: "F.Cu", width: 0.2 }));

        // Group all components 
        this.pcb.group('rd_bq24210', ...this.components);

        this.U1.pcb = { x: 143.215, y: 106.07, rotation: -90 };
        this.C1.pcb = { x: 139.84, y: 104.775, rotation: 180 };
        this.C2.pcb = { x: 146.825, y: 104.775, rotation: 0 };
        this.R_ISET.pcb = { x: 139.89, y: 106.68, rotation: 180 }; // Near ISET pin
        this.RT1.pcb = { x: 139.7, y: 108.585, rotation: 0 }; // Near TS/VTSB pins (always present)
        if (this.R_NTC) {
            this.R_NTC.pcb = { x: 141.5, y: 108.585, rotation: 0 }; // Near TS pin for NTC simulation
        }
    }
}