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

dialect "rtobjectdraw"

type MoreDraggable = {
    // move the item by specified offsets
    moveBy (dx: Number, dy: Number) -> Done

    // move the item to a specified position
    moveTo(point: Point) -> Done

    // check to see if the item contains a specified location
    contains (point: Point) -> Boolean

    color -> Color

    color:= (newColor: Color) -> Done

    reset -> Done

    sendToFront -> Done // Add others!!
}

class tShirtAt (startPosn: Point) size (size':Number)
      on (canvas: DrawingCanvas) -> MoreDraggable {
    // dimensions of t-shirt pieces relative to size
    def size: Number = size' * 10

    def sleeveWidth: Number = size
    def sleeveHeight: Number = 0.2 * size

    def bodyWidth: Number = 0.6 * size
    def bodyHeight: Number = 0.65 * size

    def bodyInset: Number = 0.2 * size

    def neckWidth: Number = 0.3 * size
    def neckHeight: Number = 0.1 * size

    def neckInset: Number = 0.35 * size

    // location of sleeves
    def sleeveStart: Point = startPosn + (0 @ (neckHeight/2))

    def sleeves: Graphic2D =
          filledRectAt (sleeveStart) size (sleeveWidth @ sleeveHeight) on (canvas)
    sleeves.color := colorGen.white

    // create sleeves of t-shirt
    def sleeveTrim: Graphic =
          framedRectAt (sleeveStart)
          size (sleeveWidth @ sleeveHeight) on (canvas)

    // location of main body of t-shirt
    def bodyStart: Point = startPosn + (bodyInset @ (neckHeight / 2))

    // create main body of t-shirt
    def body: Graphic2D =
          filledRectAt (bodyStart) size (bodyWidth @ bodyHeight) on (canvas)
    body.color := colorGen.white

    def bodyTrim: Graphic2D =
          framedRectAt (bodyStart) size (bodyWidth @ bodyHeight) on (canvas)

    // location of neck inset of t-shirt
    def neckStart: Point = startPosn + (neckInset @ 0)

    // create neck of t-shirt
    def neck: Graphic2D =
          filledOvalAt (neckStart) size (neckWidth @ neckHeight) on (canvas)
    neck.color := colorGen.white

    def neckTrim: Graphic2D =
          framedOvalAt (neckStart) size (neckWidth @ neckHeight) on (canvas)

    // move the shirt by specified offsets
    method moveBy (dx: Number, dy: Number) -> Done {
        body.moveBy (dx, dy)
        bodyTrim.moveBy (dx ,dy)
        sleeves.moveBy (dx, dy)
        sleeveTrim.moveBy (dx, dy)
        neck.moveBy (dx, dy)
        neckTrim.moveBy (dx, dy)
    }

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

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

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

    // reset the color of the t-shirt
    method color:= (newColor: Color) -> Done {
        body.color := newColor
        sleeves.color := newColor
        neck.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 {
        sleeves.sendToFront
        sleeveTrim.sendToFront
        body.sendToFront
        bodyTrim.sendToFront
        neck.sendToFront
        neckTrim.sendToFront
    }

    // string representation of shirt
    method asString -> String {
        "Tshirt at {body.location} colored {body.color} having size {body.size}"
    }

}

