use crate::public::{
  KeybdKey::{self, *},
  MouseButton::{self, *},
};

/// Keycodes may be found here: https://eastmanreference.com/complete-list-of-applescript-key-codes
// TODO: add new keys, alter some names to match existing
impl From<KeybdKey> for u64 {
    fn from(key: KeybdKey) -> u64 {
        match key {
            DeleteKey => 0x33,
            TabKey => 0x30,
            EnterKey => 0x24,
            EscapeKey => 0x35,
            SpaceKey => 0x31,
            LeftKey => 0x7B,
            UpKey => 0x7E,
            RightKey => 0x7C,
            DownKey => 0x7D,
            Numrow0Key => 0x1D,
            Numrow1Key => 0x12,
            Numrow2Key => 0x13,
            Numrow3Key => 0x14,
            Numrow4Key => 0x15,
            Numrow5Key => 0x17,
            Numrow6Key => 0x16,
            Numrow7Key => 0x1A,
            Numrow8Key => 0x1C,
            Numrow9Key => 0x19,
            // EqualKey => 0x18,
            // MinusKey => 0x1B,
            AKey => 0x00,
            BKey => 0x0B,
            CKey => 0x08,
            DKey => 0x02,
            EKey => 0x0E,
            FKey => 0x03,
            GKey => 0x05,
            HKey => 0x04,
            IKey => 0x22,
            JKey => 0x26,
            KKey => 0x28,
            LKey => 0x25,
            MKey => 0x2E,
            NKey => 0x2D,
            OKey => 0x1F,
            PKey => 0x23,
            QKey => 0x0C,
            RKey => 0x0F,
            SKey => 0x01,
            TKey => 0x11,
            UKey => 0x20,
            VKey => 0x09,
            WKey => 0x0D,
            XKey => 0x07,
            YKey => 0x10,
            ZKey => 0x06,
            Numpad0Key => 0x52,
            Numpad1Key => 0x53,
            Numpad2Key => 0x54,
            Numpad3Key => 0x55,
            Numpad4Key => 0x56,
            Numpad5Key => 0x57,
            Numpad6Key => 0x58,
            Numpad7Key => 0x59,
            Numpad8Key => 0x5B,
            Numpad9Key => 0x5C,
            // NumpadDecimalKey => 0x41,
            // NumpadMultiply => 0x43,
            // NumpadPlusKey => 0x45,
            // NumpadClearKey => 0x47,
            // NumpadDivideKey => 0x4B,
            // NumpadEnterKey => 0x4C,
            // NumpadMinusKey => 0x4E,
            // NumpadEqualsKey => 0x51,
            F1Key => 0x7A,
            F2Key => 0x78,
            F3Key => 0x63,
            F4Key => 0x76,
            F5Key => 0x60,
            F6Key => 0x61,
            F7Key => 0x62,
            F8Key => 0x64,
            F9Key => 0x65,
            F10Key => 0x6D,
            F11Key => 0x67,
            F12Key => 0x6F,
            F13Key => 0x69,
            F14Key => 0x6B,
            F15Key => 0x71,
            F16Key => 0x6A,
            F17Key => 0x40,
            F18Key => 0x4F,
            F19Key => 0x50,
            F20Key => 0x5A,
            // CommandKey => 0x37,
            LShiftKey => 0x38,
            // CapsLock => 0x39,
            // OptionKey => 0x3A,
            // Control => 0x3B,
            RShiftKey => 0x3C,
            // ROption => 0x3D,
            // RControl => 0x3E,
            // Function => 0x3F,
            HomeKey => 0x73,
            // RBracket => 0x1E,
            // LBracket => 0x21,
            // QuoteKey => 0x27,
            // Semicolon => 0x29,
            // Backslash => 0x2A,
            // CommaKey => 0x2B,
            // SlashKey => 0x2C,
            // PeriodKey => 0x2F,
            OtherKey(code) => code,
            _ => panic!("")
        }
    }
}

