# Path: pyilper/pilcharconv.py
# def icharconv(i, charset):
#    return convert_to_unicode[charset][i]
# 
# CHARSET_HP71=0
# 
# CHARSET_HP75=2
# 
# CHARSET_HP41=1
# 
# Path: pyilper/pilcore.py
# UPDATE_TIMER=25                 # Poll timer (ms) for terminal output queue
# 
# CURSOR_BLINK=500                # 500 ms cursor blink rate
# 
# TERMINAL_MINIMUM_ROWS=24        # can't get beyond that
# 
# FONT="Monospace"
# 
# AUTOSCROLL_RATE=100             # 500 ms cursor blink rate
# 
# def isMACOS():
#    return platform.system()=="Darwin"
# 
# KEYBOARD_DELAY=500              # autorepeat delay to prevent too fast kbd input
# 
# Path: pyilper/shortcutconfig.py
# SHORTCUTCONFIG=  cls_shortcutconfig()
# 
# SHORTCUT_EXEC=1
# 
# SHORTCUT_EDIT=2
# 
# SHORTCUT_INSERT=3
# 
# Path: pyilper/pilconfig.py
# PILCONFIG=  cls_pilconfig()


import array
import queue
import threading
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from .pilcharconv import icharconv, CHARSET_HP71, CHARSET_HP75, CHARSET_HP41
from .pilcore import UPDATE_TIMER, CURSOR_BLINK, TERMINAL_MINIMUM_ROWS,FONT, AUTOSCROLL_RATE, isMACOS, KEYBOARD_DELAY
from .shortcutconfig import SHORTCUTCONFIG, SHORTCUT_EXEC, SHORTCUT_EDIT, SHORTCUT_INSERT
from .pilconfig import PILCONFIG
from .pilkeymap import *

#
# Terminal backend class -----------------------------------------------------------
#
# The terminal backend class maintains the terminal line buffer. The terminal
# line buffer content is arranged by the input of text and escape sequences that
# control editing and and the cursor position.
# The backend class also maintains the boundaries of the visible part of the
# line buffer that is rendered by the frontend widget and sets the scrollbar parameters.
#
class HPTerminal:

    def __init__(self, win, name):
        self.name=name                    # name of tab
        self.w = -1                       # terminal/buffer width (characters)
        self.h =  -1                      # buffer size (lines)
                                          # both variables are initialized in
                                          # reconfigure
        self.actual_h=0                   # number of lines in the buffer
        self.fesc= False                  # status indicator for parsing esc 
                                          # sequences
        self.movecursor= 0                # status indicator for parsing the
                                          # move cursor escape sequence
        self.movecol=0                    # same as above
        self.win=win                      # terminal widget
        self.view_h= 0                    # terminal size (lines)
                                          # initialized if window was resized
        self.view_y0=0                    # bottom of buffer view
        self.view_y1=0                    # top of buffer view
        self.blink_counter=0              # counter that controls cursor blink

# Path: pyilper/pilcharconv.py
# def icharconv(i, charset):
#    return convert_to_unicode[charset][i]
# 
# CHARSET_HP71=0
# 
# CHARSET_HP75=2
# 
# CHARSET_HP41=1
# 
# Path: pyilper/pilcore.py
# UPDATE_TIMER=25                 # Poll timer (ms) for terminal output queue
# 
# CURSOR_BLINK=500                # 500 ms cursor blink rate
# 
# TERMINAL_MINIMUM_ROWS=24        # can't get beyond that
# 
# FONT="Monospace"
# 
# AUTOSCROLL_RATE=100             # 500 ms cursor blink rate
# 
# def isMACOS():
#    return platform.system()=="Darwin"
# 
# KEYBOARD_DELAY=500              # autorepeat delay to prevent too fast kbd input
# 
# Path: pyilper/shortcutconfig.py
# SHORTCUTCONFIG=  cls_shortcutconfig()
# 
# SHORTCUT_EXEC=1
# 
# SHORTCUT_EDIT=2
# 
# SHORTCUT_INSERT=3
# 
# Path: pyilper/pilconfig.py
# PILCONFIG=  cls_pilconfig()


# Path: pyilper/pilqterm.py
import array
import queue
import threading
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from .pilcharconv import icharconv, CHARSET_HP71, CHARSET_HP75, CHARSET_HP41
from .pilcore import UPDATE_TIMER, CURSOR_BLINK, TERMINAL_MINIMUM_ROWS,FONT, AUTOSCROLL_RATE, isMACOS, KEYBOARD_DELAY
from .shortcutconfig import SHORTCUTCONFIG, SHORTCUT_EXEC, SHORTCUT_EDIT, SHORTCUT_INSERT
from .pilconfig import PILCONFIG
from .pilkeymap import *


#
# Terminal backend class -----------------------------------------------------------
#
# The terminal backend class maintains the terminal line buffer. The terminal
# line buffer content is arranged by the input of text and escape sequences that
# control editing and and the cursor position.
# The backend class also maintains the boundaries of the visible part of the
# line buffer that is rendered by the frontend widget and sets the scrollbar parameters.
#
class HPTerminal:

    def __init__(self, win, name):
        self.name=name                    # name of tab
        self.w = -1                       # terminal/buffer width (characters)
        self.h =  -1                      # buffer size (lines)
                                          # both variables are initialized in
                                          # reconfigure
        self.actual_h=0                   # number of lines in the buffer
        self.fesc= False                  # status indicator for parsing esc 
                                          # sequences
        self.movecursor= 0                # status indicator for parsing the
                                          # move cursor escape sequence
        self.movecol=0                    # same as above
        self.win=win                      # terminal widget
        self.view_h= 0                    # terminal size (lines)
                                          # initialized if window was resized
        self.view_y0=0                    # bottom of buffer view
        self.view_y1=0                    # top of buffer view
        self.blink_counter=0              # counter that controls cursor blink

        self.charset=CHARSET_HP71         # character set used for output