import React from "react"
import "./viewLayout.less"
import ReactGridLayout from 'react-grid-layout'
import { Icon, Modal } from 'antd'
import { connect } from 'dva'
import ChartView from './chartView'

class ViewLayout extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            previewItem: null,
            visiblePreviewBox: false,
            screenWidth: document.documentElement.clientWidth || document.body.clientWidth,
            screenHeight: document.documentElement.clientHeight || document.body.clientHeight
        };
    }

    componentDidMount() {
        window.addEventListener('resize', this.onWindowResize);
    }

    componentWillUnmount() {
        window.removeEventListener('resize', this.onWindowResize);
    }

    onWindowResize = () => {
        this.setState({
            screenWidth: document.documentElement.clientWidth || document.body.clientWidth,
            screenHeight: document.documentElement.clientHeight || document.body.clientHeight
        });
    }

    createElement = (item, isPreview) => {
        const { dispatch, main, dashboardDesigner } = this.props;
        const { currentUser } = main;
        const { editMode } = dashboardDesigner;
        const { code, name, viewType, layout, chartCode } = item;
        const iconCls = editMode ? 'visible-icon' : '';

        return (
            <div className={`chartview${ isPreview ? ' chartview-preview' : (editMode ? ' chartview-edit' : '')}`} key={code} data-grid={layout}>
                <div className='chartview-toolbar mover'>
                    <div className='chart-title'><span>{name}</span></div>
                    <div className='chart-tools'>
                        {viewType !== 'richText' && <Icon className={iconCls} type="reload" theme="outlined" onClick={() => {
                            dispatch({ type: 'dashboardDesigner/fetchChartData', item, mandatory: true });
                        }}/>}
                        {!isPreview && viewType !== 'richText' && <Icon className={iconCls} type="arrows-alt" onClick={() => this.showPreviewBox(item)}/>}
                        {editMode && !isPreview && item.creatorCode === currentUser.code && viewType !== 'richText' &&  <Icon className={iconCls} type='edit' onClick={() => {
                            dispatch({ type: 'dashboard/remoteModify' });
                            dispatch({ type: 'chartDesigner/reset' });
                            dispatch({ type: 'main/redirect', path: '/chart/' + chartCode });
                        }}/>}
                        {!isPreview && editMode && <Icon className={iconCls} type='delete' onClick={() => {
                            dispatch({ type: 'dashboardDesigner/deleteItem', item });
                        }} />}
                        {isPreview && <Icon className={iconCls} type="close" onClick={this.hidePreviewBox}/>}
                    </div>
                </div>
                <ChartView tableBodyHeight={isPreview ? (this.state.screenHeight * 0.8 - 24 - 40 - 50 - 38) : 50 * layout.h - 30 - 50 -38} editMode={isPreview ? false : editMode} item={{...item}}/>
            </div>
        )
    }

    onLayoutChange = (layout) => {
        const { dispatch, reset } = this.props;
        dispatch({ type: 'dashboardDesigner/changeLayout', layout });
        reset();
    }

    onResize = () => {
        const { reset } = this.props;
        reset();
    }

    showPreviewBox = (item) => {
        this.setState({
            previewItem: item,
            visiblePreviewBox: true
        });
    }

    hidePreviewBox = () => {
        this.setState({
            previewItem: null,
            visiblePreviewBox: false
        });
    }

    render() {
        const { dashboardDesigner, contentSize } = this.props;
        const { editMode } = dashboardDesigner;
        const { visiblePreviewBox, previewItem } = this.state;
        const children = dashboardDesigner.items.map((item) => this.createElement(item));
        return (<div>
            <ReactGridLayout
                width={contentSize.width}
                autoSize={true}
                cols={12}
                margin = {editMode ? [2, 2] : [0, 0]}
                rowHeight = {50}
                isDraggable={editMode}
                isResizable={editMode}
                draggableHandle='.mover'
                onLayoutChange={this.onLayoutChange}
                onResize={this.onResize}
                onBreakpointChange={this.onBreakpointChange}
                verticalCompact={true}
                compactType='vertical'
            >
                {children}
            </ReactGridLayout>
            <Modal
                className='previewbox'
                width='80%'
                height='80%'
                visible={visiblePreviewBox}
                onCancel={this.hidePreviewBox}
                footer={null}
                keyboard={true}
                maskClosable={true}
                >
                {/* {!!previewItem && this.createElement(previewItem, true)} */}
                {!!previewItem && this.createElement(dashboardDesigner.items.find(item => item.code === previewItem.code), true)}
            </Modal>
        </div>);
    }
}

export default connect(({ present: { main, dashboardDesigner } }) => ({ main, dashboardDesigner }))(ViewLayout);
