UI = {} --A convenient way to store all of the information the window/dialog creates.


-- create ToolTableDialog
UI.ToolTableDialog = wx.wxDialog (wx.NULL, wx.wxID_ANY, "Tool Table", wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxDEFAULT_DIALOG_STYLE )--create and name the window with desired settings
	UI.ToolTableDialog:SetSizeHints( wx.wxDefaultSize, wx.wxDefaultSize )
	
	UI.bSizer4 = wx.wxBoxSizer( wx.wxVERTICAL )--create a sizer to place elements in
	
	UI.m_ToolTableGrid = wx.wxGrid( UI.ToolTableDialog, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, 0 )
	
	-------------------------GRID SETTINGS------------------------------------------
	-- Grid
	UI.m_ToolTableGrid:CreateGrid( 254, 5 )
	UI.m_ToolTableGrid:EnableDragGridSize( False )
	UI.m_ToolTableGrid:SetMargins( 0, 0 )
	
	-- Columns
	UI.m_ToolTableGrid:SetColSize( 0, 100 )
	UI.m_ToolTableGrid:SetColSize( 1, 100 )
	UI.m_ToolTableGrid:SetColSize( 2, 100 )
	UI.m_ToolTableGrid:SetColSize( 3, 100 )
	UI.m_ToolTableGrid:SetColSize( 4, 200 )
	UI.m_ToolTableGrid:EnableDragColSize( True )
	UI.m_ToolTableGrid:SetColLabelSize( 30 )
	UI.m_ToolTableGrid:SetColLabelAlignment( wx.wxALIGN_CENTRE, wx.wxALIGN_CENTRE )
	
	-- Rows
	UI.m_ToolTableGrid:AutoSizeRows()
	UI.m_ToolTableGrid:EnableDragRowSize( True )
	UI.m_ToolTableGrid:SetRowLabelSize( 80 )
	UI.m_ToolTableGrid:SetRowLabelAlignment( wx.wxALIGN_CENTRE, wx.wxALIGN_CENTRE )
	
	-- Label Appearance
	
	-- Cell Defaults
	UI.m_ToolTableGrid:SetDefaultCellAlignment( wx.wxALIGN_LEFT, wx.wxALIGN_TOP )
	UI.m_ToolTableGrid:SetMinSize( wx.wxSize( 700,300 ) )
	UI.m_ToolTableGrid:SetMaxSize( wx.wxSize( -1,750 ) )
	-------------------------END GRID SETTINGS------------------------------------------
	
	UI.bSizer4:Add( UI.m_ToolTableGrid, 0, wx.wxALL + wx.wxALIGN_CENTER_HORIZONTAL, 5 )
	
	UI.m_sdbSizer2 = wx.wxStdDialogButtonSizer()
	UI.m_sdbSizer2OK = wx.wxButton( UI.ToolTableDialog, wx.wxID_OK, "" )--Add an "OK" button to the bottom of the dialog.
	UI.m_sdbSizer2:AddButton( UI.m_sdbSizer2OK )
	UI.m_sdbSizer2:Realize();
	
	UI.bSizer4:Add( UI.m_sdbSizer2, 1, wx.wxALIGN_CENTER_HORIZONTAL, 5 )
	
	
	UI.ToolTableDialog:SetSizer( UI.bSizer4 )
	UI.ToolTableDialog:Layout()
	UI.bSizer4:Fit( UI.ToolTableDialog )
	
	UI.ToolTableDialog:Centre( wx.wxBOTH )
	
	-- Connect Events
