=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 './timba.rb'
class View < Qt::Widget
	attr_writer :timba
	def initialize(parent = nil)
	    super(parent)
	    @timba=nil

	    newFont = font()
	    newFont.pixelSize = 16
	    setFont(newFont)

	    @bg = Qt::Pixmap.new
	    @bg.load("img/bg.png")
		@bg_brush=Qt::Brush.new(@bg)
		
		@cards={:oros => [],:copas => [],:espadas => [],:bastos => []}
		@cards.each do |k,v|			
			1.upto(12) do |i|
				@cards[k] << Qt::Pixmap.new("img/cards/#{k}_#{i.to_s.rjust(2, "0")}.png")
			end
		end
		#pp @cards
		@backface       = Qt::Pixmap.new("img/cards/carta.png")
		@suits_px={}
	    @suits_px[:copas]   = Qt::Pixmap.new("img/copas.png")
	    @suits_px[:oros]    = Qt::Pixmap.new("img/oros.png")
	    @suits_px[:espadas] = Qt::Pixmap.new("img/espadas.png")
	    @suits_px[:bastos]  = Qt::Pixmap.new("img/bastos.png")
	    @minh=100
	end
	def get_card(suit,number)
		 @cards[suit][number-1]
	end
	def minimumSizeHint()
	    return Qt::Size.new(100, @minh)
	end	
	def sizeHint()
	    return Qt::Size.new(800, 600)
	end
	
	def paintEvent(event)
	    painter = Qt::Painter.new(self)
	    painter.fillRect(event.rect, @bg_brush)
	    hand_y_pos=30
	    piles_y_pos=300
	    painter.pen = Qt::Color.new(Qt::black)
	    
	    @timba.mutex.synchronize{
		    j=0
		    hhs=[]
		    
		    @timba.stacks.each do |st|
		    	inc1=20
		    	inc2=15
		    	x=10+140*j
		    	painter.drawText(x+10, piles_y_pos-10,st[:name])
		    	i=0
		    	last_card=nil
		    	if st[:list].count > 0
		    		mh=piles_y_pos+(st[:list].count*(153+inc1)) 
		    		if mh> height
		    			#inc1=0
		    			#inc2=0
		    		end
		    	end
				st[:list].each do |c|
					if c.status == :up 
						y=piles_y_pos+i
						painter.drawPixmap(x, y,@cards[c.suit][c.num-1])						
						painter.drawPixmap(x+102, y,@suits_px[c.suit]) if inc1>0
						i+=inc1
					else
						y=piles_y_pos+i
						i+=inc2
		    			painter.drawPixmap(x, y,@backface)
		    		end					
				end
				hhs<<piles_y_pos+i+100
				j=j+1
			end
			if hhs!= nil and hhs.count >0
				maxh=hhs.max
				if maxh > self.height()
					#puts("resize #{maxh} #{self.height()}")
					@minh=maxh
					setMinimumSize(100,maxh)
				end
			end
			painter.drawText(10, 20 ,"Condición: #{@timba.condition}")
			if @timba.card!=nil			
				if @timba.card.status == :up
	    			painter.drawPixmap(50, hand_y_pos,@cards[@timba.card.suit][@timba.card.num-1] )
	    		else
	    			painter.drawPixmap(50, hand_y_pos,@backface)
	    		end
			end
			if @timba.error!=""
				painter.pen = Qt::Color.new(Qt::red)
				painter.drawText(10, 250 ,"Error: #{@timba.error}")
			end
		}
	    painter.end
	end
end
