'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _export = require('../../components/style/export');

var _sharedPropertyStyle = require('./shared-property-style');

var _sharedPropertyStyle2 = _interopRequireDefault(_sharedPropertyStyle);

var _md = require('react-icons/md');

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

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var activeAssociate = { float: 'right', marginTop: 12, padding: '6px 12px', background: '#6F8E71', border: 'none', color: 'white' };
var deactiveAssociate = { float: 'right', marginTop: 12, padding: '6px 12px', background: 'gray', border: 'none', color: 'white' };
var searchIconStyle = { fontSize: '1.5em' };

var chooseCamera = {
  width: '80%',
  padding: 3,
  borderRadius: 3
};

var PropertyAssociated = function (_Component) {
  _inherits(PropertyAssociated, _Component);

  function PropertyAssociated(props) {
    _classCallCheck(this, PropertyAssociated);

    var _this = _possibleConstructorReturn(this, (PropertyAssociated.__proto__ || Object.getPrototypeOf(PropertyAssociated)).call(this, props));

    _this.state = {
      elements: [],
      matchString: '',
      matchedElements: [],
      associate: false,
      categories: [],
      things: [],
      selCat: "",
      selThing: ""
    };

    _this.selectCategory = _this.selectCategory.bind(_this);
    return _this;
  }

  _createClass(PropertyAssociated, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var value = this.props.value;

      var that = this;

      fetch('https://dev.api.artishok.io/reactor/admin/thing/categories').then(function (response) {
        return response.json();
      }).then(function (data) {
        that.setState({ categories: data });
      }).catch(function (err) {
        console.log("api get error : ", err);
      });

      if (value.get("category") != "-") {
        that.selectCategory(value.get("category"), that);
      }

      if (value.get("thing") != "-") that.setState({ selThing: value.get("thing") });
    }
  }, {
    key: 'selCat',
    value: function selCat(e) {
      this.selectCategory(e.target.value);
    }
  }, {
    key: 'selectCategory',
    value: function selectCategory(category, that) {

      that.setState({ selCat: category });
      fetch('https://dev.api.artishok.io/reactor/admin/thing/search', {
        method: 'post',
        body: JSON.stringify({ "options": {
            "size": 50,
            "page": "0"
          }, type: category })
      }).then(function (response) {
        return response.json();
      }).then(function (data) {
        that.setState({ things: data });
      }).catch(function (err) {
        console.log("api call error : ", err);
      });
    }
  }, {
    key: 'selThing',
    value: function selThing(e) {
      this.setState({ selThing: e.target.value });
    }
  }, {
    key: 'tryAssociate',
    value: function tryAssociate() {
      var _state = this.state,
          selCat = _state.selCat,
          selThing = _state.selThing;
      var _props = this.props,
          value = _props.value,
          onUpdate = _props.onUpdate;

      if (selThing < 0) {
        alert("please select category and things for associate.");return;
      }
      var thingName = "",
          popUp = "",
          hoverColor = "";
      this.state.things.map(function (item) {
        if (item.id == selThing) {
          thingName = item.name;
          popUp = item['popup'];
          hoverColor = item['color'];
        }
      });
      var merged = value.get("thing") == selThing ? value.merge({ category: "-", thing: "-", thingName: "", popUp: "", hoverColor: "#ffffff" }) : value.merge({ category: selCat, thing: selThing, thingName: thingName, popUp: popUp, hoverColor: hoverColor });
      return onUpdate(merged);
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          value = _props2.value,
          configs = _props2.configs;
      var _state2 = this.state,
          selCat = _state2.selCat,
          selThing = _state2.selThing;

      var category = "";
      var thing = "";
      category = value.get("category");
      thing = value.get("thing");

      return _react2.default.createElement(
        'table',
        { className: 'PropertyColor', style: (_sharedPropertyStyle2.default.tableStyle, { marginTop: 12, width: '100%' }) },
        _react2.default.createElement(
          'thead',
          null,
          _react2.default.createElement(
            'tr',
            null,
            _react2.default.createElement('td', null),
            _react2.default.createElement(
              'td',
              { style: { fontSize: 14, color: 'lightgreen', paddingBottom: 12 } },
              configs.label
            ),
            _react2.default.createElement('td', null)
          )
        ),
        _react2.default.createElement(
          'tbody',
          null,
          _react2.default.createElement(
            'tr',
            null,
            _react2.default.createElement(
              'td',
              { style: { width: '10%' } },
              _react2.default.createElement(_md.MdFolder, { style: searchIconStyle })
            ),
            _react2.default.createElement(
              'td',
              null,
              _react2.default.createElement(
                'select',
                { style: chooseCamera, onChange: this.selCat.bind(this), value: selCat },
                _react2.default.createElement(
                  'option',
                  { value: -1, key: -1 },
                  'Select Category'
                ),
                this.state.categories.map(function (item, index) {
                  return _react2.default.createElement(
                    'option',
                    { value: item, key: index, disabled: category == item ? true : false },
                    item
                  );
                })
              )
            )
          ),
          _react2.default.createElement(
            'tr',
            null,
            _react2.default.createElement(
              'td',
              { style: { width: '10%' } },
              _react2.default.createElement(_md.MdChildFriendly, { style: searchIconStyle })
            ),
            _react2.default.createElement(
              'td',
              null,
              _react2.default.createElement(
                'select',
                { style: chooseCamera, onChange: this.selThing.bind(this), value: selThing },
                _react2.default.createElement(
                  'option',
                  { value: -1, key: -1 },
                  'Select Thing'
                ),
                this.state.things.map(function (item, index) {
                  return _react2.default.createElement(
                    'option',
                    { value: item.id, key: index, disabled: thing == item.id ? true : false },
                    item.name
                  );
                })
              )
            )
          ),
          _react2.default.createElement(
            'tr',
            null,
            _react2.default.createElement('td', null),
            _react2.default.createElement(
              'td',
              null,
              _react2.default.createElement(
                'div',
                { style: { width: '80%' } },
                _react2.default.createElement(
                  'button',
                  { style: thing == selThing ? deactiveAssociate : activeAssociate, onClick: this.tryAssociate.bind(this) },
                  thing == selThing ? "Deassociate" : "Associate"
                )
              )
            )
          )
        )
      );
    }
  }]);

  return PropertyAssociated;
}(_react.Component);

exports.default = PropertyAssociated;


PropertyAssociated.propTypes = {
  value: _propTypes2.default.any.isRequired,
  onUpdate: _propTypes2.default.func.isRequired,
  configs: _propTypes2.default.object.isRequired,
  sourceElement: _propTypes2.default.object,
  internalState: _propTypes2.default.object,
  state: _propTypes2.default.object.isRequired
};