lastrow = 0
lastcol = 0
inst = mc.mcGetInstance()

	UI.m_ToolTableGrid:Connect( wx.wxEVT_GRID_CELL_CHANGE, function(event) --This is an event handler that happens when the user changes data in a cell.
		row = event:GetRow()
        col = event:GetCol()
        event:Skip()
		toolnum = row+1; --since tooling starts at tool 1 and row starts at 0, we have to add 1 to the row to get the correct tool.
		if(col == 0) then --update the length
			mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, toolnum, tonumber(UI.m_ToolTableGrid:GetCellValue(row, col)))
		elseif(col == 1) then --update the length wear
			mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT_W, toolnum, tonumber(UI.m_ToolTableGrid:GetCellValue(row, col)))
		elseif(col == 2) then --update the diameter
			mc.mcToolSetData(inst, mc.MTOOL_MILL_RAD, toolnum, tonumber(UI.m_ToolTableGrid:GetCellValue(row, col)))
		elseif(col == 3) then --update the diameter wear
			mc.mcToolSetData(inst, mc.MTOOL_MILL_RAD_W, toolnum, tonumber(UI.m_ToolTableGrid:GetCellValue(row, col)))
		elseif(col == 4) then --update the description
			mc.mcToolSetDesc(inst, toolnum, UI.m_ToolTableGrid:GetCellValue(row, col))
		end
	end )
	
	UI.m_ToolTableGrid:Connect( wx.wxEVT_GRID_CELL_LEFT_CLICK, function(event) --This is an event handler that happens when the user clicks in a cell.
		local rownum = event:GetRow()
        local colnum = event:GetCol()
        if(lastrow ~= rownum or lastcol ~= colnum) then
            UI.m_ToolTableGrid:SetCellBackgroundColour(tonumber(rownum), tonumber(colnum), wx.wxColour(wx.wxGREEN))--This code changes the new cell to green
            UI.m_ToolTableGrid:SetCellBackgroundColour(tonumber(lastrow), tonumber(lastcol), wx.wxColour(wx.wxWHITE))--This cell changes the old green cell to white.
            lastrow = rownum
            lastcol = colnum
            UI.m_ToolTableGrid:ForceRefresh()
        end
        event:Skip()
	end )
	
    function SetupToolTable()
        UI.m_ToolTableGrid:SetColLabelValue(0, "Length")--Set the labels for the columns
        UI.m_ToolTableGrid:SetColLabelValue(1, "Length Wear")
        UI.m_ToolTableGrid:SetColLabelValue(2, "Diameter")
        UI.m_ToolTableGrid:SetColLabelValue(3, "Diameter Wear")
        UI.m_ToolTableGrid:SetColLabelValue(4, "Description")
		
        local maxnumtools = 254
        local numcols = 5
        for i = 0, maxnumtools do --Loop through the rows
            for j = 0, numcols do --Loop through the columns
				local toolnum = i+1
                local returndouble
                local returnstring
                if(j == 0) then --If we are in the first column, then we get the height and set it to the cell we are in ->(i,j)
                    returndouble = mc.mcToolGetData(inst, mc.MTOOL_MILL_HEIGHT, toolnum)
                    UI.m_ToolTableGrid:SetCellValue(i,j, tostring(returndouble))
                elseif(j == 1) then--If we are in the second column, then we get the height wear and set it to the cell we are in ->(i,j)
                    returndouble = mc.mcToolGetData(inst, mc.MTOOL_MILL_HEIGHT_W, toolnum)
                    UI.m_ToolTableGrid:SetCellValue(i,j, tostring(returndouble))
                elseif(j == 2) then--If we are in the third column, then we get the radius, multiply it by 2 and set it to the cell we are in ->(i,j)
                    returndouble = mc.mcToolGetData(inst, mc.MTOOL_MILL_RAD, toolnum)
                    returndouble = returndouble * 2 --since it's the radius.
                    UI.m_ToolTableGrid:SetCellValue(i,j, tostring(returndouble))
                elseif(j == 3) then--If we are in the fourth column, then we get the radius wear, multiply it by 2 and set it to the cell we are in ->(i,j)
                    returndouble = mc.mcToolGetData(inst, mc.MTOOL_MILL_RAD_W, toolnum)
                    returndouble = returndouble * 2 --since it's the radius.
                    UI.m_ToolTableGrid:SetCellValue(i,j, tostring(returndouble))
                elseif(j == 4) then--If we are in the fifth column, then we get the description and set it to the cell we are in ->(i,j)
                    returnstring = mc.mcToolGetDesc(inst, toolnum)
                    UI.m_ToolTableGrid:SetCellValue(i,j,returnstring)
                end
            end
        end
    end

    SetupToolTable()
    UI.ToolTableDialog:Show()


--wx.wxGetApp():MainLoop()