#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.26
#  in conjunction with Tcl version 8.6
#    Dec 07, 2019 07:47:05 PM CET  platform: Windows NT

import sys
import tkinter

from ICO3Plugin.Plugin.ICO3FramePlugin import ICO3FramePlugin
from ICO3Utilities.Debug.LogDebug import ICO3Log

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True



def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    top = MinFramePlugin (root)
    #MinFramePlugin_support.init(root, top)
    root.mainloop()

# w = None
# def create_MinFramePlugin(root, *args, **kwargs):
#     '''Starting point when module is imported by another program.'''
#     global w, w_win, rt
#     rt = root
#     w = tk.Toplevel (root)
#     top = MinFramePlugin (w)
#    # MinFramePlugin_support.init(w, top, *args, **kwargs)
#     return (w, top)
#
# def destroy_MinFramePlugin():
#     global w
#     w.destroy()
#     w = None

class MinFramePlugin(ICO3FramePlugin):
    theFrame = None
    theMainframe = None

    theSenderHeader = None
    theReceiverPort = None

    theTaskEvent = None

    def on_closing(self):           # Avoid Frame Close
        # ICO3Log.print("Plugin","Close Application")
        # self.removeMe()
        # sys.exit(0)
        pass

    def __init__(self, xMainFrame=None, XY = None):

        if xMainFrame is not None:
            self.theMainframe = xMainFrame
        if self.theMainframe is None:
            return
        XYPos = "110,110"
        if XY != None:
            XYPos  = XY
            pass
        xGeometry = "430x153" + self.getXYExtention(XYPos)
        ICO3Log.print("Plugin","Geometrie MinFrame : " + xGeometry)

        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'

        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        self.theFrame = tk.Toplevel(self.theMainframe)

        self.theFrame.geometry(xGeometry)
        self.theFrame.minsize(148, 1)
        self.theFrame.maxsize(2564, 1415)
        self.theFrame.resizable(1, 1)
        self.theFrame.title("New Toplevel")
        self.theFrame.configure(background="#d9d9d9")

        self.theFrame.protocol("WM_DELETE_WINDOW", self.on_closing) # Necessary against inadvertent Frame close

        self.BTSend = tk.Button(self.theFrame)
        self.BTSend.place(relx=0.814, rely=0.131, height=33, width=45)
        self.BTSend.configure(activebackground="#ececec")
        self.BTSend.configure(activeforeground="#000000")
        self.BTSend.configure(background="#d9d9d9")
        self.BTSend.configure(disabledforeground="#a3a3a3")
        self.BTSend.configure(foreground="#000000")
        self.BTSend.configure(highlightbackground="#d9d9d9")
        self.BTSend.configure(highlightcolor="black")
        self.BTSend.configure(pady="0")
        self.BTSend.configure(text='''Send''')
        self.BTSend.configure(command=self.SendtheMessage)

        self.TextToSend = tk.Entry(self.theFrame)
        self.TextToSend.place(relx=0.07, rely=0.131,height=24, relwidth=0.684)
        self.TextToSend.configure(background="white")
        self.TextToSend.configure(disabledforeground="#a3a3a3")
        self.TextToSend.configure(font="TkFixedFont")
        self.TextToSend.configure(foreground="#000000")
        self.TextToSend.configure(insertbackground="black")

        self.TextReceived = tk.Text(self.theFrame)
        self.TextReceived.place(relx=0.256, rely=0.458, relheight=0.418
                , relwidth=0.684)
        self.TextReceived.configure(background="white")
        self.TextReceived.configure(font="TkTextFont")
        self.TextReceived.configure(foreground="black")
        self.TextReceived.configure(highlightbackground="#d9d9d9")
        self.TextReceived.configure(highlightcolor="black")
        self.TextReceived.configure(insertbackground="black")
        self.TextReceived.configure(selectbackground="#c4c4c4")
        self.TextReceived.configure(selectforeground="black")
        self.TextReceived.configure(wrap="word")
        self.TextReceived.configure(state="disable")

        self.Label1 = tk.Label(self.theFrame)
        self.Label1.place(relx=0.07, rely=0.458, height=26, width=57)
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(text="Receive")



    def SendtheMessage(self):
        TextInput = self.TextToSend.get()

                # Create Message from destination Header template and the Text
                # Send Message and Check Header Address
                # If result = 1 is OK
        result = self.sendMessageToTarget("TargetTest", TextInput)
       # result = self.createSendMessageCheck(self.theSenderHeader, TextInput)

        self.TextToSend.delete(0, 'end')
        pass


        # Receive a message from ICOCUBE via an event
    def callBackReceiveMessage(self, xMessage):
            # Get the payload message as String
        TextRec = xMessage.getPayloadString()

            # Display It
        self.TextReceived.configure(state="normal")
        self.TextReceived.delete('1.0', tkinter.END)
        self.TextReceived.insert('1.0', TextRec)
        self.TextReceived.configure(state="disable")

        return 1        # return Status 1 is OK


        # Init the plugin with Argvs as String.
    def initPlugin(self, Argvs):
            # get Task parameter with the Port will use to receiver port
            # and Target to specify the target to send message
            # getTaskParameterFlat get target Address after internal Processing and checking.

        # xTargetTest = self.getTaskParameterFlat("TargetTest")
        #
        # if xTargetTest is not None:
        #     self.theReceiverPort = xTargetTest.Port         # Store Port for next usage
        #     if xTargetTest.Target is not None:
        #                 #  Process Target address with Node Directory if necessary
        #                 #  and Create MessageHeader Template to send Data
        #         self.theSenderHeader = self.createMessageHeader(xTargetTest.Target)

        self.theFrame.title(Argvs + " (" + str(self.theModuleMaster.ModuleID) + ")")       # use Args in the Frame Title
        pass

    def startPlugin(self):
        LoopCount = 0
        self.theTaskEvent = self.createInstallTaskEventFromTask("TargetTest", self.callBackReceiveMessage)

    def stopPlugin(self):
                #  Remove TaskEvent from message Dispatcher
        self.removeTaskEvent(self.theTaskEvent)
        self.theFrame.destroy()
        pass

    LoopCount = 0
    LoopInternalCount = 0
    def MainLoopProcess(self):
        if self.LoopInternalCount >= 10:
            xTT = "LoopCount = "+ str(self.LoopCount)
            ICO3Log.print("Plugin",xTT)
            result = self.sendMessageToTarget("TargetTest", xTT)
            # result = self.createSendMessageCheck(self.theSenderHeader, xTT)
            self.LoopInternalCount = 0
            self.LoopCount += 1
        self.LoopInternalCount += 1
        pass

if __name__ == '__main__':
    vp_start_gui()





