"""
WORKAH0L1C helps you to communicate your engagement & professionalism
by sending good vibrations of hard work to your desktop pointer device
repeatedly, to keep things (i.e. online activity status indicators) green.

THIS CODE BORROWED SOME LINES OF CODE FROM APPS CREATING SOUND WITH VIBRATIONS.
FEEL FREE TO UNDERLINE YOUR WORKAHOL1C LIFE ON CORPORATE WORKER SURVEILLANCE APPLIANCES (CWSA)
LIKE e.g. SLACK, TEAMS, SKYPE, ZOOM, etc. BY SIMULATING POINTER DEVICE MOVEMENTS
IN AN UNPREDICTABLE MANNER TO AVOID SCREENSAVERS & CWSA TO SEND THE WRONG MESSAGE
TO YOUR COMPANY's BOSS. BUT KEEP IN MIND, THIS IS JUST A SILLY APP NO SOLUTION.

CODE IS FOUND ON:
https://badge.team/badge/card10
"""

import buttons
import display
import leds
import utime
import vibra

# CONSTS
black = [0, 0, 0]
yellow = [255, 255, 0]
red = [255, 0, 50]
blue = [0, 80, 255]
violet = [128, 0, 255]
green = [0, 255, 50]
orange = [255, 155, 0]
white = [255, 255, 255]
lightgray = [150, 150, 150]

# SETUP STUFF
disp = display.open()
workmode_sleeptimes = ( 300, 180, 60, 30, 15, 5 )
workmode = 0
workmode_sleep_time = workmode_sleeptimes[ workmode ]
time_last_worked = utime.monotonic() - (workmode_sleep_time-1)

def do_some_hard_work():
    global workmode, workmode_sleep_time, time_last_worked
    vibra_count = 0
    vib_duration = 150
    pause_duration = 400
    while vibra_count < 6:
        if ( vibra_count % 2 ) == 0:
            vibra.vibrate( vib_duration )
            # FLASH ORANGE ROCKET FOR EACH VIBRATION
            leds.flash_rocket( 1, 31, vib_duration )
        else:
            utime.sleep_ms( pause_duration )
        vibra_count += 1
    time_last_worked = utime.monotonic()

# MAIN
button_pressed_recent = 0
waituntil = utime.monotonic_ms()
waitdisplayrefresh = utime.monotonic_ms()
elapsedseconds = 0
timebuttonpressed = utime.monotonic()
workmode_titles = ( 'SLOTH', 'GROUNDHOG', 'TURTLE','RABBIT','OCTOPUS','SQUIRREL' )
workmode_names = ( ' 300 SECS ', ' 180 SECS ', ' 60 SECS ',' 30 SECS ',' 15 SECS ','  5 SECS  ' )
workmode_colors = ( green, yellow, orange, red, blue, violet )

while True:
    # CHECK BUTTON AFTER 200ms UNPRELL
    if waituntil < utime.monotonic_ms():
        button_pressed_now = buttons.read()
        button_pressed = ~button_pressed_recent & button_pressed_now
        button_pressed_recent = button_pressed_now

        if button_pressed:
            # FLASH GREEN ROCKET ON ANY BUTTON PRESSED
            leds.flash_rocket( 2, 31, 100 )
            timebuttonpressed = utime.monotonic()

        if button_pressed & buttons.TOP_RIGHT:
            workmode += 1
            waituntil = utime.monotonic_ms()+200

            if workmode > 5:
                workmode = 0
            workmode_sleep_time = workmode_sleeptimes[ workmode ]

    seconds_since_lastpress = utime.monotonic() - timebuttonpressed
    if seconds_since_lastpress <= 30: # NORMAL BACKLIGHT
        disp.backlight( 80 )
    elif seconds_since_lastpress > 30 and seconds_since_lastpress <= 60: # LOWEST BACKLIGHT
        disp.backlight( 1 )
    else: # BACKLIGHT COMPLETELY OFF
        disp.backlight( 0 )

    # FLASH BLUE ROCKET EVERY SECOND
    seconds_since_work = utime.monotonic() - time_last_worked
    if elapsedseconds < seconds_since_work:
        elapsedseconds = seconds_since_work
        leds.flash_rocket( 0, 31, 20 )

    if time_last_worked + workmode_sleep_time <= utime.monotonic():
        elapsedseconds = 0
        do_some_hard_work()
    
    workmode_name = str( workmode_names[ workmode ] )
    workmode_title = str( workmode_titles[ workmode ] )
    
    workmode_color = workmode_colors[ workmode ]

    # DISPLAY REFRESH MAX ALL 200 ms, MORE IS OVERKILL
    if waitdisplayrefresh < utime.monotonic_ms()+200 and seconds_since_lastpress <= 60:
        disp.clear( col = black )
        disp.print( workmode_title, fg=lightgray, bg=black, posx=80 - round(len( workmode_title) / 2 * 14), posy=5)
        disp.print( "WORKAHOL1C", fg=white, bg=black, posx=80 - round(len( "WORKAH0L1C" ) / 2 * 14), posy=28)
        disp.print( workmode_name, fg=black, bg=workmode_color, posx=80 - round(len( workmode_name ) / 2 * 14), posy=54)
        disp.update()

    utime.sleep_ms( 50 )

"""
Sure this is a silly approach, but it was fun to code. You can find
nice solutions with other approaches here:
- Basic UNIX way of having workaholic fun: https://www.xmodulo.com/simulate-key-press-mouse-movement-linux.html
- PicoPI: https://hridaybarot.home.blog/2021/01/31/using-raspberry-pi-pico-has-hid-device-to-control-mouse-and-keyboard/
- KeepPresence: https://github.com/carrot69/keep-presence (all platforms running Python)
- Drinking Typing Bird: [1] https://www.youtube.com/watch?v=R_rF4kcqLkI [2] https://www.youtube.com/watch?v=iapECJKx4k0
- $addYourFavouriteCWSAdefeatingDevice
"""