# Path: lib/windowhandling.py
# KEYBOARD = PyKeyboard()
# PYKB_KEYS_DOWN = set()
# def sendkeys(win_id, up=None, press=None, down=None):
# def sendkeys_pykb(up=None, tap=None, down=None):
# def xwininfo(win_id):
# def get_window_coordinates(win_id):
# def find_window_ids(needle_name):
# def get_window_name(win_id):
# def get_active_window_id():
# def activate_window(win_id):
# 
# Path: lib/ets2window.py
# class ETS2Window(object):
#     def __init__(self, win_id, coordinates=None):
#     def is_activated(self):
#     def get_coordinates(self):
#     def get_image(self):
#     def get_speed_image(self, scr=None):
#     def get_route_advisor_image(self, scr=None):
#     def is_route_advisor_visible(self, scr, threshold=2):
#     def is_paused(self, scr, threshold=4):
#     def is_offence_shown(self, scr, threshold=0.97):
#     def is_damage_shown(self, scr, threshold=2):
#     def is_reverse(self, scr, threshold=2):
#     def keys(self, up=None, press=None, down=None):
# def find_ets2_window_id():
# def get_speed_image_coords(scr, mode="extended", border=(2, 2, 2, 2)):
# def get_route_advisor_image_coords(scr):
# def get_route_advisor_image(scr):
# def get_speed_image(scr):
# 
# Path: lib/ets2game.py
# class ETS2Game(object):
#     def __init__(self, win=None):
#     def run(self):
#     def reset_actions(self):
#     def set_actions_of_interval(self, actions):
#     def _actions_tick(self):
#     def pause(self):
#     def unpause(self):
#     def load_random_save_game(self):
# 
# Path: lib/screenshot.py
# CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
# LIB_NAME = "screenshot_c.so" # naming this just "screenshot.so" causes problem for "import screenshot" (seems to prefer the .so over .py?)
# ABS_LIB_PATH = os.path.join(CURRENT_DIR, LIB_NAME)
# SCREENSHOT_LIB = ctypes.CDLL(ABS_LIB_PATH)
# def make_screenshot(x1, y1, x2, y2):
# 
# Path: lib/speed.py
# class SpeedSegmentsDatabase(object):
# class SpeedOMeter(object):
# class SpeedSegment(object):
#     def __init__(self, filepath=Config.SPEED_IMAGE_SEGMENTS_DB_FILEPATH):
#     def get_instance():
#     def load_dict_from_file(filepath):
#     def save(self):
#     def add(self, segment, save=True, force=False):
#     def get_by_key(self, key):
#     def contains_key(self, key):
#     def __init__(self, image):
#     def get_postprocessed_image(self):
#     def get_postprocessed_image_rgb(self):
#     def split_to_segments(self):
#     def predict_speed_raw(self):
#     def predict_speed(self):
#     def postprocess_speed_image(speed_image):
#     def segment_speed_image(speed_image_bin):
#     def __init__(self, arr, label=None):
#     def get_image(self):
#     def predict_label(self):
#     def is_in_database(self):
#     def get_key(self):
#     def to_string(self):
#     def from_string(line):
#     def segment_image_to_key(arr):


import sys
import os
import time
import cv2
import numpy as np
import os
from lib import windowhandling
from lib import ets2window
from lib import ets2game
from lib import screenshot
from lib import speed as speedlib
from __future__ import print_function, division

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))



def main():
    cv2.namedWindow("speed", cv2.WINDOW_NORMAL)
    cv2.namedWindow("speed_bin", cv2.WINDOW_NORMAL)

    def on_route_advisor_visible(game, scr):
        speed_image = game.win.get_speed_image(scr)
        som = speedlib.SpeedOMeter(speed_image)

        speed_image_bin = som.get_postprocessed_image()

        cv2.imshow("speed", speed_image)
        cv2.imshow("speed_bin", speed_image_bin.astype(np.uint8)*255)
        cv2.waitKey(1)

        segments = som.split_to_segments()
        print("Found %d segments" % (len(segments),), [s.arr.shape for s in segments])

        for seg in segments:
            if not seg.is_in_database():

