# order is important
SPECIAL_BUTTONS = {
    'CONTROL_RIGHT': 'MOD_CONTROL_RIGHT',
    'CONTROL_LEFT': 'MOD_CONTROL_LEFT',
    'CONTROL': 'MOD_CONTROL_LEFT',
    'CTRL_RIGHT': 'MOD_CONTROL_RIGHT',
    'CTRL_LEFT': 'MOD_CONTROL_LEFT',
    'CTRL': 'MOD_CONTROL_LEFT',
    'SHIFT_RIGHT': 'MOD_SHIFT_RIGHT',
    'SHIFT_LEFT': 'MOD_SHIFT_LEFT',
    'SHIFT': 'MOD_SHIFT_LEFT',
    'ALT_RIGHT': 'MOD_ALT_RIGHT',
    'ALT_LEFT': 'MOD_ALT_LEFT',
    'ALT': 'MOD_ALT_LEFT',
    'GUI_RIGHT': 'MOD_GUI_RIGHT',
    'GUI_LEFT': 'MOD_GUI_LEFT',
    'GUI': 'MOD_GUI_LEFT',
    'WINDOWS': 'MOD_GUI_LEFT'
}

# menu = Shift + F10
BUTTONS = {
    'a': 'KEY_A',
    'b': 'KEY_B',
    'c': 'KEY_С',
    'd': 'KEY_D',
    'e': 'KEY_E',
    'f': 'KEY_F',
    'g': 'KEY_G',
    'h': 'KEY_H',
    'i': 'KEY_I',
    'j': 'KEY_J',
    'k': 'KEY_K',
    'l': 'KEY_L',
    'm': 'KEY_M',
    'n': 'KEY_N',
    'o': 'KEY_O',
    'p': 'KEY_P',
    'q': 'KEY_Q',
    'r': 'KEY_R',
    's': 'KEY_S',
    't': 'KEY_T',
    'u': 'KEY_U',
    'v': 'KEY_V',
    'w': 'KEY_W',
    'x': 'KEY_X',
    'y': 'KEY_Y',
    'z': 'KEY_Z',
    'F1': 'KEY_F1',
    'F2': 'KEY_F2',
    'F3': 'KEY_F3',
    'F4': 'KEY_F4',
    'F5': 'KEY_F5',
    'F6': 'KEY_F6',
    'F7': 'KEY_F7',
    'F8': 'KEY_F8',
    'F9': 'KEY_F9',
    'F10': 'KEY_F10',
    'F11': 'KEY_F11',
    'F12': 'KEY_F12',
    'LEFTARROW': 'KEY_ARROW_LEFT',
    'RIGHTARROW': 'KEY_ARROW_RIGHT',
    'UPARROW': 'KEY_ARROW_UP',
    'DOWNARROW': 'KEY_ARROW_DOWN',
    'LEFT': 'KEY_ARROW_LEFT',
    'RIGHT': 'KEY_ARROW_RIGHT',
    'UP': 'KEY_ARROW_UP',
    'DOWN': 'KEY_ARROW_DOWN',
    'DELETE': 'KEY_DELETE',
    'DEL': 'KEY_DELETE',
    'PRINTSCREEN': 'KEY_PRT_SCR',
    'TAB': 'KEY_TAB',
    'ESCAPE': 'KEY_ESC',
    'SPACE': 'KEY_SPACE',
    'ENTER': 'KEY_ENTER'
}

# Digispark program fragment
digitem = '''//generated by Duckyspark https://github.com/toxydose/Duckyspark

#include "DigiKeyboard.h"
#define KEY_ESC     41
#define KEY_BACKSPACE 42
#define KEY_TAB     43
#define KEY_PRT_SCR 70
#define KEY_DELETE  76
#define KEY_ARROW_RIGHT 0x4F
#define KEY_ARROW_DOWN  0x51
#define KEY_ARROW_UP    0x52

void setup() {

DigiKeyboard.delay(5000);
DigiKeyboard.sendKeyStroke(0);
'''


# ---------------------------------------
def Duckyspark_translator(duckystr):
    digistr = digitem
    strlist = list(duckystr.split('\n'))
    for i in strlist:
        line = i.strip('\n')
        if len(line) < 1:
            digistr += ''
        else:
            if 'REM' in line:
                digistr += '//' + line.replace('REM ', '') + "\n"
            else:
                if 'DELAY' in line:  # 替换注释
                    digistr += "DigiKeyboard." + line.replace(
                        'DELAY', 'delay(').replace(' ', '') + ");\n"
                elif 'STRING' in line:  # 替换 打印字符串
                    digistr += "DigiKeyboard." + line.replace(
                        '"',
                        '")); DigiKeyboard.print(char(34)); DigiKeyboard.print(F("'
                    ).replace(
                        '\\',
                        '")); DigiKeyboard.print(char(92)); DigiKeyboard.print(F("'
                    ).replace('STRING ', 'print(F("') + "\"));\n"
                elif 'MENU' in line:  # 替换 右键菜单
                    # print('DigiKeyboard.sendKeyStroke(KEY_F10,MOD_SHIFT_LEFT);')
                    digistr += "DigiKeyboard.sendKeyStroke(KEY_F10,MOD_SHIFT_LEFT);\n"
                else:  # 替换 按键
                    mod = ''
                    for key in SPECIAL_BUTTONS.keys():
                        if key in line:  # order in keys is important
                            line = line.replace(key, '')
                            mod += SPECIAL_BUTTONS.get(key) + ' | '
                    mod += '0'
                    line = line.replace(' ', '')
                    if line in BUTTONS.keys():
                        line = BUTTONS.get(line)
                    else:
                        line = '0'
                    digistr += "DigiKeyboard.sendKeyStroke(%s" % str(line)
                    digistr += "," + mod + ");\n"

            if len(line) < 1:
                print('', end='')
    digistr += "\n}\n\nvoid loop() {\n\n}"
    return digistr
