require './BigCard.coffee'

################
# Card
################
Crafty.c 'Card',
  # Mouse interactivity, Tween (animation smooth)
  required: '2D, Canvas, Mouse, Tween'

  # initialize card
  init: ->
    @attr {
      z: CONF.layer.cards
      w: CONF.card.w
      h: CONF.card.h
      tapped: false
    }
    @origin 'center'
    return

  # on destroy (TODO/example)
  remove: ->
    Crafty.log 'Card was removed!'
    return

  events:
    # smooth placement
    Place: (position) ->
      @tween {
        x: position.x
        y: position.y
      }, CONF.anim.time, CONF.anim.func

    # Tap or untap, pass true iff you want to tap
    Tap: (newState) ->
      # store new state in property as boolean value
      @tapped = !!newState

      # animate to new rotation, @tapped has "int" value 0 or 1
      @tween {
        rotation: @baseRot + 90 * @tapped
      }, CONF.anim.time, CONF.anim.func
      return

    ToggleTap: ->
      @trigger 'Tap', !@tapped
      return

    # bring card to focus layer and back
    MouseOver: ->
      @z = CONF.layer.focus
      return

    MouseOut: ->
      @z = CONF.layer.cards
      return

    # show big version
    Enlarge: ->
      Crafty 'BigCard'
        .each ->
          @trigger 'Fade'
          return

      Crafty.e 'BigCard, Color'
        .color 'black'
        .trigger 'Copy', @
        .trigger 'Enlarge'
      return

    MouseUp: (e) ->
      if e.mouseButton == Crafty.mouseButtons.RIGHT
        @trigger 'Enlarge'
      return

################
# EnemyCard
################
Crafty.c 'EnemyCard',
  # is a card
  required: 'Card'

  # flipped by default
  init: ->
    @baseRot = 180
    @rotation = @baseRot
    return

################
# AllyCard
################
Crafty.c 'AllyCard',
  # is a card
  required: 'Card'

  # normally oriented by default
  init: ->
    @baseRot = 0
    @rotation = @baseRot
    return

  events:
    # un-/tappable
    DoubleClick: ->
      @trigger 'ToggleTap'
      return
