#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.9.3 on Fri Nov 15 01:15:10 2019
#

import wx
import random
from threading import Thread
from time import sleep
import sys
import re
# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode
# end wxGlade

command=""
regex_list=[]
arg1="\0"
arg2="\0"
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((950, 763))
        self.SetPosition((500,160))
        self.panel_1 = wx.Panel(self, wx.ID_ANY)
        self.panel_2 = wx.Panel(self.panel_1, wx.ID_ANY)
        self.panel_3 = wx.Panel(self.panel_2, wx.ID_ANY)
        self.panel_4 = wx.Panel(self.panel_2, wx.ID_ANY)
        self.panel_5 = wx.Panel(self.panel_1, wx.ID_ANY)
        self.gauge_1 = wx.Gauge(self.panel_5,wx.ID_ANY,range=100)
        self.gauge_2 = wx.Gauge(self.panel_5,wx.ID_ANY,range=100,style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)
        self.list_ctrl_1 = wx.ListCtrl(self.panel_3, wx.ID_ANY, style=wx.LC_REPORT)
        self.text_ctrl_1 = wx.TextCtrl(self.panel_3, wx.ID_ANY, style=wx.TE_PROCESS_ENTER)
        self.text_ctrl_2 = wx.TextCtrl(self.panel_4,wx.ID_ANY, style=wx.TE_MULTILINE|wx.TE_READONLY)
        self.list_ctrl_1.InsertColumn(0, 'Index')
        self.list_ctrl_1.InsertColumn(1, 'Command')
        self.list_ctrl_1.SetColumnWidth(0, 100)
        self.list_ctrl_1.SetColumnWidth(1, 400)
        if sys.version_info[0] >=3:
            self.Bind(wx.EVT_TEXT_ENTER,self.OnEnter,self.text_ctrl_1)
        else:
            self.Bind(wx.EVT_TEXT_ENTER,self.OnEnter_under_3,self.text_ctrl_1)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED,self.OnItemSelected,self.list_ctrl_1)
        self.font3 = wx.Font(11, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_BOLD)
        self.dlg=wx.ProgressDialog("VCS Connect","connecting with vcsd",100,style=wx.PD_APP_MODAL|wx.PD_AUTO_HIDE)
        self.target_velocity_text= wx.StaticText(self.panel_5,wx.ID_ANY)
        self.current_velocity_text= wx.StaticText(self.panel_5,wx.ID_ANY)
        self.t_vel=0
        self.c_vel=0
        self.speed=0
        self.monthread=Thread(target=self.mon_func)
        self.vcs_connect()
        self.__set_properties()
        self.__do_layout()
        
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("Ichthus Controller")
        self.lookahead_distance = 30
        self.lidar_hz="10hz"
        self.text_ctrl_2.SetFont(self.font3)
        self.text_ctrl_2.AppendText(" lookahead distance : ")
        self.text_ctrl_2.AppendText(str(self.lookahead_distance)+'\n')
        self.text_ctrl_2.AppendText(" lidar hz : ")
        self.text_ctrl_2.AppendText(self.lidar_hz)
        # end wxGlade
    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_6 = wx.BoxSizer(wx.VERTICAL)
        sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_8 = wx.BoxSizer(wx.VERTICAL)
        sizer_9 = wx.BoxSizer(wx.VERTICAL)
        sizer_10 = wx.BoxSizer(wx.VERTICAL)
        sizer_11 = wx.BoxSizer(wx.VERTICAL)

        label_1 = wx.StaticText(self.panel_5, wx.ID_ANY, "target_velocity")
        label_2 = wx.StaticText(self.panel_5, wx.ID_ANY, "current_velocity")
        
        sizer_11.Add(label_1, 0, wx.ALIGN_CENTER | wx.ALL, 1)
        sizer_11.Add(self.target_velocity_text, 0, wx.ALIGN_CENTER)
        sizer_11.Add(self.gauge_1, 0, wx.ALL | wx.EXPAND, 15)
        sizer_11.Add(self.gauge_2, 0, wx.ALL | wx.EXPAND, 15)
        sizer_11.Add(label_2, 0, wx.ALIGN_CENTER | wx.ALL, 1)
        sizer_11.Add(self.current_velocity_text,0,wx.ALIGN_CENTER)

        sizer_10.Add(sizer_11, 1, wx.EXPAND, 0)
        self.panel_5.SetSizer(sizer_10)

        sizer_6.Add(self.panel_5, 0, wx.ALL | wx.EXPAND, 60)
        sizer_8.Add(self.list_ctrl_1, 1, wx.BOTTOM | wx.EXPAND, 10)
        sizer_8.Add(self.text_ctrl_1, 0, wx.ALL | wx.EXPAND, 13)
        self.text_ctrl_1.SetFocus()

        self.panel_3.SetSizer(sizer_8)
        sizer_7.Add(self.panel_3, 1, wx.EXPAND, 0)
        sizer_9.Add(self.text_ctrl_2, 1, wx.ALL| wx.EXPAND, 0)
        self.panel_4.SetSizer(sizer_9)
        sizer_7.Add(self.panel_4, 1, wx.ALL|wx.EXPAND, 0)
        self.panel_2.SetSizer(sizer_7)
        sizer_6.Add(self.panel_2, 2, wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 0)
        self.panel_1.SetSizer(sizer_6)
        sizer_5.Add(self.panel_1, 1, wx.ALL | wx.EXPAND, 0)
        self.SetSizer(sizer_5)
        self.Layout()
        # end wxGlade

    def mon_func(self):
        while True:
            wx.MilliSleep(100)
            self.t_vel=self.t_vel%100
            self.t_vel+=1
            wx.CallAfter(self.update_mon_GUI)    
    def update_mon_GUI(self):
        self.gauge_1.SetValue(self.t_vel)
        self.gauge_2.SetValue(self.t_vel)
        self.current_velocity_text.SetLabel(str(self.t_vel))
        self.target_velocity_text.SetLabel(str(self.t_vel))

    def vcs_connect(self):
        message = "connecting"
        keepGoing = True
        count = 0
        while count < 100:
            if(count%20==0):
                message+="."
            count = count + 1
            wx.MilliSleep(25)
            keepGoing = self.dlg.Update(count,message)
        self.dlg.Destroy()

    def OnEnter(self, event):
        if not self.text_ctrl_1.GetValue():
            return
        num_items = self.list_ctrl_1.GetItemCount()
        command=self.text_ctrl_1.GetValue()
        self.list_ctrl_1.InsertItem(num_items,str(num_items),num_items)
        self.list_ctrl_1.SetItem(num_items,1,command)
        self.list_ctrl_1.SetScrollPos(wx.VERTICAL,self.list_ctrl_1.GetScrollRange(wx.VERTICAL))
        self.text_ctrl_1.Clear()

    def OnEnter_under_3(self,event):
        if not self.text_ctrl_1.GetValue():
            return
        num_items = self.list_ctrl_1.GetItemCount()
        command=self.text_ctrl_1.GetValue()
        self.list_ctrl_1.InsertStringItem(num_items, str(num_items))
        self.list_ctrl_1.SetStringItem(num_items, 1, command)
        self.list_ctrl_1.SetScrollPos(wx.VERTICAL,self.list_ctrl_1.GetScrollRange(wx.VERTICAL))
        self.text_ctrl_1.Clear()

    def OnItemSelected(self,event):
        self.text_ctrl_1.Clear()
        select=self.list_ctrl_1.GetFocusedItem()
        select_command=self.list_ctrl_1.GetItemText(select,1)
        self.text_ctrl_1.AppendText(select_command)
        self.text_ctrl_1.SetFocus()
    

            
        
            
# end of class MyFrame

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.monthread.start()
        self.frame.Show()
        return True

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()
