from collections import deque
import time
import logging

from arrange_base import ArrangeBase



class ArrangeMoveMonitor(ArrangeBase):
    """ Class to move a window to another monitor """

    def do(self, direction):
        """ Main function that performs the arrangement """
        
        window_original_geometry = self._screen.get_active_window_geometry()

        old_monitor = self._screen.get_active_window_monitor()
        old_monitor_geometry = self._screen.get_monitor_geometry(old_monitor) 
        
        logging.debug("Window currently on monitor {}".format(old_monitor))

        new_monitor = old_monitor

        if direction == "left":
            if old_monitor > 0:
                new_monitor = old_monitor - 1
        else:
            if old_monitor < self._screen.get_number_monitors():
                new_monitor = old_monitor + 1
        
        # Do we need to move the window?
        if new_monitor != old_monitor:

            new_monitor_geometry = self._screen.get_monitor_geometry(new_monitor) 

            # TODO deal with if the window is now too large for the new monitor
            new_position = [
                new_monitor_geometry.x + (window_original_geometry[0] - old_monitor_geometry.x),
                new_monitor_geometry.y + (window_original_geometry[1] - old_monitor_geometry.y),
                window_original_geometry[2], 
                window_original_geometry[3], 
            ]
            
            # Now move the window
            self._screen.move_active_window(new_position)


            


@@ -273,33 +273,32 @@
 ment %22%22%22%0A       
- 
 %0A        window_
@@ -294,76 +294,110 @@
     
-window_original_geometry = self._screen.get_active_window_geometry()
+# No action if only one monitor%0A        if self._screen.get_number_monitors() == 1:%0A            return
 %0A%0A  
@@ -596,16 +596,28 @@
 monitor 
+%7B%7D.  Moving 
 %7B%7D%22.form
@@ -630,16 +630,27 @@
 _monitor
+, direction
 ))%0A%0A    
@@ -680,16 +680,67 @@
 onitor%0A%0A
+        # Work out if we have a new monitor number%0A
         
@@ -918,16 +918,20 @@
 nitors()
+ - 1
 :%0A      
@@ -970,24 +970,16 @@
 tor + 1%0A
-        
 %0A       
@@ -1051,16 +1051,176 @@
 onitor:%0A
+        %0A            logging.debug(%22Moving to monitor %7B%7D%22.format(new_monitor))%0A%0A            window_original_geometry = self._screen.get_active_window_geometry()
 %0A       

Fix bug to prevent looping of screens