import { _decorator, Button, Component, Input, Node, Sprite } from 'cc';
import ActionEvent from './ActionEvent';
const { ccclass, property } = _decorator;

@ccclass('UIActions')
export class UIActions extends Component {
  @property({
    type: Sprite
  })
  leftBtn: Sprite;

  @property({
    type: Button
  })
  rightBtn: Button;

  @property({
    type: Button
  })
  upBtn: Button;

  @property({
    type: Button
  })
  downBtn: Button;

  @property({
    type: Button
  })
  jumpBtn: Button;

  start() {
    this.leftBtn.node.on(Input.EventType.TOUCH_START, this.onTouchLeftStart, this);
    this.leftBtn.node.on(Input.EventType.TOUCH_END, this.onTouchLeftEnd, this);

    this.rightBtn.node.on(Input.EventType.TOUCH_START, this.onTouchRightStart, this);
    this.rightBtn.node.on(Input.EventType.TOUCH_END, this.onTouchRightEnd, this);

    this.upBtn.node.on(Input.EventType.TOUCH_START, this.onTouchUpStart, this);
    this.upBtn.node.on(Input.EventType.TOUCH_END, this.onTouchUpEnd, this);

    this.downBtn.node.on(Input.EventType.TOUCH_START, this.onTouchDownStart, this);
    this.downBtn.node.on(Input.EventType.TOUCH_END, this.onTouchDownEnd, this);

    this.jumpBtn.node.on(Input.EventType.TOUCH_START, this.onTouchJumpStart, this);
    this.jumpBtn.node.on(Input.EventType.TOUCH_END, this.onTouchJumpEnd, this);
  }

  onTouchLeftStart() {
    ActionEvent.setPressedStatus('left', true);
    ActionEvent.setReleasedStatus('left', false);
  }
  onTouchLeftEnd() {
    ActionEvent.setPressedStatus('left', false);
    ActionEvent.setReleasedStatus('left', true);
  }

  onTouchRightStart() {
    ActionEvent.setPressedStatus('right', true);
    ActionEvent.setReleasedStatus('right', false);
  }
  onTouchRightEnd() {
    ActionEvent.setPressedStatus('right', false);
    ActionEvent.setReleasedStatus('right', true);
  }

  onTouchUpStart() {
    ActionEvent.setPressedStatus('up', true);
    ActionEvent.setReleasedStatus('up', false);
  }
  onTouchUpEnd() {
    ActionEvent.setPressedStatus('up', false);
    ActionEvent.setReleasedStatus('up', true);
  }

  onTouchDownStart() {
    ActionEvent.setPressedStatus('down', true);
    ActionEvent.setReleasedStatus('down', false);
  }
  onTouchDownEnd() {
    ActionEvent.setPressedStatus('down', false);
    ActionEvent.setReleasedStatus('down', true);
  }

  onTouchJumpStart() {
    ActionEvent.setPressedStatus('jump', true);
    ActionEvent.setReleasedStatus('jump', false);
  }
  onTouchJumpEnd() {
    ActionEvent.setPressedStatus('jump', false);
    ActionEvent.setReleasedStatus('jump', true);
  }
}