# Path: lib/windowhandling.py
# KEYBOARD = PyKeyboard()
# PYKB_KEYS_DOWN = set()
# def sendkeys(win_id, up=None, press=None, down=None):
# def sendkeys_pykb(up=None, tap=None, down=None):
# def xwininfo(win_id):
# def get_window_coordinates(win_id):
# def find_window_ids(needle_name):
# def get_window_name(win_id):
# def get_active_window_id():
# def activate_window(win_id):
# 
# Path: lib/ets2window.py
# class ETS2Window(object):
#     def __init__(self, win_id, coordinates=None):
#     def is_activated(self):
#     def get_coordinates(self):
#     def get_image(self):
#     def get_speed_image(self, scr=None):
#     def get_route_advisor_image(self, scr=None):
#     def is_route_advisor_visible(self, scr, threshold=2):
#     def is_paused(self, scr, threshold=4):
#     def is_offence_shown(self, scr, threshold=0.97):
#     def is_damage_shown(self, scr, threshold=2):
#     def is_reverse(self, scr, threshold=2):
#     def keys(self, up=None, press=None, down=None):
# def find_ets2_window_id():
# def get_speed_image_coords(scr, mode="extended", border=(2, 2, 2, 2)):
# def get_route_advisor_image_coords(scr):
# def get_route_advisor_image(scr):
# def get_speed_image(scr):
# 
# Path: lib/ets2game.py
# class ETS2Game(object):
#     def __init__(self, win=None):
#     def run(self):
#     def reset_actions(self):
#     def set_actions_of_interval(self, actions):
#     def _actions_tick(self):
#     def pause(self):
#     def unpause(self):
#     def load_random_save_game(self):
# 
# Path: lib/screenshot.py
# CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
# LIB_NAME = "screenshot_c.so" # naming this just "screenshot.so" causes problem for "import screenshot" (seems to prefer the .so over .py?)
# ABS_LIB_PATH = os.path.join(CURRENT_DIR, LIB_NAME)
# SCREENSHOT_LIB = ctypes.CDLL(ABS_LIB_PATH)
# def make_screenshot(x1, y1, x2, y2):
# 
# Path: lib/speed.py
# class SpeedSegmentsDatabase(object):
# class SpeedOMeter(object):
# class SpeedSegment(object):
#     def __init__(self, filepath=Config.SPEED_IMAGE_SEGMENTS_DB_FILEPATH):
#     def get_instance():
#     def load_dict_from_file(filepath):
#     def save(self):
#     def add(self, segment, save=True, force=False):
#     def get_by_key(self, key):
#     def contains_key(self, key):
#     def __init__(self, image):
#     def get_postprocessed_image(self):
#     def get_postprocessed_image_rgb(self):
#     def split_to_segments(self):
#     def predict_speed_raw(self):
#     def predict_speed(self):
#     def postprocess_speed_image(speed_image):
#     def segment_speed_image(speed_image_bin):
#     def __init__(self, arr, label=None):
#     def get_image(self):
#     def predict_label(self):
#     def is_in_database(self):
#     def get_key(self):
#     def to_string(self):
#     def from_string(line):
#     def segment_image_to_key(arr):


# Path: scripts/collect_speed_segments.py
import sys
import os
import time
import cv2
import numpy as np
import os
from lib import windowhandling
from lib import ets2window
from lib import ets2game
from lib import screenshot
from lib import speed as speedlib

from __future__ import print_function, division

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))



def main():
    cv2.namedWindow("speed", cv2.WINDOW_NORMAL)
    cv2.namedWindow("speed_bin", cv2.WINDOW_NORMAL)

    def on_route_advisor_visible(game, scr):
        speed_image = game.win.get_speed_image(scr)
        som = speedlib.SpeedOMeter(speed_image)

        speed_image_bin = som.get_postprocessed_image()

        cv2.imshow("speed", speed_image)
        cv2.imshow("speed_bin", speed_image_bin.astype(np.uint8)*255)
        cv2.waitKey(1)

        segments = som.split_to_segments()
        print("Found %d segments" % (len(segments),), [s.arr.shape for s in segments])

        for seg in segments:
            if not seg.is_in_database():

                speedlib.SpeedSegmentsDatabase.get_instance().add(seg)