'use strict';

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

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 _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactSlick = require('react-slick');

var _reactSlick2 = _interopRequireDefault(_reactSlick);

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; }

if (process.env.BROWSER) {}
var PrevArrow = function PrevArrow(props) {
  return _react2.default.createElement('div', _extends({}, props, { style: { background: 'url(' + props.src + ') no-repeat center center', height: '50px', width: '50px', left: '-50px', backgroundSize: 'cover' } }));
};

PrevArrow.defaultProps = {
  src: '/asset/icon/arrow_left.png'
};

var NextArrow = function NextArrow(props) {
  return _react2.default.createElement('div', _extends({}, props, { style: { background: 'url(' + props.src + ') no-repeat center center', height: '50px', width: '50px', right: '-50px', backgroundSize: 'cover' } }));
};

NextArrow.defaultProps = {
  src: '/asset/icon/arrow_right.png'
};

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

  function Leading(props, context) {
    _classCallCheck(this, Leading);

    return _possibleConstructorReturn(this, (Leading.__proto__ || Object.getPrototypeOf(Leading)).call(this, props, context));
  }

  _createClass(Leading, [{
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      if (this.refs.video) {
        this.refs.video.load();
      }
      if (this.refs.audio) {
        this.refs.audio.load();
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props;
      var leading = _props.leading;
      var mediaSource = _props.mediaSource;

      var embed = mediaSource.embed;
      var eventPeriod = mediaSource.eventPeriod ? mediaSource.eventPeriod : [null, null];
      var flag = mediaSource.flag ? mediaSource.flag : null;
      var heroImage = _lodash2.default.get(mediaSource.heroImage, ['image'], {});
      var imageInfo = _lodash2.default.map(mediaSource.images, function (itm) {
        return {
          'filename': _lodash2.default.get(itm, ['image', 'filename']),
          'filetype': _lodash2.default.get(itm, ['image', 'filetype']),
          'height': _lodash2.default.get(itm, ['image', 'height']),
          'width': _lodash2.default.get(itm, ['image', 'width']),
          'url': _lodash2.default.get(itm, ['image', 'url'])
        };
      });
      var isFeatured = mediaSource.isFeatured ? mediaSource.isFeatured : false;
      var settings = {
        dots: false,
        infinite: true,
        speed: 500,
        slidesToShow: 1,
        slidesToScroll: 1,
        autoplay: true,
        autoplaySpeed: 3000,
        cssEase: 'linear',
        fade: this.props.device === 'desktop' ? true : false,
        draggable: this.props.device === 'desktop' ? false : true,
        prevArrow: _react2.default.createElement(PrevArrow, { src: '/asset/icon/golden_Horse_arrow_left.png' }),
        nextArrow: _react2.default.createElement(NextArrow, { src: '/asset/icon/golden_Horse_arrow_right.png' })
      };
      var video = _lodash2.default.get(mediaSource.heroVideo, ['video'], {});
      var audio = _lodash2.default.get(mediaSource.audio, ['audio'], {});

      if (EventStuff.ifShowLeading(eventPeriod[0], eventPeriod[1], flag, isFeatured)) {
        switch (leading) {
          case 'slideshow':
            if (imageInfo.length > 0) {
              return _react2.default.createElement(
                'div',
                { className: 'container' },
                _react2.default.createElement(
                  'div',
                  { className: 'leading-container' },
                  _react2.default.createElement(
                    _reactSlick2.default,
                    settings,
                    _lodash2.default.map(imageInfo, function (img) {
                      return _react2.default.createElement(
                        'div',
                        { className: 'slide-container', key: img.filename, style: { height: '100%', backgroundColor: 'rgba(255,255,255,0.1)' } },
                        _react2.default.createElement('div', { className: 'slide', style: { background: 'url(' + img.url + ') no-repeat center center', backgroundSize: 'contain', height: img.height + 'px', maxHeight: '400px' } })
                      );
                    })
                  )
                )
              );
            } else {
              return _react2.default.createElement(ShowNothing, null);
            }
            break;
          case 'image':
            if (heroImage && heroImage.url && heroImage.url.length > 0) {
              return _react2.default.createElement(
                'div',
                { className: 'container' },
                _react2.default.createElement(
                  'div',
                  { className: 'leading-container' },
                  _react2.default.createElement(
                    'div',
                    { className: 'leading-container--fit' },
                    _react2.default.createElement(
                      'div',
                      { className: 'img', style: { maxHeight: '550px', overflow: 'hidden' } },
                      _react2.default.createElement('img', { src: heroImage.url, style: { width: '100%' } })
                    )
                  )
                )
              );
            } else {
              return _react2.default.createElement(ShowNothing, null);
            }
            break;
          case 'video':
            if (video.url && video.url.length > 0) {
              return _react2.default.createElement(
                'div',
                { className: 'container' },
                _react2.default.createElement(
                  'div',
                  { className: 'leading-container' },
                  _react2.default.createElement(
                    'div',
                    { className: 'leading-container--fit' },
                    _react2.default.createElement(
                      'video',
                      { style: { width: '100%' }, controls: true, ref: 'video' },
                      _react2.default.createElement('source', { src: video.url, type: video.filetype })
                    )
                  )
                )
              );
            } else {
              return _react2.default.createElement(ShowNothing, null);
            }
            break;
          case 'audio':
            if (audio.url && audio.url.length > 0) {
              return _react2.default.createElement(
                'div',
                { className: 'container' },
                _react2.default.createElement(
                  'div',
                  { className: 'leading-container' },
                  _react2.default.createElement(
                    'div',
                    { className: 'leading-container--fit' },
                    _react2.default.createElement(
                      'audio',
                      { style: { width: '100%', minWidth: '100%' }, 'data-file': audio.url, controls: true, ref: 'audio' },
                      _react2.default.createElement('source', { src: audio.url, type: audio.filetype })
                    )
                  )
                )
              );
            } else {
              return _react2.default.createElement(ShowNothing, null);
            }
            break;
          case 'embedded':
            if (embed && _lodash2.default.trim(embed).length > 0) {
              return _react2.default.createElement(
                'div',
                { className: 'container' },
                _react2.default.createElement(
                  'div',
                  { className: 'leading-container' },
                  _react2.default.createElement(
                    'div',
                    { className: 'leading-container--fit' },
                    _react2.default.createElement('div', { className: 'leading-container_embedded', dangerouslySetInnerHTML: { __html: embed } })
                  )
                )
              );
            } else {
              return _react2.default.createElement(ShowNothing, null);
            }
            break;
          default:
            return _react2.default.createElement(ShowNothing, null);
        }
      } else {
        return _react2.default.createElement(ShowNothing, null);
      }
    }
  }]);

  return Leading;
}(_react.Component);

exports.default = Leading;
exports.Leading = Leading;


var ShowNothing = function ShowNothing() {
  return _react2.default.createElement('div', null);
};

var EventStuff = function () {
  function EventStuff() {
    _classCallCheck(this, EventStuff);
  }

  _createClass(EventStuff, null, [{
    key: 'ifShowLeading',
    value: function ifShowLeading() {
      var startDate = arguments.length <= 0 || arguments[0] === undefined ? Date.now() : arguments[0];
      var endDate = arguments.length <= 1 || arguments[1] === undefined ? Date.now() : arguments[1];
      var flag = arguments[2];
      var isFeatured = arguments[3];

      var dNow = new Date();
      var sDt = new Date(startDate);
      var eDt = new Date(endDate);
      return flag !== 'event' || isFeatured && flag === 'event' && dNow <= eDt && dNow >= sDt ? true : false;
    }
  }]);

  return EventStuff;
}();