=begin
Timba language interpreter.
Copyright (C) 2016  Nicolas Guzzo

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
=end	
require './highlighter.rb'
	
class Editor < Qt::TextEdit
	
	def initialize(parent = nil)
	    super(parent)
	    @fileName=nil
		@highlighter = Highlighter.new	    
	    setupEditor()
	end
	def minimumSizeHint()
	    return Qt::Size.new(200, 200)
	end
	
	def sizeHint()
	    return Qt::Size.new(400, 400)
	end
	def newFile()
		@fileName =nil
	    self.clear()
	end
	def save(fn)
		file = Qt::File.new(fn)
		if file.open(Qt::File::WriteOnly | Qt::File::Text)
			file.write(self.plainText)
		end
		file.close
	end
	def saveFile()
		if @fileName !=nil and @fileName !=""
			save(@fileName)
		else
			@fileName = Qt::FileDialog.getSaveFileName(self,tr("Guardar Archivo"), "", "Timba Files (*.tba)")			
			save(@fileName) if @fileName !=nil and @fileName !=""
		end

	end
	def openFile(path = nil)
	    @fileName = path
	
	    if @fileName.nil?
	        @fileName = Qt::FileDialog.getOpenFileName(self,
	            tr("Abrir Archivo"), "", "Timba Files (*.tba)")
		end
	
	    if !@fileName.nil?
	        file = Qt::File.new(@fileName)
	        if file.open(Qt::File::ReadOnly | Qt::File::Text)
	            self.plainText = file.readAll.to_s
			end
			file.close
	    end
	    #@editor.document().contentsChanged()
	    @highlighter.highlight_all(self.document)
	end
	
	def setupEditor()
	    variableFormat = Qt::TextCharFormat.new
	    variableFormat.fontWeight = Qt::Font::Bold
	    variableFormat.foreground = Qt::Brush.new(Qt::blue)
	    @highlighter.addMapping('pila\s*[A-Z_]+', variableFormat)

	    
	    singleLineCommentFormat = Qt::TextCharFormat.new
	    singleLineCommentFormat.background = Qt::Brush.new(Qt::Color.new("#77ff77"))
	    @highlighter.addMapping('#[^\n]*', singleLineCommentFormat)
	
	    controlFormat = Qt::TextCharFormat.new	    
	    controlFormat.foreground = Qt::Brush.new(Qt::blue)
	    @highlighter.addMapping(/(sino|si\s+|mientras|nada\s+mas|repita)/i, controlFormat)

	    opFormat = Qt::TextCharFormat.new	    
	    opFormat.foreground = Qt::Brush.new(Qt::Color.new("#008000"))
	    @highlighter.addMapping(/(invierta(la)?|deposite(la)?|tome|(la\s+)?carta\s+)/i, opFormat)
		
		defFormat = Qt::TextCharFormat.new
	    defFormat.fontWeight = Qt::Font::Bold
	    defFormat.background = Qt::Brush.new(Qt::cyan)
	    defFormat.foreground = Qt::Brush.new(Qt::black)
	    @highlighter.addMapping(/(definicion\s+de\s+programa|ucp\s+ejecute\s+con\s+las\s+siguientes\s+cartas)/i, defFormat)
	    
	    font = Qt::Font.new
	    font.family = "Courier"
	    font.fixedPitch = true
	    font.pointSize = 10
	
	    self.font = font
	    @highlighter.addToDocument(self.document())
	end
	
end
