# this module creates the necesarry HAL pins 
# for atc control
# for QtDragon:
# put/rename the file in YOUR CONFIG/qtvcp/screens/qtdragon/qtdragon_handler.py
# or for qtdragon_hd:
# put/rename the file in YOUR CONFIG/qtvcp/screens/qtdragon_hd/qtdragon_hd_handler.py

import sys
import importlib
from qtvcp.widgets.simple_widgets import PushButton as Button
from PyQt5.QtWidgets import QSizePolicy
from qtvcp.core import Path, Qhal, Action, Status

from qtvcp.lib.qt_ngcgui.ngcgui import NgcGui
from qtvcp.lib.auto_height.auto_height import Auto_Measure

PATH = Path()
QHAL = Qhal()
ACTION = Action()
STATUS = Status()

# get reference to original handler file so we can subclass it
sys.path.insert(0, PATH.SCREENDIR)
module = "{}.{}_handler".format(PATH.BASEPATH,PATH.BASEPATH)
mod = importlib.import_module(module, PATH.SCREENDIR)
sys.path.remove(PATH.SCREENDIR)
HandlerClass = mod.HandlerClass

DEFAULT = 0
WARNING = 1
CRITICAL = 2

# return our subclassed handler object
def get_handlers(halcomp, widgets, paths):
    return [UserHandlerClass(halcomp, widgets, paths)]

class UserHandlerClass(HandlerClass):
    print('Custom subclassed handler loaded for local customizations\n')


    def init_widgets(self):
        # call the original function:
        super().init_widgets()

        # add and set up 3 HAL pushButtons:
        for i in('remap_tool_change',):
            self.w['btn_{}'.format(i)] = Button()
            self.w['btn_{}'.format(i)].setObjectName(i)
            self.w['btn_{}'.format(i)].setProperty('isManSensitive',True)
            self.w['btn_{}'.format(i)].setProperty('isOnSensitive',True)
            self.w['btn_{}'.format(i)].setProperty('isAllHomedSensitive',True)

            #self.w['btn_{}'.format(i)].setProperty('isSpindleOffSensitive',True)
            self.w['btn_{}'.format(i)].hal_init(HAL_NAME=i.replace('_','-'))
            self.w['btn_{}'.format(i)].setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
            self.w['btn_{}'.format(i)].setMinimumSize(66, 50)
            self.w.widget_tool_table.layout().addWidget(self.w['btn_{}'.format(i)] )

        # Set the button text
        self.w.btn_remap_tool_change.setText('TOOL\nCHANGE')
        self.w.btn_remap_tool_change.pressed.connect(lambda: self.change_selected_tool())

    def change_selected_tool(self):
        checked = self.w.tooloffsetview.get_checked_list()
        if len(checked) > 1:
            self.add_status("Select only 1 tool to load", WARNING)
        elif checked:
            self.add_status("Loaded tool {}".format(checked[0]))
            ACTION.CALL_MDI("M6 T{} G43".format(checked[0]))
        else:
            self.add_status("No tool selected", CRITICAL)

    def init_utils(self):
        # from qtvcp.lib.gcode_utility.facing2 import Facing          # <<<< modify this
        # self.facing = Facing()


        module_name = "Facing"
        file_path = "/home/cnc/linuxcnc/configs/QtDragon-6560/qtvcp/lib/gcode_utility/facing.py"
        spec = importlib.util.spec_from_file_location(module_name,file_path)
        module = importlib.util.module_from_spec(spec)
        sys.modules[module_name] = module
        spec.loader.exec_module(module)

        self.facing = module.Facing()
        self.w.layout_facing.addWidget(self.facing)

        from qtvcp.lib.gcode_utility.hole_circle import Hole_Circle
        self.hole_circle = Hole_Circle()
        self.w.layout_hole_circle.addWidget(self.hole_circle)

        try:
            from qtvcp.lib.gcode_utility.hole_enlarge import Hole_Enlarge
            self.hole_enlarge = Hole_Enlarge()
            ACTION.ADD_WIDGET_TO_TAB(self.w.tabWidget_utilities,self.hole_enlarge, 'Hole Enlarge')
        except Exception as e:
            print("Utility hole enlarge unavailable: {}".format(e))

        #LOG.info("Using NGCGUI utility")
        self.ngcgui = NgcGui()
        self.w.layout_ngcgui.addWidget(self.ngcgui)
        self.ngcgui.warp_info_frame(self.w.ngcGuiLeftLayout)

        self.auto_measure = Auto_Measure(self.w)
        self.w.layout_workpiece.addWidget(self.auto_measure)
        self.auto_measure._hal_init()
