# Path: linux_story/PlayerLocation.py
# def generate_real_path(fake_path):
#     return fake_path.replace('~', fake_home_dir)
# 
# Path: linux_story/commands_real.py
# def nano(real_path, line):
#     '''
#     Runs the linux-story version of nano as a separate process,
#     and prints out any resulting errors.
# 
#     Args:
#         real_path (str): the current location of the user.
#         line (str): the command entered by the user.
# 
#     Returns:
#         None
#     '''
# 
#     # File path of the local nano
#     dir_path = os.path.abspath(os.path.dirname(__file__))
#     nano_filepath = os.path.join(dir_path, "..", "nano-2.2.6/src/nano")
# 
#     if not os.path.exists(nano_filepath):
#         # File path of installed nano
#         nano_filepath = "/usr/share/linux-story/nano"
# 
#     if not os.path.exists(nano_filepath):
#         raise Exception("Cannot find nano")
# 
#     # notifying the SoundManager about the command that is about run
#     sounds_manager.on_command_run(['nano'] + line.split())
# 
#     # Unsetting the LINES and COLUMNS variables because the
#     # hack in TerminalUi.py which sets the size to 1000x1000 is disturbing nano
#     # (https://github.com/KanoComputing/terminal-quest/commit/dd45b447363e3fdcebf80000dbbfd920b96637db)
#     cmd = 'LINES= COLUMNS= ' + nano_filepath + " " + line
#     p = subprocess.Popen(cmd, cwd=real_path, shell=True)
#     stdout, stderr = p.communicate()
# 
#     if stdout:
#         print stdout.strip()
# 
#     if stderr:
#         print stderr.strip()
# 
# Path: linux_story/story/terminals/terminal_mkdir.py
# class TerminalMkdir(TerminalEcho):
#     terminal_commands = ["ls", "cat", "cd", "mv", "echo", "mkdir"]
# 
#     def do_mkdir(self, line):
#         shell_command(self._location.get_real_path(), line, "mkdir")


import os
import threading
from kano.logging import logger
from linux_story.PlayerLocation import generate_real_path
from linux_story.commands_real import nano
from linux_story.story.terminals.terminal_mkdir import TerminalMkdir
class TerminalNano(TerminalMkdir):
    terminal_commands = ["ls", "cat", "cd", "mv", "echo", "mkdir", "nano"]

    def __init__(self, step, location, dirs_to_attempt, client):
        self._step_nano = step.get_nano_logic()

        TerminalMkdir.__init__(self, step, location, dirs_to_attempt, client)

    def do_nano(self, line):

        ##########################################
        # set up step_nano
        self._step_nano.set_nano_running(True)
        self._step_nano.opened_nano(line)

        # Read contents of the file
        text = self.read_goal_contents()
        self._step_nano.set_nano_content(text)
        ##########################################

        # Read nano in a separate thread
        t = threading.Thread(target=self.try_and_get_pipe_contents)
        t.daemon = True
        t.start()

        # If the line is given, it is the filepath of the file we are
        # adjusting.
        # if line:
        #    self.set_last_nano_filepath(line)


# Path: linux_story/PlayerLocation.py
# def generate_real_path(fake_path):
#     return fake_path.replace('~', fake_home_dir)
# 
# Path: linux_story/commands_real.py
# def nano(real_path, line):
#     '''
#     Runs the linux-story version of nano as a separate process,
#     and prints out any resulting errors.
# 
#     Args:
#         real_path (str): the current location of the user.
#         line (str): the command entered by the user.
# 
#     Returns:
#         None
#     '''
# 
#     # File path of the local nano
#     dir_path = os.path.abspath(os.path.dirname(__file__))
#     nano_filepath = os.path.join(dir_path, "..", "nano-2.2.6/src/nano")
# 
#     if not os.path.exists(nano_filepath):
#         # File path of installed nano
#         nano_filepath = "/usr/share/linux-story/nano"
# 
#     if not os.path.exists(nano_filepath):
#         raise Exception("Cannot find nano")
# 
#     # notifying the SoundManager about the command that is about run
#     sounds_manager.on_command_run(['nano'] + line.split())
# 
#     # Unsetting the LINES and COLUMNS variables because the
#     # hack in TerminalUi.py which sets the size to 1000x1000 is disturbing nano
#     # (https://github.com/KanoComputing/terminal-quest/commit/dd45b447363e3fdcebf80000dbbfd920b96637db)
#     cmd = 'LINES= COLUMNS= ' + nano_filepath + " " + line
#     p = subprocess.Popen(cmd, cwd=real_path, shell=True)
#     stdout, stderr = p.communicate()
# 
#     if stdout:
#         print stdout.strip()
# 
#     if stderr:
#         print stderr.strip()
# 
# Path: linux_story/story/terminals/terminal_mkdir.py
# class TerminalMkdir(TerminalEcho):
#     terminal_commands = ["ls", "cat", "cd", "mv", "echo", "mkdir"]
# 
#     def do_mkdir(self, line):
#         shell_command(self._location.get_real_path(), line, "mkdir")


# Path: linux_story/story/terminals/terminal_nano.py
import os
import threading
from kano.logging import logger
from linux_story.PlayerLocation import generate_real_path
from linux_story.commands_real import nano
from linux_story.story.terminals.terminal_mkdir import TerminalMkdir

class TerminalNano(TerminalMkdir):
    terminal_commands = ["ls", "cat", "cd", "mv", "echo", "mkdir", "nano"]

    def __init__(self, step, location, dirs_to_attempt, client):
        self._step_nano = step.get_nano_logic()

        TerminalMkdir.__init__(self, step, location, dirs_to_attempt, client)

    def do_nano(self, line):

        ##########################################
        # set up step_nano
        self._step_nano.set_nano_running(True)
        self._step_nano.opened_nano(line)

        # Read contents of the file
        text = self.read_goal_contents()
        self._step_nano.set_nano_content(text)
        ##########################################

        # Read nano in a separate thread
        t = threading.Thread(target=self.try_and_get_pipe_contents)
        t.daemon = True
        t.start()

        # If the line is given, it is the filepath of the file we are
        # adjusting.
        # if line:
        #    self.set_last_nano_filepath(line)


        nano(self._location.get_real_path(), line)