#![allow(dead_code)]

use crate::hardware::pin::{Pin, PinDevice};
use embedded_hal::digital::PinState;
use log::trace;
use std::any::type_name;
use std::fmt::Debug;
use std::time::Instant;

pub const OUT1: PinoutChannel = PinoutChannel::ExtA(0);
pub const OUT2: PinoutChannel = PinoutChannel::ExtA(1);
pub const OUT3: PinoutChannel = PinoutChannel::ExtA(2);
pub const OUT4: PinoutChannel = PinoutChannel::ExtA(3);
pub const OUT5: PinoutChannel = PinoutChannel::ExtA(4);
pub const OUT6: PinoutChannel = PinoutChannel::ExtA(5);
pub const OUT7: PinoutChannel = PinoutChannel::ExtA(6);
pub const OUT8: PinoutChannel = PinoutChannel::ExtA(7);
pub const LED_A: PinoutChannel = PinoutChannel::ExtA(8);
pub const MOTOR1_B: PinoutChannel = PinoutChannel::ExtA(9);
pub const MOTOR1_A: PinoutChannel = PinoutChannel::ExtA(10);
pub const MOTOR0_B: PinoutChannel = PinoutChannel::ExtA(11);
pub const MOTOR0_A: PinoutChannel = PinoutChannel::ExtA(12);
pub const DRIVE0_BRAKE: PinoutChannel = PinoutChannel::ExtA(13);
pub const DRIVE0_DIR: PinoutChannel = PinoutChannel::ExtA(14);
pub const DRIVE0_DRIVEOFF: PinoutChannel = PinoutChannel::ExtA(15);
pub const RCS1: PinoutChannel = PinoutChannel::ExtB(0);
pub const RCS0: PinoutChannel = PinoutChannel::ExtB(1);
pub const OUT9: PinoutChannel = PinoutChannel::ExtB(2);
pub const MOTOR3_B: PinoutChannel = PinoutChannel::ExtB(3);
pub const MOTOR3_A: PinoutChannel = PinoutChannel::ExtB(4);
pub const MOTOR2_B: PinoutChannel = PinoutChannel::ExtB(5);
pub const MOTOR2_A: PinoutChannel = PinoutChannel::ExtB(6);
pub const RCS9: PinoutChannel = PinoutChannel::ExtB(7);
pub const LED_B: PinoutChannel = PinoutChannel::ExtB(8);
pub const RCS8: PinoutChannel = PinoutChannel::ExtB(9);
pub const RCS6: PinoutChannel = PinoutChannel::ExtB(10);
pub const RCS7: PinoutChannel = PinoutChannel::ExtB(11);
pub const RCS4: PinoutChannel = PinoutChannel::ExtB(12);
pub const RCS5: PinoutChannel = PinoutChannel::ExtB(13);
pub const RCS2: PinoutChannel = PinoutChannel::ExtB(14);
pub const RCS3: PinoutChannel = PinoutChannel::ExtB(15);

#[derive(Copy, Clone, Debug)]
pub enum PinoutChannel {
    ExtA(u8),
    ExtB(u8),
}

pub struct DevicePin<Device: PinDevice> {
    pin: u8,
    device: Device,
}

impl<Device: PinDevice> Pin for DevicePin<Device> {
    fn set(&mut self, value: PinState, valid_until: Instant, priority: u8) {
        trace!(
            "ChannelPin<Device={}>::set(self, value: {value:?}, valid_until: {valid_until:?}, priority: {priority})",
            type_name::<Device>()
        );
        self.device.set_pin(self.pin, value, valid_until, priority);
    }
}

pub enum ChannelPin<A: PinDevice, B: PinDevice> {
    ExtA(DevicePin<A>),
    ExtB(DevicePin<B>),
}

impl<A: PinDevice, B: PinDevice> Pin for ChannelPin<A, B> {
    fn set(&mut self, value: PinState, valid_until: Instant, priority: u8) {
        trace!(
            "ChannelPin<A={}, B={}>::set(self, value: {value:?}, valid_until: {valid_until:?}, priority: {priority})",
            type_name::<A>(),
            type_name::<B>()
        );
        match self {
            ChannelPin::ExtA(pin) => pin.set(value, valid_until, priority),
            ChannelPin::ExtB(pin) => pin.set(value, valid_until, priority),
        }
    }
}

impl PinoutChannel {
    pub fn get_pin<A, B>(self, ext_a: A, ext_b: B) -> ChannelPin<A, B>
    where
        A: PinDevice + Debug,
        B: PinDevice + Debug,
    {
        trace!("PinoutChannel::get_pin(self: {self:?}, ext_a: {ext_a:?}, ext_b: {ext_b:?}");
        match self {
            PinoutChannel::ExtA(pin) => ChannelPin::ExtA(DevicePin { pin, device: ext_a }),
            PinoutChannel::ExtB(pin) => ChannelPin::ExtB(DevicePin { pin, device: ext_b }),
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub enum PininChannel {
    In1 = 0,
    In2 = 1,
    In3 = 2,
    In4 = 3,
    In5 = 4,
    BatterySense = 5,
    In6 = 6,
    In7 = 7,
}
