use crate::connection_config::*;
use crate::connection_status::*;
use crate::connections::*;
use crate::receiver::*;
use crossbeam::channel::Sender;
use std::sync::{Arc, Mutex};

pub struct BluetoothConnection {
    config: BluetoothConnectionConfig,
    serial_connection: SerialConnection,
}

impl BluetoothConnection {
    pub fn new(config: &BluetoothConnectionConfig) -> Self {
        let serial_config = SerialConnectionConfig {
            port_name: config.port_name.clone(),
            baud_rate: 115200,
            rts_cts_enabled: false,
        };

        let serial_connection = SerialConnection::new(&serial_config);

        Self {
            config: config.clone(),
            serial_connection,
        }
    }
}

impl GenericConnection for BluetoothConnection {
    fn open(&mut self) -> std::io::Result<()> {
        self.serial_connection.open()
    }

    fn close(&self) {
        self.serial_connection.close();
    }

    fn get_config(&self) -> ConnectionConfig {
        ConnectionConfig::BluetoothConnectionConfig(self.config.clone())
    }

    fn get_status(&self) -> ConnectionStatus {
        self.serial_connection.get_status()
    }

    fn get_receiver(&self) -> Arc<Mutex<Receiver>> {
        self.serial_connection.get_receiver()
    }

    fn get_write_sender(&self) -> Option<Sender<Vec<u8>>> {
        self.serial_connection.get_write_sender()
    }
}