impl From<u64> for KeybdKey {
    fn from(code: u64) -> KeybdKey {
        match code {
            0x33 => DeleteKey,
            0x30 => TabKey,
            0x24 => EnterKey, // RETURN key
            0x35 => EscapeKey,
            0x31 => SpaceKey,
            0x7B => LeftKey,
            0x7E => UpKey,
            0x7C => RightKey,
            0x7D => DownKey,
            0x1D => Numrow0Key,
            0x12 => Numrow1Key,
            0x13 => Numrow2Key,
            0x14 => Numrow3Key,
            0x15 => Numrow4Key,
            0x17 => Numrow5Key,
            0x16 => Numrow6Key,
            0x1A => Numrow7Key,
            0x1C => Numrow8Key,
            0x19 => Numrow9Key,
            // 0x18 => EqualKey,
            // 0x1B => MinusKey,
            0x00 => AKey,
            0x0B => BKey,
            0x08 => CKey,
            0x02 => DKey,
            0x0E => EKey,
            0x03 => FKey,
            0x05 => GKey,
            0x04 => HKey,
            0x22 => IKey,
            0x26 => JKey,
            0x28 => KKey,
            0x25 => LKey,
            0x2E => MKey,
            0x2D => NKey,
            0x1F => OKey,
            0x23 => PKey,
            0x0C => QKey,
            0x0F => RKey,
            0x01 => SKey,
            0x11 => TKey,
            0x20 => UKey,
            0x09 => VKey,
            0x0D => WKey,
            0x07 => XKey,
            0x10 => YKey,
            0x06 => ZKey,
            0x52 => Numpad0Key,
            0x53 => Numpad1Key,
            0x54 => Numpad2Key,
            0x55 => Numpad3Key,
            0x56 => Numpad4Key,
            0x57 => Numpad5Key,
            0x58 => Numpad6Key,
            0x59 => Numpad7Key,
            0x5B => Numpad8Key,
            0x5C => Numpad9Key,
            // 0x41 => NumpadDecimalKey,
            // 0x43 => NumpadMultiply,
            // 0x45 => NumpadPlusKey,
            // 0x47 => NumpadClearKey,
            // 0x4B => NumpadDivideKey,
            // 0x4C => NumpadEnterKey,
            // 0x4E => NumpadMinusKey,
            // 0x51 => NumpadEqualsKey,
            0x7A => F1Key,
            0x78 => F2Key,
            0x63 => F3Key,
            0x76 => F4Key,
            0x60 => F5Key,
            0x61 => F6Key,
            0x62 => F7Key,
            0x64 => F8Key,
            0x65 => F9Key,
            0x6D => F10Key,
            0x67 => F11Key,
            0x6F => F12Key,
            0x69 => F13Key,
            0x6B => F14Key,
            0x71 => F15Key,
            0x6A => F16Key,
            0x40 => F17Key,
            0x4F => F18Key,
            0x50 => F19Key,
            0x5A => F20Key,
            // 0x37 => CommandKey,
            0x38 => LShiftKey,
            0x39 => CapsLockKey ,
            // 0x3A => OptionKey,
            0x3B => LControlKey,
            0x3C => RShiftKey,
            // 0x3D => ROption ,
            0x3E => RControlKey,
            // 0x3F => FunctionKey,
            0x73 => HomeKey,
            // 0x1E => RBracket,
            // 0x21 => LBracket ,
            // 0x27 => QuoteKey,
            // 0x29 => Semicolon,
            // 0x2A => Backslash,
            // 0x2B => CommaKey,
            // 0x2C => SlashKey,
            // 0x2F => PeriodKey,
            _ => OtherKey(code),
        }
  }
}

// TODO: find these
impl From<MouseButton> for u32 {
    fn from(button: MouseButton) -> u32 {
        match button {
            LeftButton => 0x01,
            RightButton => 0x02,
            MiddleButton => 0x04,
            X1Button => 0x05,
            X2Button => 0x06,
            OtherButton(code) => code,
        }
    }
}
