'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _isEqual = require('lodash/isEqual');

var _isEqual2 = _interopRequireDefault(_isEqual);

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

var WaterMark = function (_Component) {
    (0, _inherits3['default'])(WaterMark, _Component);

    function WaterMark(props) {
        (0, _classCallCheck3['default'])(this, WaterMark);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (WaterMark.__proto__ || Object.getPrototypeOf(WaterMark)).call(this, props));

        if (typeof document === 'undefined') {
            _this.canvas = null;
            _this.ctx = null;
        } else {
            _this.canvas = document.createElement('canvas');
            _this.ctx = _this.canvas.getContext('2d');
        }
        return _this;
    }

    (0, _createClass3['default'])(WaterMark, [{
        key: 'shouldComponentUpdate',
        value: function shouldComponentUpdate(nextProps) {
            return !(0, _isEqual2['default'])(this.props, nextProps);
        }
    }, {
        key: 'render',
        value: function render() {
            var props = this.props,
                _props = this.props,
                content = _props.content,
                _props$rotation = _props.rotation,
                rotation = _props$rotation === undefined ? 0 : _props$rotation,
                _props$fontFamily = _props.fontFamily,
                fontFamily = _props$fontFamily === undefined ? 'serif' : _props$fontFamily,
                _props$fontColor = _props.fontColor,
                fontColor = _props$fontColor === undefined ? 'white' : _props$fontColor,
                children = _props.children;

            if (typeof document === 'undefined') {
                return _react2['default'].createElement(
                    'div',
                    props,
                    children
                );
            }
            var _props$fontSize = props.fontSize,
                fontSize = _props$fontSize === undefined ? 16 : _props$fontSize,
                _props$lineHeight = props.lineHeight,
                lineHeight = _props$lineHeight === undefined ? 20 : _props$lineHeight,
                _props$gap = props.gap,
                gap = _props$gap === undefined ? 50 : _props$gap;
            var _window = window,
                devicePixelRatio = _window.devicePixelRatio;

            if (devicePixelRatio < 1) {
                devicePixelRatio = 1;
            }
            fontSize *= devicePixelRatio;
            lineHeight *= devicePixelRatio;
            gap *= devicePixelRatio;
            if (rotation < -Math.PI / 2 || rotation > Math.PI / 2) {
                throw new Error('旋转角度应该介于-PI / 2 - PI / 2之间');
            }
            var canvas = this.canvas,
                ctx = this.ctx;
            // ////
            // document.body.appendChild(canvas);
            // canvas.style.position = 'fixed';
            // canvas.style.top = 0;
            // canvas.style.left = 0;
            // canvas.style.backgroundColor = 'white';
            // ////

            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.fillStyle = fontColor;
            ctx.font = fontSize + 'px ' + fontFamily;
            var contentArray = content.split('\n');
            var wMatrix = [];
            var maxWidth = Math.ceil(contentArray.reduce(function (accumulator, currentValue) {
                var _ctx$measureText = ctx.measureText(currentValue),
                    width = _ctx$measureText.width;

                wMatrix.push(width);
                if (width > accumulator) {
                    return width;
                }
                return accumulator;
            }, 0));
            var maxHeight = lineHeight * contentArray.length;
            var w = maxWidth + gap * 2;
            var h = maxHeight + gap * 2;
            var r = rotation;
            if (r < 0) {
                r = -rotation;
            }
            canvas.height = Math.cos(r) * h + Math.sin(r) * w;
            canvas.width = Math.cos(r) * w + Math.sin(r) * h;
            ctx.translate(canvas.width / 2, canvas.height / 2);
            ctx.rotate(-rotation);
            var startY = (canvas.height - maxHeight) / 2;
            contentArray.forEach(function (line, index) {
                var startX = Math.round((canvas.width - wMatrix[index]) / 2);
                ctx.fillStyle = fontColor;
                ctx.font = fontSize + 'px ' + fontFamily;
                ctx.fillText(line, startX - canvas.width / 2, startY - canvas.height / 2 + (lineHeight - fontSize) / 2);
                startY += lineHeight;
            });
            var backgroundBase64 = canvas.toDataURL('image/png');
            var properties = {};
            properties.className = props.className;
            properties.style = (0, _extends3['default'])((0, _extends3['default'])({}, props.style), { position: 'relative' });
            return _react2['default'].createElement(
                'div',
                properties,
                children,
                _react2['default'].createElement('div', { style: {
                        background: 'url(\'' + backgroundBase64 + '\') 0 0 repeat',
                        backgroundSize: canvas.width / devicePixelRatio + 'px ' + canvas.height / devicePixelRatio + 'px',
                        position: 'absolute',
                        left: 0,
                        right: 0,
                        top: 0,
                        bottom: 0,
                        zIndex: 99,
                        pointerEvents: 'none',
                        opacity: 0.4
                    } })
            );
        }
    }]);
    return WaterMark;
}(_react.Component);

exports['default'] = WaterMark;
module.exports = exports['default'];