"""MainMenuState-related input and event handlers."""

from xoinvader import application
from xoinvader.gui import (
    MenuItemContainer,
    MenuItemWidget,
    PopUpNotificationWidget,
    TextCallbackWidget,
    TextWidget,
)
from xoinvader.handlers import EventHandler
from xoinvader.keys import KEY
from xoinvader.state import State
from xoinvader.utils import Point


class PauseMenuState(State):

    def __init__(self, owner):
        super().__init__(owner)
        self._screen = owner.screen

        self.add(TextWidget(Point(4, 4), "Pause"))
        self._items = MenuItemContainer([
            MenuItemWidget(
                Point(10, 10), "Continue",
                template=("=> ", ""),
                action=lambda: application.get_current().trigger_state("InGameState")),
            MenuItemWidget(
                Point(10, 11), "Quit",
                template=("=> ", ""),
                action=application.get_current().stop),
        ])
        self._items.select(0)
        self.add(self._items)

        self._events = EventHandler(self, {
            KEY.ESCAPE: lambda: application.get_current().trigger_state("InGameState"),
            KEY.SPACE: self._items.do_action,
            KEY.W: self._items.prev,
            KEY.S: self._items.next,
            KEY.N: lambda: self.notify("This is test notification"),
        })

    def notify(self, text, pos=Point(15, 15)):
        self.add(
            PopUpNotificationWidget(
                pos, text,
                callback=lambda w: self.remove(w)))

    def events(self):
        self._events.handle()


class GameOverState(State):

    def __init__(self, owner):
        super().__init__(owner)
        self._screen = owner.screen

        self._score = "Your score: {0}"

        self._objects = [
            TextWidget(Point(4, 4), "Your soul completely lost this time"),
            TextCallbackWidget(Point(4, 5), self.score_callback),
            MenuItemWidget(Point(10, 10), "I agree"),
            MenuItemWidget(Point(10, 11), "No, I want more")
        ]
        self._objects[2].select()
        self._events = EventHandler(self, {
            KEY.ESCAPE: application.get_current().stop,
            # KEY.ENTER: exit_game_command,
        })

    def score_callback(self):
        return self._score

    def trigger(self, score):
        """Trigger the state and pass the score info to it.

        :param int score: last player score
        """

        self._score = self._score.format(score)

    def events(self):
        self._events.handle()

@@ -1778,96 +1778,81 @@
 elf.
-_objects = %5B%0A            TextWidget(Point(4, 4), %22Your soul completely lost this time%22),
+add(TextWidget(Point(4, 4), %22I was too tired and scared to continue...%22))
 %0A   
@@ -1856,20 +1856,25 @@
         
-    
+self.add(
 TextCall
@@ -1917,17 +1917,60 @@
 allback)
-,
+)%0A%0A        self._items = MenuItemContainer(%5B
 %0A       
@@ -1981,32 +1981,49 @@
  MenuItemWidget(
+%0A                
 Point(10, 10), %22
@@ -2028,14 +2028,125 @@
  %22I 
-agree%22
+must to continue my revenge!%22,%0A                action=lambda: application.get_current().trigger_reinit(%22InGameState%22)
 ),%0A 
@@ -2167,24 +2167,41 @@
 uItemWidget(
+%0A                
 Point(10, 11
@@ -2210,17 +2210,17 @@
  %22No
-,
+!
  I want 
 more
@@ -2219,14 +2219,77 @@
 ant 
-more%22)
+to go home!%22,%0A                action=application.get_current().stop),
 %0A   
@@ -2294,16 +2294,17 @@
        %5D
+)
 %0A       
@@ -2313,27 +2313,52 @@
 elf.
-_objects%5B2%5D
+add(self._items)%0A        self._items
 .select(
 )%0A  
@@ -2353,17 +2353,19 @@
 .select(
-)
+0)%0A
 %0A       
@@ -2421,46 +2421,73 @@
 KEY.
-ESCAPE: application.get_current().stop
+SPACE: self._items.do_action,%0A            KEY.W: self._items.prev
 ,%0A  
@@ -2499,39 +2499,32 @@
     
- #
  KEY.
-ENTER: exit_game_command
+S: self._items.next
 ,%0A  

use MenuItemContainer in GameOverState