# card stuff
proc cardfile {cardval} {
    global cardpicture
    return $cardpicture($cardval)
}
proc cardcolor {cardval} {
    global cardcolor
    set cc $cardcolor($cardval)
    if {$cc == "red"} { return "red" }
    if {$cc == "red"} { return "white" }
    if {$cc == "black"} { return "white" }
    return "green"
}

proc dealcardat {{crd} {x} {y} {facing}} {
    global tk_library
    set c .canvas.c

    if {$facing == "up"} {
	set item [$c create bitmap $x $y -foreground [cardcolor $crd] -bitmap @[cardfile $crd]]
    } else {
	set item [$c create bitmap $x $y -foreground [cardcolor back] -bitmap @[cardfile back]]
    }
    $c addtag point withtag $item
    $c addtag face:$facing withtag $item
    $c addtag card:$crd withtag $item
#    $c bind point <Any-Enter> "cardEnter $c %x %y"
#    $c bind point <Any-Leave> "cardEnter $c %x %y"
    $c bind point <1> "cardDown $c %x %y"
    $c bind point <Shift-1> "cardDownGrp $c %x %y"
#    $c bind point <ButtonRelease-1> "$c dtag selected"
    $c bind point <ButtonRelease-1> "cardDrop $c %x %y"
    $c bind point <Shift-ButtonRelease-1> "cardDropGrp $c %x %y"
#    $c bind point <2> "cardFlipOn $c %x %y"
#    $c bind point <3> "cardFlipOff $c %x %y"
#    $c bind point <2> "cardFlip $c %x %y"
    $c bind point <2> "reloadDeck $c"
    $c bind point <3> "cardFlip $c %x %y"
    bind $c <B1-Motion> "cardMove $c %x %y"
    bind $c <Shift-B1-Motion> "cardMove $c %x %y"
}

proc cardEnter {w x y} {
    $w itemconfig current -foreground [cardcolor [getprop_curr $w card]]
}

set card(lastX) 0
set card(lastY) 0
set card(firstX) 0
set card(firstY) 0


proc cardDrop {w x y} {
    global card
    puts stdout [$w coords selected]
    set xy [eval align_to_pile [$w coords selected]]
    eval $w coords selected $xy
#    puts stdout [$w coords selected]

    $w dtag selected
}

proc cardDown {w x y} {
    global card

    $w dtag selected
    $w addtag selected withtag current
    $w raise current
    
    set card(lastX) $x
    set card(lastY) $y
    set card(firstX) $x
    set card(firstY) $y
}

proc cardDownGrp {w x y} {
    global card

    $w dtag selected
    $w addtag selected withtag current

    set xy [$w coords selected]
 # $x $y
    puts stdout "carddowngrp $xy"
    set x1 [lindex $xy 0]
    set y1 [lindex $xy 1]
    set x2 [expr 3+$x1]
    set y2 [expr 400+$y1]

    $w dtag selected
    $w addtag selected overlapping $x1 $y1 $x2 $y2
    $w raise selected
#    $w addtag selected withtag current
#    $w addtag selected below current

#    $w raise selected
#    $w dtag current
#    $w addtag current withtag selected

    
    set card(lastX) $x
    set card(lastY) $y
    set card(firstX) $x
    set card(firstY) $y
}

proc cardDropGrp {w x y} {
    global card
    puts stdout cardDropGrp
    puts stdout [$w coords selected]
    set xy [eval align_to_pile [$w coords selected]]
    eval $w coords selected $xy
#    puts stdout [$w coords selected]

    $w dtag selected
}
proc cardMove {w x y} {
    global card
    $w move selected [expr $x-$card(lastX)] [expr $y-$card(lastY)]
    set card(lastX) $x
    set card(lastY) $y
}

proc cardFlipOff {w x y} {
    global card
    $w itemconfig current -bitmap @[cardfile back] -foreground [cardcolor back]
    putprop_curr $w face down
}

proc cardFlipOn {w x y} {
    global card
    $w itemconfig current -bitmap @[cardfile [getprop_curr $w card]] -foreground [cardcolor [getprop_curr $w card]]
    putprop_curr $w face up
}

proc cardFlip {w x y} {
    global card
    set facing [getprop_curr $w face]
    if {$facing == "up"} {
	cardFlipOff $w $x $y 
    } elseif {$facing == "down"} {
	cardFlipOn $w $x $y 
    } else {
	puts "bogus card face: $facing"
    }
}



##    foreach tag [$w gettags current] {
##	if {[regexp "^card:(.*)$" $tag xx face]} {
##	}
##    }
