import os
import json

class UserControls:
    def __init__(self):
        print("UserControls init")
        self.configs = self.get_config()
        
    def get_config(self):
        configs = {}
        for file in os.listdir("conf"):
            if file.endswith(".json"):
                with open(os.path.join("conf", file)) as f:
                    configs[file[:-5]] = json.load(f)
        return configs
    def handle_events(self, mapping, event):
        if event.startswith("MOUSEMOTION"):
            x, y = event.split(":")[1:]
            self.set_cursor(int(x), int(y))
        if method := self.configs[mapping].get(event):
            getattr(self, method)()

            
    def scroll_up(self):
        self.graphics.view_offset_y += 10
        self.graphics.create_background(self.map, "tiles")
        
    def scroll_down(self):
        self.graphics.view_offset_y -= 10
        self.graphics.create_background(self.map, "tiles")
        
    def scroll_left(self):
        self.graphics.view_offset_x += 10
        self.graphics.create_background(self.map, "tiles")
        
    def scroll_right(self):
        self.graphics.view_offset_x -= 10
        self.graphics.create_background(self.map, "tiles")