#!/usr/bin/env python
 
# Copyright (C) 2008 Joshua Bronson <jabronson@gmail.com>
#
# 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 3
# 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
 
 
from board import Board, Piece
from functools import partial
from time import sleep
from Canvas import Rectangle, Oval
 
 
# in pixels:
S = 50 # piece radius/side length of board squares
H = 8*S # board height
W = 8*S # board width
SBH = 20 # status bar height
 
 
class PieceTk(Piece):
    def __init__(self, r, c, color, canvas):
        Piece.__init__(self, r, c, color)
        self.canvas = canvas
        self.oval = Oval(canvas, c*S, r*S, (c+1)*S, (r+1)*S, fill=color)
 
    def flip(self):
        Piece.flip(self)
        self.canvas.itemconfig(self.oval, fill=self.color)
 
    def delete(self):
        self.oval.delete()
 
 
# XXX implement suggest move
class BoardTk(Board):
    def __init__(self, tk, canvas):
        self.tk = tk
        self.canvas = canvas
        Board.__init__(self, Piecetype=partial(PieceTk, canvas=canvas))
        self.drawboard()
        self.drawstatusbar()
        self.canvas.pack()
 
    def reset(self):
        Board.reset(self)
        self.click = None
        self.setstatusl('')
        self.setstatusc('')
        self.setstatusr('')
 
    def setstatusl(self, text):
        self.canvas.itemconfig(self.sbtxtl, text=text)
 
    def setstatusc(self, text):
        self.canvas.itemconfig(self.sbtxtc, text=text)
 
    def setstatusr(self, text):
        self.canvas.itemconfig(self.sbtxtr, text=text)
 
    def clickhandler(self, event):
        r, c = event.y//S, event.x//S
        self.click = r, c
 
    def drawboard(self):
        self.rects = [[Rectangle(self.canvas, c*S, r*S, (c+1)*S, (r+1)*S, fill='green') for c in range(8)] for r in range(8)]
        [[rect.bind('<1>', self.clickhandler) for rect in row] for row in self.rects]
 
    def drawstatusbar(self):
        self.sbrect = Rectangle(self.canvas, 0, 8*S, W, 8*S+SBH, fill='white')
        self.sbtxtl = self.canvas.create_text(5, 8*S + 5, text='', anchor="nw")
        self.sbtxtc = self.canvas.create_text((W - 20)//2 + 5, 8*S + 5, text='', anchor="n")
        self.sbtxtr = self.canvas.create_text(W - 20, 8*S + 5, text='', anchor="ne")
        # XXX W - 20 because OS X resize widget gets drawn *despite* setting non-resizable window
 
    def animatefunc(self):
        self.canvas.update()
        sleep(.1)
 
    def makemove(self, r, c, flip):
        Board.makemove(self, r, c, flip, animatefunc=self.animatefunc)
 
    def switchplayers(self, printboard=False, lostturn=False):
        if lostturn:
            self.click = None
            self.setstatusl('%s cannot move' % self.color1)
            sleep(.5)
        Board.switchplayers(self, printboard=printboard, lostturn=lostturn)
        self.setstatusl("%s's turn" % self.color1)
        self.setstatusr('black: %2d  white: %2d' % self.countpieces())
        self.click = None
 
    def togglepause(self, pause):
        if pause:
            self.setstatusc('paused')
        else:
            self.setstatusc('')
            self.click = None # in case there were clicks while we were paused
 
    def gameover(self):
        self.setstatusl('game: %s' % self.advantage())
        Board.gameover(self)