// Class to draw and move a T-shirt
//
// @author Kim Bruce

dialect "rtobjectdraw"
import "LaundryType" as lt

// type of laundry items that can be dragged around
class pantsAt (startPosn:Point) size (size':Number)
      on (canvas:DrawingCanvas) -> lt.Laundry {
    // dimensions of t-shirt pieces relative to size
    def size: Number = size' * 9

    def hipWidth: Number = size / 3
    def hipHeight: Number = size / 3

    def legWidth: Number = hipWidth / 2 - 3
    def legHeight: Number = size - hipHeight


    //draw legs and hips
    def leftLegStart: Point = startPosn + (0 @ hipHeight)

    def leftLeg: Graphic2D =
          filledRectAt (leftLegStart) size (legWidth @ legHeight) on (canvas)

    def leftLegTrim: Graphic2D =
          framedRectAt (leftLegStart) size (legWidth @ legHeight)on(canvas)

    def rightLegStart: Point = startPosn + ((hipWidth - legWidth) @ hipHeight)

    def rightLeg: Graphic2D =
          filledRectAt (rightLegStart) size (legWidth @ legHeight)on(canvas)

    def rightLegTrim: Graphic2D =
          framedRectAt (rightLegStart) size (legWidth @ legHeight) on (canvas)

    def hips: Graphic2D =
          filledRectAt (startPosn) size (hipWidth @ hipHeight) on (canvas)

    def hipTrim: Graphic2D =
          framedRectAt (startPosn) size (hipWidth @ hipHeight) on (canvas)

    // move the shirt by specified offsets
    method moveBy (dx:Number, dy:Number) -> Done {
        leftLeg.moveBy (dx, dy)
        leftLegTrim.moveBy (dx, dy)
        rightLeg.moveBy (dx, dy)
        rightLegTrim.moveBy (dx, dy)
        hips.moveBy (dx, dy)
        hipTrim.moveBy (dx, dy)
    }

    // check to see if the shirt contains a specified location
    method contains(point:Point) -> Boolean {
        leftLeg.contains (point) || rightLeg.contains (point)
              || hips.contains (point)
    }

    // move the shirt to a specified position
    method moveTo (point: Point) -> Done{
        moveBy (point.x - hips.x, point.y - hips.y)
    }

    // return color of the t-shirt
    method color -> Color {hips.color}

    // reset the color of the t-shirt
    method color:= (newColor: Color) -> Done {
        leftLeg.color := newColor
        rightLeg.color := newColor
        hips.color := newColor
    }

    // move t-shirt back to position it was created at
    method reset -> Done {
        moveTo (startPosn)
    }

    // move t-shirt above all other graphics objects
    method sendToFront -> Done {
        rightLeg.sendToFront
        rightLegTrim.sendToFront
        leftLeg.sendToFront
        leftLegTrim.sendToFront
        hips.sendToFront
        hipTrim.sendToFront
    }

    // move t-shirt above all other graphics objects
    method removeFromCanvas -> Done {
        rightLeg.removeFromCanvas
        rightLegTrim.removeFromCanvas
        leftLeg.removeFromCanvas
        leftLegTrim.removeFromCanvas
        hips.removeFromCanvas
        hipTrim.removeFromCanvas
    }

    // string representation of shirt
    method asString -> String {
        "Pants at {leftLeg.location} colored {hips.color} having size {size'}"
    }

}

