import React from 'react';
import { Pagination } from 'antd';
import classNames from 'classnames';
import styles from './index.less';
import { CommonProps, CommonState } from './interface';
import Table from './Table';
import OperateConfig from './operate';
import TableOperateConfig from './tableOperate';
import { defaultLocale } from '../locale';
import { Provider } from './context';

class AirTable extends React.Component<CommonProps, CommonState> {
    static getDerivedStateFromProps(nextProps: CommonProps, prevState: CommonState) {
        const { columns, dataSource } = prevState;

        const nextState: CommonState = {
            ...prevState,
        };
        if (JSON.stringify(dataSource) !== JSON.stringify(nextProps.dataSource)) {
            nextState.dataSource = nextProps.dataSource;
        }
        if (JSON.stringify(columns) !== JSON.stringify(nextProps.columns)) {
            nextState.columns = nextProps.columns;
        }
        return nextState;
    }

    constructor(props: CommonProps) {
        super(props);
        const { columns, dataSource } = props;
        this.state = {
            columns,
            dataSource,
        };
    }

    onScroll = () => {
        const { onScroll } = this.props;
        if (typeof onScroll === 'function') {
            onScroll();
        }
    };

    getContext = () => {
        const { locale } = this.props;
        if (locale) {
            if (locale.context) {
                if (locale.lang && defaultLocale[locale.lang]) {
                    return { ...defaultLocale[locale.lang], ...locale.context };
                }
                return locale.context;
            }
        }
        return defaultLocale.cn;
    };

    render() {
        const { columns, dataSource } = this.state;
        const {
            className,
            tableClassName,
            rowStyle,
            rowClassName,
            distanceToLoad,
            emitChangeCell,
            bordered,
            width,
            height,
            operateConfig,
            paginationConfig,
            showIndex,
            showExpand,
            emptyPlaceholder,
            cellEditable,
            rowHeight,
            loading,
            rowSelection,
            tableOperateConfig,
            noDataPlaceholder,
            columnWidth,
            headerHeight,
            contentMenu,
            onScroll,
            loadComp,
            showCondition,
            canFixed,
            id,
            onDragSorted,
            onEmitMsg,
            tableId,
            onDragFixed,
        } = this.props;
        const sortConfig = operateConfig
            && operateConfig.menusGroup
            && operateConfig.menusGroup.find((item: any) => {
                return item.type === 'sort';
            });
        return (
            <Provider value={{ locale: this.getContext() }}>
                <div
                    id={id}
                    className={
                        onScroll
                            ? classNames(styles.container, styles.scroll, className)
                            : classNames(styles.container, className)
                    }
                >
                    {operateConfig && (
                        <OperateConfig columns={columns} operateConfig={operateConfig} showCondition={showCondition} />
                    )}
                    <div
                        className={
                            onScroll
                                ? classNames(styles.tableContainer, styles.scroll, tableClassName)
                                : classNames(styles.tableContainer, tableClassName)
                        }
                    >
                        {tableOperateConfig && (
                            <div className={styles.tableOperateContainer}>
                                <TableOperateConfig operateConfig={tableOperateConfig} />
                            </div>
                        )}
                        <div className={styles.tableC}>
                            <Table
                                columns={columns}
                                dataSource={dataSource}
                                onScroll={this.onScroll}
                                rowStyle={rowStyle}
                                rowClassName={rowClassName}
                                distanceToLoad={distanceToLoad}
                                emitChangeCell={emitChangeCell}
                                bordered={bordered}
                                width={width}
                                height={height}
                                sortConfig={sortConfig}
                                paginationConfig={paginationConfig}
                                showIndex={showIndex}
                                showExpand={showExpand}
                                emptyPlaceholder={emptyPlaceholder}
                                cellEditable={cellEditable}
                                rowHeight={rowHeight}
                                headerHeight={headerHeight}
                                columnWidth={columnWidth}
                                loading={loading}
                                rowSelection={rowSelection}
                                noDataPlaceholder={noDataPlaceholder}
                                contentMenu={contentMenu}
                                loadComp={loadComp}
                                canFixed={canFixed}
                                onDragSorted={onDragSorted}
                                onEmitMsg={onEmitMsg}
                                tableId={tableId}
                                onDragFixed={onDragFixed}
                            />
                        </div>
                    </div>
                    {paginationConfig && <Pagination {...paginationConfig} />}
                </div>
            </Provider>
        );
    }
}

export default AirTable;
