"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _phaser = _interopRequireDefault(require("phaser"));

var _firebaseApp = _interopRequireDefault(require("../../../helper/firebaseApp"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var JoinRoomScene =
/*#__PURE__*/
function (_Phaser$Scene) {
  _inherits(JoinRoomScene, _Phaser$Scene);

  function JoinRoomScene() {
    var _this;

    _classCallCheck(this, JoinRoomScene);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(JoinRoomScene).call(this, {
      key: 'JoinRoomScene'
    }));

    _defineProperty(_assertThisInitialized(_this), "handleCreateRoom",
    /*#__PURE__*/
    _asyncToGenerator(
    /*#__PURE__*/
    regeneratorRuntime.mark(function _callee() {
      var roomId, s;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              roomId = _this.input.value;
              console.log('create room');
              _context.next = 4;
              return _firebaseApp["default"].firestore().collection('room').doc(roomId).get();

            case 4:
              s = _context.sent;

              if (!s.exists) {
                _context.next = 8;
                break;
              }

              _this.scene.launch('JoinRoomScene', {
                error: 'Room already exist'
              });

              return _context.abrupt("return");

            case 8:
              _context.next = 10;
              return _firebaseApp["default"].firestore().collection('room').doc(roomId).set({
                adminUserId: _this.game.playerId
              }, {
                merge: true
              });

            case 10:
              _this.scene.stop(_this.scene.key); // this.scene.get('RoomAdminScene').createRoom(roomId)


              _this.scene.launch('GameScene', {
                roomId: roomId
              }); // this.scene.get('GameScene').createRoom(roomId)


            case 12:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));

    _defineProperty(_assertThisInitialized(_this), "handleJoinRoom", function () {
      var roomId = _this.input.value;
      console.log('join room');

      _this.scene.stop(_this.scene.key);

      _this.game.roomId = roomId;

      _this.scene.get('GameScene').joinRoom(roomId);
    });

    _defineProperty(_assertThisInitialized(_this), "addButtons", function () {
      var buttons = []; // if (score > 0) {

      buttons = [_this.add.container(0, 0, [_this.add.image(0, 0, 'button').setDisplaySize(800, 200).setInteractive().on('pointerdown', _this.handleJoinRoom), _this.add.text(0, 0, 'Join room', {
        fontSize: '96px',
        fill: '#ffffff'
      }).setOrigin(0.5, 0.5)])]; // }

      buttons = [].concat(_toConsumableArray(buttons), [_this.add.container(0, buttons.length * 250, [_this.add.image(0, 0, 'button').setDisplaySize(800, 200).setInteractive().on('pointerdown', _this.handleCreateRoom), _this.add.text(0, 0, 'Create room', {
        fontSize: '96px',
        fill: '#ffffff'
      }).setOrigin(0.5, 0.5)])]);
      return buttons;
    });

    return _this;
  }

  _createClass(JoinRoomScene, [{
    key: "preload",
    value: function preload() {}
  }, {
    key: "create",
    value: function create() {
      var score = this.scene.get('GameScene').score;
      var width = this.game.config.width;
      var height = this.game.config.height;
      var input = document.createElement('input');
      input.setAttribute('type', 'text');
      input.setAttribute('name', 'roomId');
      input.setAttribute('placeholder', 'Enter room id');
      this.input = input;
      this.add.container(width / 2, height / 2, [this.add.image(0, 0, 'frame').setDisplaySize(1600, 1400), this.add.text(0, -500, 'Join Room', {
        fontSize: '126px',
        fill: '#000000'
      }).setOrigin(0.5, 0).setScrollFactor(0), this.add.dom(0, -150, input, {
        'font-size': '126px',
        width: '800px',
        'text-align': 'center'
      }), // this.add.image(-300, -200, score >= 1 ? 'star' : 'star-inactive'),
      // this.add.image(0, -200, score >= 2 ? 'star' : 'star-inactive'),
      // this.add.image(300, -200, score >= 3 ? 'star' : 'star-inactive'),
      this.add.container(0, 150, this.addButtons())]);
    }
  }]);

  return JoinRoomScene;
}(_phaser["default"].Scene);

var _default = JoinRoomScene;
exports["default"] = _default;