"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const React = require('react');
const ReactDOM = require('react-dom');
const typings = require('./components.type');
const mobx_react_1 = require('mobx-react');
const src_1 = require('../../../../../../../../common/auto-bind/src');
const src_2 = require('../../../../../../../button/src');
const Sortable = require('sortablejs');
const drag_source_component_1 = require('./drag-source.component');
const switchTypes = [{
        type: 'custom',
        name: '定制'
    }, {
        type: 'base',
        name: '基础'
    }, {
        type: 'group',
        name: '组合'
    }];
let Components = class Components extends React.Component {
    constructor(...args) {
        super(...args);
        this.state = new typings.State();
    }
    componentDidMount() {
        /**
         * 子元素可拖拽
         */
        Sortable.create(ReactDOM.findDOMNode(this.dragContainerInstance), {
            animation: 150,
            // 放在一个组里,可以跨组拖拽
            group: {
                name: 'gaea-layout',
                pull: 'clone',
                put: false
            },
            sort: false,
            delay: 0,
            onStart: (event) => {
                this.lastDragStartIndex = event.oldIndex;
                this.props.viewport.startDragging('', this.getUniqueKeyByIndex(event.oldIndex), true, null, event.oldIndex);
            },
            onEnd: (event) => {
                this.props.viewport.endDragging();
                // 因为是 clone 方式, 拖拽成功的话元素会重复, 没成功拖拽会被添加到末尾
                // 所以先移除 clone 的元素（吐槽下, 拖走的才是真的, 留下的才是 clone 的）
                // 有 parentNode, 说明拖拽完毕还是没有被清除, 说明被拖走了, 因为如果没真正拖动成功, clone 元素会被删除
                if (event.clone.parentNode) {
                    // 有 clone, 说明已经真正拖走了
                    this.dragContainerDomInstance.removeChild(event.clone);
                    // 再把真正移过去的弄回来
                    if (this.lastDragStartIndex === this.dragContainerDomInstance.childNodes.length) {
                        // 如果拖拽的是最后一个
                        this.dragContainerDomInstance.appendChild(event.item);
                    }
                    else {
                        // 拖拽的不是最后一个
                        this.dragContainerDomInstance.insertBefore(event.item, this.dragContainerDomInstance.childNodes[this.lastDragStartIndex]);
                    }
                }
                else {
                }
            }
        });
    }
    /**
     * 获得第 n 个子元素的 uniqueKey
     */
    getUniqueKeyByIndex(index) {
        switch (this.state.selectedType) {
            case 'custom':
                return this.props.application.customComponents[index].defaultProps.gaeaUniqueKey;
            case 'base':
                return this.props.application.baseComponents[index].defaultProps.gaeaUniqueKey;
            case 'group':
                return 'combo';
        }
    }
    /**
     * 设置元素对象
     */
    setDragContainerInstance(ref) {
        this.dragContainerInstance = ref;
        this.dragContainerDomInstance = ReactDOM.findDOMNode(ref);
    }
    /**
     * 选中了一个类型
     */
    handleChangeSelectedType(type) {
        this.setState({
            selectedType: type
        });
    }
    /**
     * 生成组件类型选择按钮组
     */
    renderSwitchButtonGroup() {
        return switchTypes.map((item, index) => {
            // 可能会隐藏掉定制组件
            if (this.props.application.isHideCustomComponents && item.type === 'base') {
                return null;
            }
            return (<src_2.Button type="secondary" key={index} onClick={this.handleChangeSelectedType.bind(this, item.type)} active={item.type === this.state.selectedType}>
                    {item.name}
                </src_2.Button>);
        });
    }
    /**
     * 渲染拖拽组件
     */
    renderDragComponents() {
        switch (this.state.selectedType) {
            case 'custom':
                return this.props.application.customComponents.map((item, index) => {
                    return (<drag_source_component_1.default key={index}>
                            <i className={`fa fa-${item.defaultProps.gaeaIcon || 'cube'} icons`}/>
                            {item.defaultProps.gaeaName}
                        </drag_source_component_1.default>);
                });
            case 'base':
                return this.props.application.baseComponents.map((item, index) => {
                    return (<drag_source_component_1.default key={index}>
                            <i className={`fa fa-${item.defaultProps.gaeaIcon || 'cube'} icons gaea`}/>
                            {item.defaultProps.gaeaName}
                        </drag_source_component_1.default>);
                });
            case 'group':
                return this.props.application.comboComponents.map((component, index) => {
                    return (<drag_source_component_1.default key={index}>
                            <i className={`fa fa-cubes icons gaea`}/>
                            {component.name}
                        </drag_source_component_1.default>);
                });
        }
    }
    render() {
        const SwitchButtonGroup = this.renderSwitchButtonGroup();
        const DragComponents = this.renderDragComponents();
        return (<div className="_namespace components-container">
                <div className="container" ref={this.setDragContainerInstance}>
                    {DragComponents}
                </div>
                <div className="switch">
                    <src_2.ButtonGroup className="button-group" vertical>{SwitchButtonGroup}</src_2.ButtonGroup>
                </div>
            </div>);
    }
};
Components.defaultProps = new typings.Props();
__decorate([
    src_1.autoBindMethod
], Components.prototype, "getUniqueKeyByIndex", null);
__decorate([
    src_1.autoBindMethod
], Components.prototype, "setDragContainerInstance", null);
__decorate([
    src_1.autoBindMethod
], Components.prototype, "handleChangeSelectedType", null);
__decorate([
    src_1.autoBindMethod
], Components.prototype, "renderSwitchButtonGroup", null);
__decorate([
    src_1.autoBindMethod
], Components.prototype, "renderDragComponents", null);
Components = __decorate([
    mobx_react_1.inject('application', 'viewport'),
    mobx_react_1.observer
], Components);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Components;
