import React = require('react');
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { action, computed, observable } from 'mobx';
import { observer } from 'mobx-react';
import { emptyFunction, setupMoveUpEvents } from '../../../../Utils';
import './CollectionSchemaView.scss';
import { ColumnType } from './CollectionSchemaView';
import { IconButton } from 'browndash-components';
import { Colors } from '../../global/globalEnums';
import { ContextMenu } from '../../ContextMenu';
import { Doc, DocListCast } from '../../../../fields/Doc';
import { Id } from '../../../../fields/FieldSymbols';
import { RichTextField } from '../../../../fields/RichTextField';
import { StrCast } from '../../../../fields/Types';
import { ImageField } from '../../../../fields/URLField';
import { DocUtils, Docs } from '../../../documents/Documents';
import { ContextMenuProps } from '../../ContextMenuItem';
import { FormattedTextBox } from '../../nodes/formattedText/FormattedTextBox';

export interface SchemaColumnHeaderProps {
    columnKeys: string[];
    columnWidths: number[];
    columnIndex: number;
    sortField: string;
    sortDesc: boolean;
    setSort: (field: string, desc: boolean) => void;
    removeColumn: (index: number) => void;
    resizeColumn: (e: any, index: number, left: boolean) => void;
    // dragColumn: (e: any, index: number) => boolean;
    openContextMenu: (x: number, y: number, index: number) => void;
}

@observer
export class SchemaColumnHeader extends React.Component<SchemaColumnHeaderProps> {
    @computed get fieldKey() {
        return this.props.columnKeys[this.props.columnIndex];
    }

    @action
    sortClicked = (e: React.PointerEvent) => {
        e.stopPropagation();
        e.preventDefault();
        if (this.props.sortField == this.fieldKey) {
            this.props.setSort(this.fieldKey, !this.props.sortDesc);
        } else {
            this.props.setSort(this.fieldKey, false);
        }
    };

    // @action
    // onPointerDown = (e: React.PointerEvent) => {
    //     e.stopPropagation();

    //     setupMoveUpEvents(this, e, e => this.props.dragColumn(e, this.props.columnIndex), emptyFunction, emptyFunction);
    // };

    render() {
        return (
            <div className="schema-column-header" style={{ width: this.props.columnWidths[this.props.columnIndex] }}>
                <div className="schema-column-resizer left" onPointerDown={e => this.props.resizeColumn(e, this.props.columnIndex, true)}></div>
                <div className="schema-column-title">{this.fieldKey}</div>

                <div className="schema-header-menu">
                    <div className="schema-header-button" onPointerDown={e => this.props.openContextMenu(e.clientX, e.clientY, this.props.columnIndex)}>
                        <FontAwesomeIcon icon="ellipsis-h" />
                    </div>
                    <div className="schema-sort-button" onPointerDown={this.sortClicked} style={this.props.sortField == this.fieldKey ? { backgroundColor: Colors.MEDIUM_BLUE } : {}}>
                        <FontAwesomeIcon icon="caret-right" style={this.props.sortField == this.fieldKey ? { transform: `rotate(${this.props.sortDesc ? '270deg' : '90deg'})` } : {}} />
                    </div>
                </div>

                <div className="schema-column-resizer right" onPointerDown={e => this.props.resizeColumn(e, this.props.columnIndex, false)}></div>
            </div>
        );
    }
}
